1 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 23 public class GetBeanProtocol { 24 25 private final transient Log logger = LogFactory.getLog(getClass()); 26 27 private final transient ThreadLocal beanNameCflow = new ThreadLocal () { 28 protected Object initialValue() { 29 return new String [1]; 30 } 31 }; 32 33 34 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 56 public Object beanNameCflow(StaticJoinPoint jp, String beanName, AutowireCapableBeanFactory factory) throws Throwable { 57 String [] beanNameHolder = (String []) beanNameCflow.get(); 58 String 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 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 91 public Object virtualizeSingletonBean(StaticJoinPoint jp, AutowireCapableBeanFactory beanFactory) throws Throwable { 92 Object localBean = jp.proceed(); 93 94 if (beanFactory instanceof DistributableBeanFactory) { 95 DistributableBeanFactory distributableBeanFactory = (DistributableBeanFactory) beanFactory; 96 String beanName = ((String []) 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 123 public void initializeSingletonBean(String 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 localInstance = instanceWrapper.getWrappedInstance(); 132 distributableBeanFactory.initializeBean(beanId, localInstance, container); 133 } 134 } 135 } 136 } 137 138 } 139 140 | Popular Tags |