KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tcspring > GetBeanProtocol


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

4 package com.tcspring;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.springframework.beans.BeanWrapper;
9 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
10 import org.springframework.beans.factory.support.AbstractBeanFactory;
11 import org.springframework.beans.factory.support.RootBeanDefinition;
12
13 import com.tc.aspectwerkz.joinpoint.StaticJoinPoint;
14 import com.tc.object.bytecode.Manager;
15 import com.tc.object.bytecode.ManagerUtil;
16
17
18 /**
19  * Virtualize <code>AbstractBeanFactory.getBean()</code>.
20  *
21  * @author Eugene Kuleshov
22  */

23 public class GetBeanProtocol {
24   
25   private final transient Log logger = LogFactory.getLog(getClass());
26
27   private final transient ThreadLocal JavaDoc beanNameCflow = new ThreadLocal JavaDoc() {
28       protected Object JavaDoc initialValue() {
29         return new String JavaDoc[1];
30       }
31     };
32   
33   
34   /**
35    * Invoked after constructor of the <code>AbstractBeanFactory</code>
36    *
37    * @see org.springframework.beans.factory.support.AbstractBeanFactory#AbstractBeanFactory()
38    *
39    * @deprecated This approach does not work because of Spring's handling of the circular dependencies
40    */

41   public void registerBeanPostProcessor(StaticJoinPoint jp, AbstractBeanFactory factory) {
42     if(factory instanceof DistributableBeanFactory) {
43       factory.addBeanPostProcessor(new DistributableBeanPostProcessor((DistributableBeanFactory) factory));
44     }
45   }
46   
47
48   /**
49    * Captures the name of the bean being created and makes it accessible to virtualizeSingletonBean()
50    * It also maintain the locking for distributed bean initialization.
51    *
52    * Invoked around <code>AbstractAutowireCapableBeanFactory.createBean(String, ..)</code> method.
53    *
54    * @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(String, ...)
55    */

56   public Object JavaDoc beanNameCflow(StaticJoinPoint jp, String JavaDoc beanName, AutowireCapableBeanFactory factory) throws Throwable JavaDoc {
57     String JavaDoc[] beanNameHolder = (String JavaDoc[]) beanNameCflow.get();
58     String JavaDoc previousBeanName = beanNameHolder[0];
59     beanNameHolder[0] = beanName;
60     try {
61       if (factory instanceof DistributableBeanFactory) {
62         DistributableBeanFactory distributableBeanFactory = (DistributableBeanFactory) factory;
63         if (distributableBeanFactory.isDistributedSingleton(beanName)) {
64           logger.info(distributableBeanFactory.getId()+" distributed lock for bean " + beanName);
65           String JavaDoc lockId = "@spring_context_" + ((DistributableBeanFactory) factory).getId() + "_" + beanName;
66           ManagerUtil.beginLock(lockId, Manager.LOCK_TYPE_WRITE);
67           try {
68             return jp.proceed();
69           } finally {
70             ManagerUtil.commitLock(lockId);
71           }
72         }
73       }
74       return jp.proceed();
75       
76     } finally {
77       beanNameHolder[0] = previousBeanName;
78     }
79   }
80
81   /**
82    * Virtualize singleton bean.
83    *
84    * Invoked around call to <code>BeanWrapper.getWrappedInstance()</code> method within
85    * <code>AbstractAutowireCapableBeanFactory.createBean(String, ..)</code> method execution.
86    *
87    * @see GetBeanProtocol#beanNameCflow(StaticJoinPoint, String, AutowireCapableBeanFactory)
88    * @see org.springframework.beans.BeanWrapper#getWrappedInstance()
89    * @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(String, ..)
90    */

91   public Object JavaDoc virtualizeSingletonBean(StaticJoinPoint jp, AutowireCapableBeanFactory beanFactory) throws Throwable JavaDoc {
92     Object JavaDoc localBean = jp.proceed();
93
94     if (beanFactory instanceof DistributableBeanFactory) {
95       DistributableBeanFactory distributableBeanFactory = (DistributableBeanFactory) beanFactory;
96       String JavaDoc beanName = ((String JavaDoc[]) beanNameCflow.get())[0];
97       if (distributableBeanFactory.isDistributedSingleton(beanName)) {
98         ComplexBeanId beanId = new ComplexBeanId(beanName);
99         BeanContainer container = distributableBeanFactory.getBeanContainer(beanId);
100         if (container != null) {
101           logger.info(distributableBeanFactory.getId() + " virtualizing existing bean " + beanName);
102           return container.getBean();
103         }
104         logger.info(distributableBeanFactory.getId() + " virtualizing new bean " + beanName);
105         distributableBeanFactory.putBeanContainer(beanId, new BeanContainer(localBean, true));
106       }
107     }
108     
109     return localBean;
110   }
111   
112   /**
113    * Initialize singleton bean.
114    *
115    * Invoked after call to <code>AbstractAutowireCapableBeanFactory.populateBean(..)</code> method
116    * within execution of <code>AbstractAutowireCapableBeanFactory.createBean(String, ..))</code>
117    *
118    * @see GetBeanProtocol#beanNameCflow(StaticJoinPoint, String, AutowireCapableBeanFactory)
119    * @see GetBeanProtocol#virtualizeSingletonBean(StaticJoinPoint, AutowireCapableBeanFactory)
120    * @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(String, ..)
121    * @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean(..)
122    */

123   public void initializeSingletonBean(String JavaDoc beanName, RootBeanDefinition mergedBeanDefinition,
124       BeanWrapper instanceWrapper, AutowireCapableBeanFactory beanFactory) {
125     if (beanFactory instanceof DistributableBeanFactory) {
126       DistributableBeanFactory distributableBeanFactory = (DistributableBeanFactory) beanFactory;
127       if (distributableBeanFactory.isDistributedSingleton(beanName)) {
128         ComplexBeanId beanId = new ComplexBeanId(beanName);
129         BeanContainer container = distributableBeanFactory.getBeanContainer(beanId);
130         if (container != null && !container.isInitialized()) {
131           Object JavaDoc localInstance = instanceWrapper.getWrappedInstance();
132           distributableBeanFactory.initializeBean(beanId, localInstance, container);
133         }
134       }
135     }
136   }
137   
138 }
139
140
Popular Tags