KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tcspring > DistributableBeanPostProcessor


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tcspring;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 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 JavaDoc;
14
15 /**
16  * Post process local beans for distributing.
17  *
18  * @author Eugene Kuleshov
19  */

20 public class DistributableBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
21
22   private final transient Log logger = LogFactory.getLog(getClass());
23
24   private final DistributableBeanFactory factory;
25
26   public DistributableBeanPostProcessor(DistributableBeanFactory factory) {
27     this.factory = factory;
28   }
29
30   public Object JavaDoc postProcessBeforeInitialization(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
31     logger.info(factory.getId() + " post processing before initialization " + isDistributed(beanName));
32     return bean;
33   }
34
35   public Object JavaDoc postProcessAfterInitialization(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
36     logger.info(factory.getId() + " post processing after initialization " + isDistributed(beanName));
37     if (factory.isDistributedSingleton(beanName)) {
38       ComplexBeanId beanId = new ComplexBeanId(beanName);
39       BeanContainer container = factory.getBeanContainer(beanId);
40       if (container == null) {
41         logger.info(factory.getId() + " distributing new bean " + beanName);
42         factory.putBeanContainer(beanId, new BeanContainer(bean, true));
43       } else {
44         logger.info(factory.getId() + " initializing existing bean " + beanName);
45         factory.initializeBean(beanId, bean, container);
46         container.setInitialized(true);
47         return container.getBean();
48       }
49     }
50     return bean;
51   }
52
53   public Object JavaDoc postProcessBeforeInstantiation(Class JavaDoc beanClass, String JavaDoc beanName) throws BeansException {
54     logger.info(factory.getId() + " post processing before instantiation " + isDistributed(beanName));
55     return null;
56   }
57
58   public boolean postProcessAfterInstantiation(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
59     logger.info(factory.getId() + " post processing after instantiation " + isDistributed(beanName));
60     return true;
61   }
62
63   public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor JavaDoc[] pds, Object JavaDoc bean,
64                                                   String JavaDoc beanName) throws BeansException {
65     logger.info(factory.getId() + " post processing property values " + isDistributed(beanName));
66     return pvs;
67   }
68
69   private String JavaDoc isDistributed(String JavaDoc beanName) {
70     return (factory.isDistributedSingleton(beanName) ? "distributed" : "local") + " bean " + beanName;
71   }
72
73 }
74
Popular Tags