KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > spring > factory > AbstractBeanFactoryLoader


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.spring.factory;
24
25 import java.net.URL JavaDoc;
26 import java.net.URLClassLoader JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.TreeMap JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
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 /**
44  * @author <a HREF="mailto:ales.justin@genera-lynx.com">Ales Justin</a>
45  */

46 public abstract class AbstractBeanFactoryLoader implements BeanFactoryLoader
47 {
48
49    private final Logger log = Logger.getLogger(getClass());
50
51    protected Map JavaDoc beanFactoryNamesMap = new TreeMap JavaDoc();
52
53    private URL JavaDoc getDocUrl(DeploymentInfo di) throws DeploymentException
54    {
55       URL JavaDoc docURL = di.localUrl;
56       if (!di.isXML)
57       {
58          URLClassLoader JavaDoc localCL = di.localCl;
59          docURL = localCL.findResource("META-INF/jboss-spring.xml");
60       }
61       // Validate that the descriptor was found
62
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 JavaDoc getDefaultName(DeploymentInfo di)
70    {
71       String JavaDoc 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 JavaDoc docURL = getDocUrl(di);
88       String JavaDoc defaultName = getDefaultName(di);
89       BeanFactory beanFactory = null;
90       ClassLoader JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc docURL = getDocUrl(di);
117          String JavaDoc name = (String JavaDoc) 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 JavaDoc 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    // JNDI stuff
136

137    public static void bind(BeanFactory beanFactory, String JavaDoc name) throws BeansException
138    {
139       InitialContext JavaDoc ctx = null;
140       try
141       {
142          ctx = new InitialContext JavaDoc();
143          NonSerializableFactory.rebind(ctx, name, beanFactory);
144       }
145       catch (NamingException JavaDoc 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 JavaDoc ignored)
158             {
159             }
160          }
161       }
162    }
163
164    public static void unbind(String JavaDoc name) throws BeansException
165    {
166       InitialContext JavaDoc ctx = null;
167       try
168       {
169          ctx = new InitialContext JavaDoc();
170          ctx.unbind(name);
171          NonSerializableFactory.unbind(name);
172       }
173       catch (NamingException JavaDoc 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 JavaDoc ignored)
186             {
187             }
188          }
189       }
190    }
191
192    public BeanFactory lookup(String JavaDoc name) throws Exception JavaDoc
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 JavaDoc getExactBeanFactoryClass();
200
201 }
202
Popular Tags