01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tcspring;
06:
07: import org.apache.commons.logging.Log;
08: import org.apache.commons.logging.LogFactory;
09: import org.springframework.beans.BeansException;
10: import org.springframework.beans.PropertyValues;
11: import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
12:
13: import java.beans.PropertyDescriptor;
14:
15: /**
16: * Post process local beans for distributing.
17: *
18: * @author Eugene Kuleshov
19: */
20: public class DistributableBeanPostProcessor implements
21: InstantiationAwareBeanPostProcessor {
22:
23: private final transient Log logger = LogFactory.getLog(getClass());
24:
25: private final DistributableBeanFactory factory;
26:
27: public DistributableBeanPostProcessor(
28: DistributableBeanFactory factory) {
29: this .factory = factory;
30: }
31:
32: public Object postProcessBeforeInitialization(Object bean,
33: String beanName) throws BeansException {
34: logger.info(factory.getId()
35: + " post processing before initialization "
36: + isDistributed(beanName));
37: return bean;
38: }
39:
40: public Object postProcessAfterInitialization(Object bean,
41: String beanName) throws BeansException {
42: logger.info(factory.getId()
43: + " post processing after initialization "
44: + isDistributed(beanName));
45: if (factory.isDistributedSingleton(beanName)) {
46: ComplexBeanId beanId = new ComplexBeanId(beanName);
47: BeanContainer container = factory.getBeanContainer(beanId);
48: if (container == null) {
49: logger.info(factory.getId() + " distributing new bean "
50: + beanName);
51: factory.putBeanContainer(beanId, new BeanContainer(
52: bean, true));
53: } else {
54: logger.info(factory.getId()
55: + " initializing existing bean " + beanName);
56: factory.initializeBean(beanId, bean, container);
57: container.setInitialized(true);
58: return container.getBean();
59: }
60: }
61: return bean;
62: }
63:
64: public Object postProcessBeforeInstantiation(Class beanClass,
65: String beanName) throws BeansException {
66: logger.info(factory.getId()
67: + " post processing before instantiation "
68: + isDistributed(beanName));
69: return null;
70: }
71:
72: public boolean postProcessAfterInstantiation(Object bean,
73: String beanName) throws BeansException {
74: logger.info(factory.getId()
75: + " post processing after instantiation "
76: + isDistributed(beanName));
77: return true;
78: }
79:
80: public PropertyValues postProcessPropertyValues(PropertyValues pvs,
81: PropertyDescriptor[] pds, Object bean, String beanName)
82: throws BeansException {
83: logger.info(factory.getId()
84: + " post processing property values "
85: + isDistributed(beanName));
86: return pvs;
87: }
88:
89: private String isDistributed(String beanName) {
90: return (factory.isDistributedSingleton(beanName) ? "distributed"
91: : "local")
92: + " bean " + beanName;
93: }
94:
95: }
|