1 16 17 package org.springframework.ejb.support; 18 19 import javax.ejb.EnterpriseBean ; 20 21 import org.springframework.beans.BeansException; 22 import org.springframework.beans.FatalBeanException; 23 import org.springframework.beans.factory.BeanFactory; 24 import org.springframework.beans.factory.access.BeanFactoryLocator; 25 import org.springframework.beans.factory.access.BeanFactoryReference; 26 import org.springframework.context.access.ContextJndiBeanFactoryLocator; 27 import org.springframework.util.WeakReferenceMonitor; 28 29 53 abstract class AbstractEnterpriseBean implements EnterpriseBean { 54 55 public static final String BEAN_FACTORY_PATH_ENVIRONMENT_KEY = "java:comp/env/ejb/BeanFactoryPath"; 56 57 58 62 private BeanFactoryLocator beanFactoryLocator; 63 64 65 private String beanFactoryLocatorKey; 66 67 68 private BeanFactoryReference beanFactoryReference; 69 70 71 85 public void setBeanFactoryLocator(BeanFactoryLocator beanFactoryLocator) { 86 this.beanFactoryLocator = beanFactoryLocator; 87 } 88 89 98 public void setBeanFactoryLocatorKey(String factoryKey) { 99 this.beanFactoryLocatorKey = factoryKey; 100 } 101 102 108 void loadBeanFactory() throws BeansException { 109 if (this.beanFactoryLocator == null) { 110 this.beanFactoryLocator = new ContextJndiBeanFactoryLocator(); 111 } 112 if (this.beanFactoryLocatorKey == null) { 113 this.beanFactoryLocatorKey = BEAN_FACTORY_PATH_ENVIRONMENT_KEY; 114 } 115 116 this.beanFactoryReference = this.beanFactoryLocator.useBeanFactory(this.beanFactoryLocatorKey); 117 118 WeakReferenceMonitor.monitor(this, new BeanFactoryReferenceReleaseListener(this.beanFactoryReference)); 122 } 123 124 131 void unloadBeanFactory() throws FatalBeanException { 132 if (this.beanFactoryReference != null) { 135 this.beanFactoryReference.release(); 136 this.beanFactoryReference = null; 137 } 138 } 139 140 144 protected BeanFactory getBeanFactory() { 145 return this.beanFactoryReference.getFactory(); 146 } 147 148 155 public void ejbRemove() { 156 onEjbRemove(); 157 unloadBeanFactory(); 158 } 159 160 167 protected void onEjbRemove() { 168 } 170 171 172 177 private static class BeanFactoryReferenceReleaseListener implements WeakReferenceMonitor.ReleaseListener { 178 179 private final BeanFactoryReference beanFactoryReference; 180 181 public BeanFactoryReferenceReleaseListener(BeanFactoryReference beanFactoryReference) { 182 this.beanFactoryReference = beanFactoryReference; 183 } 184 185 public void released() { 186 this.beanFactoryReference.release(); 187 } 188 } 189 190 } 191 | Popular Tags |