1 4 package com.tcspring; 5 6 import org.springframework.beans.factory.config.Scope; 7 import org.springframework.beans.factory.support.AbstractBeanFactory; 8 9 import com.tc.aspectwerkz.joinpoint.StaticJoinPoint; 10 11 14 public class ScopeProtocol { 15 16 private final ThreadLocal scopedBeanId = new ThreadLocal (); 17 18 21 public void setDistributableBeanFactory(String scopeName, Scope scope, AbstractBeanFactory beanFactory) { 22 if(beanFactory instanceof DistributableBeanFactory) { 23 if(scope instanceof DistributableBeanFactoryAware) { 24 ((DistributableBeanFactoryAware) scope).setBeanFactory((DistributableBeanFactory) beanFactory); 25 } 26 } 27 } 28 29 32 public Object virtualizeScopedBean(StaticJoinPoint jp, Scope s, String beanName) throws Throwable { 33 if(s instanceof DistributableBeanFactoryAware) { 34 DistributableBeanFactoryAware scope = (DistributableBeanFactoryAware) s; 35 36 DistributableBeanFactory factory = scope.getBeanFactory(); 37 if(factory!=null && factory.isDistributedScoped(beanName)) { 38 ComplexBeanId beanId = new ComplexBeanId(s.getConversationId(), beanName); 39 40 BeanContainer container = factory.getBeanContainer(beanId); 41 42 if(container!=null && container.isInitialized()) { 43 return container.getBean(); 44 } 45 46 if(container==null) { 47 container = new BeanContainer(null, false); 48 factory.putBeanContainer(beanId, container); 49 } 50 51 Object bean = null; 52 scopedBeanId.set(beanId); 53 try { 54 bean = jp.proceed(); 55 } finally { 56 scopedBeanId.set(null); 57 } 58 59 if(container.getBean()==null) { 60 container.setBean(bean); 61 } else { 62 factory.initializeBean(beanId, bean, container); 63 } 64 65 if(container.getDestructionCallBack()==null) { 66 ScopedBeanDestructionCallBack destructionCallBack = new ScopedBeanDestructionCallBack(beanId, factory, null); 67 s.registerDestructionCallback(beanName, destructionCallBack); 68 } 69 70 container.setInitialized(true); 71 72 return container.getBean(); 73 } 74 } 75 76 return jp.proceed(); 77 } 78 79 82 public Object wrapDestructionCallback(StaticJoinPoint jp, String beanName, Runnable callback, Scope scope) throws Throwable { 83 if (!(callback instanceof ScopedBeanDestructionCallBack) && 84 scope instanceof DistributableBeanFactoryAware) { 85 ComplexBeanId beanId = new ComplexBeanId(scope.getConversationId(), beanName); 86 87 DistributableBeanFactoryAware distributableScope = (DistributableBeanFactoryAware) scope; 88 89 DistributableBeanFactory factory = distributableScope.getBeanFactory(); 90 BeanContainer container = factory.getBeanContainer(beanId); 91 92 ScopedBeanDestructionCallBack destructionCallBack = container.getDestructionCallBack(); 93 if(destructionCallBack==null) { 94 destructionCallBack = new ScopedBeanDestructionCallBack(beanId, factory, callback); 95 container.setDestructionCallBack(destructionCallBack); 96 } else { 97 if(destructionCallBack.getBeanFactory()==null) { 98 destructionCallBack.setBeanFactory(factory); 99 } 100 if(destructionCallBack.getCallback()==null) { 101 destructionCallBack.setCallback(callback); 102 } 103 } 104 105 scope.registerDestructionCallback(beanName, destructionCallBack); 106 return null; 107 } 108 return jp.proceed(); 109 } 110 111 112 public Object suspendRequestAttributeGet(StaticJoinPoint jp, Scope s, String beanName) throws Throwable { 113 return isInRehydration(s, beanName) ? null : jp.proceed(); 114 } 115 116 public Object suspendRequestAttributeSet(StaticJoinPoint jp, Scope s, String beanName) throws Throwable { 117 return isInRehydration(s, beanName) ? null : jp.proceed(); 118 } 119 120 private boolean isInRehydration(Scope s, String beanName) { 121 if (s instanceof DistributableBeanFactoryAware) { 122 DistributableBeanFactory factory = ((DistributableBeanFactoryAware) s).getBeanFactory(); 123 ComplexBeanId beanId = (ComplexBeanId) scopedBeanId.get(); 124 BeanContainer container = factory.getBeanContainer(beanId); 125 if(container!=null && !container.isInitialized()) { 126 return true; 127 } 128 } 129 return false; 130 } 131 132 133 public interface DistributableBeanFactoryAware { 134 void setBeanFactory(DistributableBeanFactory beanFactory); 135 DistributableBeanFactory getBeanFactory(); 136 } 137 138 139 public static class DistributableBeanFactoryAwareMixin implements DistributableBeanFactoryAware { 140 private DistributableBeanFactory beanFactory; 141 142 public DistributableBeanFactory getBeanFactory() { 143 return beanFactory; 144 } 145 146 public void setBeanFactory(DistributableBeanFactory beanFactory) { 147 this.beanFactory = beanFactory; 148 } 149 150 } 151 152 } 153 154 | Popular Tags |