1 22 23 package org.jboss.spring.factory; 24 25 import java.net.URL ; 26 import java.net.URLClassLoader ; 27 import java.util.Map ; 28 import java.util.TreeMap ; 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingException ; 31 32 import org.jboss.deployment.DeploymentException; 33 import org.jboss.deployment.DeploymentInfo; 34 import org.jboss.logging.Logger; 35 import org.jboss.util.naming.NonSerializableFactory; 36 import org.jboss.util.naming.Util; 37 import org.springframework.beans.BeansException; 38 import org.springframework.beans.FatalBeanException; 39 import org.springframework.beans.factory.BeanFactory; 40 import org.springframework.core.io.Resource; 41 import org.springframework.core.io.UrlResource; 42 43 46 public abstract class AbstractBeanFactoryLoader implements BeanFactoryLoader 47 { 48 49 private final Logger log = Logger.getLogger(getClass()); 50 51 protected Map beanFactoryNamesMap = new TreeMap (); 52 53 private URL getDocUrl(DeploymentInfo di) throws DeploymentException 54 { 55 URL docURL = di.localUrl; 56 if (!di.isXML) 57 { 58 URLClassLoader localCL = di.localCl; 59 docURL = localCL.findResource("META-INF/jboss-spring.xml"); 60 } 61 if (docURL == null) 63 { 64 throw new DeploymentException("Failed to find META-INF/jboss-spring.xml"); 65 } 66 return docURL; 67 } 68 69 private String getDefaultName(DeploymentInfo di) 70 { 71 String shortName = di.shortName; 72 int p = shortName.indexOf(".spring"); 73 if (p > 0) 74 { 75 return shortName.substring(0, p); 76 } 77 p = shortName.indexOf("-spring.xml"); 78 if (p > 0) 79 { 80 return shortName.substring(0, p); 81 } 82 return null; 83 } 84 85 public void create(DeploymentInfo di) throws DeploymentException 86 { 87 URL docURL = getDocUrl(di); 88 String defaultName = getDefaultName(di); 89 BeanFactory beanFactory = null; 90 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 91 try 92 { 93 Thread.currentThread().setContextClassLoader(di.ucl); 94 beanFactory = createBeanFactory(defaultName, new UrlResource(docURL)); 95 } 96 finally 97 { 98 Thread.currentThread().setContextClassLoader(cl); 99 } 100 String name = ((Nameable) beanFactory).getName(); 101 bind(beanFactory, name); 102 log.info("Bean factory [" + name + "] binded to local JNDI."); 103 beanFactoryNamesMap.put(docURL.toString(), name); 104 } 105 106 protected abstract BeanFactory createBeanFactory(String defaultName, Resource resource); 107 108 public void start(DeploymentInfo di) throws DeploymentException 109 { 110 } 111 112 public void stop(DeploymentInfo di) throws DeploymentException 113 { 114 try 115 { 116 URL docURL = getDocUrl(di); 117 String name = (String ) beanFactoryNamesMap.remove(docURL.toString()); 118 BeanFactory beanFactory = lookup(name); 119 doClose(beanFactory); 120 unbind(name); 121 log.info("Bean factory [" + name + "] unbinded from local JNDI."); 122 } 123 catch (Exception e) 124 { 125 throw new DeploymentException(e); 126 } 127 } 128 129 protected abstract void doClose(BeanFactory beanFactory); 130 131 public void destroy(DeploymentInfo di) throws DeploymentException 132 { 133 } 134 135 137 public static void bind(BeanFactory beanFactory, String name) throws BeansException 138 { 139 InitialContext ctx = null; 140 try 141 { 142 ctx = new InitialContext (); 143 NonSerializableFactory.rebind(ctx, name, beanFactory); 144 } 145 catch (NamingException e) 146 { 147 throw new FatalBeanException("Unable to bind BeanFactory into JNDI", e); 148 } 149 finally 150 { 151 if (ctx != null) 152 { 153 try 154 { 155 ctx.close(); 156 } 157 catch (Throwable ignored) 158 { 159 } 160 } 161 } 162 } 163 164 public static void unbind(String name) throws BeansException 165 { 166 InitialContext ctx = null; 167 try 168 { 169 ctx = new InitialContext (); 170 ctx.unbind(name); 171 NonSerializableFactory.unbind(name); 172 } 173 catch (NamingException e) 174 { 175 throw new FatalBeanException("Unable to unbind BeanFactory from JNDI", e); 176 } 177 finally 178 { 179 if (ctx != null) 180 { 181 try 182 { 183 ctx.close(); 184 } 185 catch (Throwable ignored) 186 { 187 } 188 } 189 } 190 } 191 192 public BeanFactory lookup(String name) throws Exception 193 { 194 BeanFactory beanFactory = (BeanFactory) Util.lookup(name, getExactBeanFactoryClass()); 195 log.debug("Found Spring bean factory [" + name + "]: " + beanFactory); 196 return beanFactory; 197 } 198 199 protected abstract Class getExactBeanFactoryClass(); 200 201 } 202 | Popular Tags |