Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 14, 2016
0 parents commit 411f8d4
Show file tree
Hide file tree
Showing 743 changed files with 31,843 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Freeware License, some rights reserved

Copyright (c) 2010 Gary Mak, Daniel Rubio, and Josh Long

Permission is hereby granted, free of charge, to anyone obtaining a copy
of this software and associated documentation files (the "Software"),
to work with the Software within the limits of freeware distribution and fair use.
This includes the rights to use, copy, and modify the Software for personal use.
Users are also allowed and encouraged to submit corrections and modifications
to the Software for the benefit of other users.

It is not allowed to reuse, modify, or redistribute the Software for
commercial use in any way, or for a user�s educational materials such as books
or blog articles without prior permission from the copyright holder.

The above copyright notice and this permission notice need to be included
in all copies or substantial portions of the software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#Apress Source Code

This repository accompanies [*Spring Recipes*](http://www.apress.com/9781430224990) by Gary Mak, Daniel Rubio, and Josh Long (Apress, 2010).

![Cover image](9781430224990.jpg)

Download the files as a zip using the green button, or clone the repository to your machine using Git.

##Releases

Release v1.0 corresponds to the code in the published book, without corrections or updates.

##Contributions

See the file Contributing.md for more information on how you can contribute to this repository.
29 changes: 29 additions & 0 deletions Spring Recipes 2nd Ed/advancedioc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.apress.springrecipes</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>advancedioc</artifactId>
<name>Advanced IOC</name>
<dependencies>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.javaee</groupId>
<artifactId>jboss-jca-api</artifactId>
</dependency>

</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.apress.springrecipes.shop;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.springframework.core.io.Resource;

public class BannerLoader {

private Resource banner;

public void setBanner(Resource banner) {
this.banner = banner;
}

public void showBanner() throws IOException {
InputStream in = banner.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while (true) {
String line = reader.readLine();
if (line == null)
break;
System.out.println(line);
}
reader.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.apress.springrecipes.shop;

public class Battery extends Product {

private boolean rechargeable;

public Battery() {
super();
}

public Battery(String name, double price) {
super(name, price);
}

public boolean isRechargeable() {
return rechargeable;
}

public void setRechargeable(boolean rechargeable) {
this.rechargeable = rechargeable;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.apress.springrecipes.shop;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.Locale;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;

public class Cashier implements BeanNameAware, MessageSourceAware,
ApplicationEventPublisherAware, StorageConfig {

private String name;
private String path;
private BufferedWriter writer;
private MessageSource messageSource;
private ApplicationEventPublisher applicationEventPublisher;

public void setBeanName(String beanName) {
this.name = beanName;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}

public void setApplicationEventPublisher(
ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}

@PostConstruct
public void openFile() throws IOException {
File logFile = new File(path, name + ".txt");
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(logFile, true)));
}

public void checkout(ShoppingCart cart) throws IOException {
double total = 0;
for (Product product : cart.getItems()) {
total += product.getPrice();
}
writer.write(new Date() + "\t" + total + "\r\n");
writer.flush();

String alert = messageSource.getMessage("alert.checkout", new Object[] {
total, new Date() }, Locale.US);
System.out.println(alert);

CheckoutEvent event = new CheckoutEvent(this, total, new Date());
applicationEventPublisher.publishEvent(event);
}

@PreDestroy
public void closeFile() throws IOException {
writer.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.apress.springrecipes.shop;

import java.util.Date;

import org.springframework.context.ApplicationEvent;

public class CheckoutEvent extends ApplicationEvent {

private double amount;
private Date time;

public CheckoutEvent(Object source, double amount, Date time) {
super(source);
this.amount = amount;
this.time = time;
}

public double getAmount() {
return amount;
}

public Date getTime() {
return time;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.apress.springrecipes.shop;

import java.util.Date;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

public class CheckoutListener implements ApplicationListener {

public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof CheckoutEvent) {
double amount = ((CheckoutEvent) event).getAmount();
Date time = ((CheckoutEvent) event).getTime();

// Do anything you like with the checkout amount and time
System.out.println("Checkout event [" + amount + ", " + time + "]");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.apress.springrecipes.shop;

public class Disc extends Product {

private int capacity;

public Disc() {
super();
}

public Disc(String name, double price) {
super(name, price);
}

public int getCapacity() {
return capacity;
}

public void setCapacity(int capacity) {
this.capacity = capacity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.apress.springrecipes.shop;

import org.springframework.beans.factory.config.AbstractFactoryBean;

public class DiscountFactoryBean extends AbstractFactoryBean {

private Product product;
private double discount;

public void setProduct(Product product) {
this.product = product;
}

public void setDiscount(double discount) {
this.discount = discount;
}

public Class getObjectType() {
return product.getClass();
}

protected Object createInstance() throws Exception {
product.setPrice(product.getPrice() * (1 - discount));
return product;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.apress.springrecipes.shop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) throws Exception {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");

Product aaa = (Product) context.getBean("aaa");
Product cdrw = (Product) context.getBean("cdrw");
Product dvdrw = (Product) context.getBean("dvdrw");

ShoppingCart cart1 = (ShoppingCart) context.getBean("shoppingCart");
cart1.addItem(aaa);
cart1.addItem(cdrw);
System.out.println("Shopping cart 1 contains " + cart1.getItems());

ShoppingCart cart2 = (ShoppingCart) context.getBean("shoppingCart");
cart2.addItem(dvdrw);
System.out.println("Shopping cart 2 contains " + cart2.getItems());

Cashier cashier1 = (Cashier) context.getBean("cashier1");
cashier1.checkout(cart1);

ProductRanking productRanking =
(ProductRanking) context.getBean("productRanking");
System.out.println(
"Product ranking from " + productRanking.getFromDate() +
" to " +productRanking.getToDate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.apress.springrecipes.shop;

import java.io.File;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.PriorityOrdered;

public class PathCheckingBeanPostProcessor implements BeanPostProcessor, PriorityOrdered {

private int order;

public int getOrder() {
return order;
}

public void setOrder(int order) {
this.order = order;
}

public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof StorageConfig) {
String path = ((StorageConfig) bean).getPath();
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
}
return bean;
}

public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
}

0 comments on commit 411f8d4

Please sign in to comment.