KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > embedded > EJB3StandaloneBootstrap


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 package org.jboss.ejb3.embedded;
23
24 import java.io.File JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.MBeanServerFactory JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36 import javax.naming.Context JavaDoc;
37
38 import org.jboss.aop.AspectXmlLoader;
39 import org.jboss.ejb3.InitialContextFactory;
40 import org.jboss.ejb3.KernelAbstractionFactory;
41 import org.jboss.dependency.spi.ControllerContext;
42 import org.jboss.kernel.Kernel;
43 import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
44 import org.jboss.kernel.plugins.deployment.xml.BeanXMLDeployer;
45 import org.jboss.kernel.spi.deployment.KernelDeployment;
46 import org.jboss.logging.Logger;
47 import org.jboss.mx.server.ServerConstants;
48 import org.jboss.mx.util.ObjectNameFactory;
49
50 /**
51  * This class is used to bootstrap the ejb3 container.
52  * It find things by ClassLoader.getResource and will load
53  * by default the embedded-jbosss-beans.xml file
54  * and the ejb3-interceptors-aop.xml file.
55  * <p/>
56  * This is usually only used in standalone Java programs or Junit tests.
57  *
58  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
59  * @version $Revision: 55301 $
60  */

61 public class EJB3StandaloneBootstrap
62 {
63    private static final Logger log = Logger.getLogger(EJB3StandaloneBootstrap.class);
64    
65    private final static ObjectName JavaDoc DEFAULT_LOADER_NAME =
66       ObjectNameFactory.create(ServerConstants.DEFAULT_LOADER_NAME);
67
68    public static Kernel kernel;
69    private static MBeanServer JavaDoc mbeanServer;
70    private static EJB3StandaloneDeployer deployer;
71    
72    /** Whether we have warned about the initial context */
73    private static boolean warned = false;
74
75    public static Kernel getKernel()
76    {
77       return kernel;
78    }
79
80    private static ArrayList JavaDoc<KernelDeployment> deployments = new ArrayList JavaDoc<KernelDeployment>();
81    private static ArrayList JavaDoc<URL JavaDoc> aopDeployments = new ArrayList JavaDoc<URL JavaDoc>();
82
83
84    /**
85     * Deploy a JBoss Microcontainer XML file.
86     *
87     * @param resource
88     */

89    public static void deployXmlResource(String JavaDoc resource)
90    {
91       try
92       {
93          BeanXMLDeployer deployer = new BeanXMLDeployer(kernel);
94          URL JavaDoc bootstrap = Thread.currentThread().getContextClassLoader().getResource(resource);
95          if (bootstrap == null)
96             throw new RuntimeException JavaDoc("Cannot find " + resource);
97          KernelDeployment deployment = deployer.deploy(bootstrap);
98          deployments.add(0, deployment);
99          deployer.validate();
100       }
101       catch (Throwable JavaDoc throwable)
102       {
103          throw new RuntimeException JavaDoc(throwable);
104       }
105    }
106    
107    public static Object JavaDoc getDeployment(String JavaDoc name)
108    {
109       Iterator JavaDoc allDeployments = deployments.iterator();
110       while (allDeployments.hasNext())
111       {
112          KernelDeployment deployment = (KernelDeployment)allDeployments.next();
113          Iterator JavaDoc contexts = deployment.getInstalledContexts().iterator();
114          while (contexts.hasNext())
115          {
116             org.jboss.kernel.plugins.dependency.AbstractKernelControllerContext context = (org.jboss.kernel.plugins.dependency.AbstractKernelControllerContext)contexts.next();
117             if (context.getName().equals(name))
118                return context.getTarget();
119          }
120       }
121       
122       return null;
123    }
124
125    public static HashSet JavaDoc ignoredJars = new HashSet JavaDoc();
126
127    static
128    {
129       ignoredJars.add("antlr-2.7.5H3.jar");
130       ignoredJars.add("asm-attrs.jar");
131       ignoredJars.add("asm.jar");
132       ignoredJars.add("cglib-2.1.1.jar");
133       ignoredJars.add("commons-collections.jar");
134       ignoredJars.add("commons-logging-api.jar");
135       ignoredJars.add("commons-logging.jar");
136       ignoredJars.add("concurrent.jar");
137       ignoredJars.add("dom4j.jar");
138       ignoredJars.add("ejb3-persistence.jar");
139       ignoredJars.add("hibernate3.jar");
140       ignoredJars.add("hibernate-annotations.jar");
141       ignoredJars.add("hibernate-entitymanager.jar");
142       ignoredJars.add("hsqldb.jar");
143       ignoredJars.add("javassist.jar");
144       ignoredJars.add("jboss-annotations-ejb3.jar");
145       ignoredJars.add("jboss-aop-jdk50.jar");
146       ignoredJars.add("jboss-aspect-library-jdk50.jar");
147       ignoredJars.add("jboss-common.jar");
148       ignoredJars.add("jboss-common-jdbc-wrapper.jar");
149       ignoredJars.add("jboss-container.jar");
150       ignoredJars.add("jboss-dependency.jar");
151       ignoredJars.add("jboss-ejb3.jar");
152       ignoredJars.add("jboss-ejb3x.jar");
153       ignoredJars.add("jboss-j2ee.jar");
154       ignoredJars.add("jboss-j2se.jar");
155       ignoredJars.add("jboss.jar");
156       ignoredJars.add("jboss-jca.jar");
157       ignoredJars.add("jboss-local-jdbc.jar");
158       ignoredJars.add("jboss-microcontainer.jar");
159       ignoredJars.add("jbossmq.jar");
160       ignoredJars.add("jboss-remoting.jar");
161       ignoredJars.add("jbosssx.jar");
162       ignoredJars.add("jboss-system.jar");
163       ignoredJars.add("jboss-transaction.jar");
164       ignoredJars.add("jboss-xa-jdbc.jar");
165       ignoredJars.add("jnpserver.jar");
166       ignoredJars.add("log4j.jar");
167       ignoredJars.add("resolver.jar");
168       ignoredJars.add("trove.jar");
169       ignoredJars.add("xercesImpl.jar");
170       ignoredJars.add("xml-apis.jar");
171       ignoredJars.add("jboss-ejb3-all.jar");
172       ignoredJars.add("hibernate-all.jar");
173       ignoredJars.add("thirdparty-all.jar");
174    }
175
176
177    public static Hashtable JavaDoc getInitialContextProperties()
178    {
179       Hashtable JavaDoc hash = null;
180       ControllerContext context = kernel.getController().getInstalledContext("InitialContextProperties");
181       if (context != null)
182          hash = (Hashtable JavaDoc) context.getTarget();
183       else if (warned == false)
184       {
185          log.warn("Could not find an configured InitialContextProperties, this is ok if your already have a correct jndi.properties file");
186          warned = true;
187       }
188       return hash;
189    }
190
191
192    /**
193     * Scan java.class.path System property for jars that contain EJB3 files.
194     * <p/>
195     * This is unportable and unreliable. Use with caution.
196     */

197    public static void scanClasspath(String JavaDoc paths)
198    {
199       try
200       {
201          String JavaDoc classpath = System.getProperty("java.class.path");
202          StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(classpath, File.pathSeparator);
203          String JavaDoc[] split = paths.split(",");
204          for (int i = 0; i < split.length; i++)
205          {
206             split[i] = split[i].trim();
207          }
208
209          deployer = createDeployer();
210
211          while (tokenizer.hasMoreTokens())
212          {
213             String JavaDoc path = tokenizer.nextToken().trim();
214             boolean found = false;
215             for (String JavaDoc wantedPath : split)
216             {
217                if (path.endsWith(System.getProperty("file.separator") + wantedPath))
218                {
219                   found = true;
220                   break;
221                }
222             }
223             if (!found) continue;
224             File JavaDoc fp = new File JavaDoc(path);
225             if (ignoredJars.contains(fp.getName())) continue;
226             URL JavaDoc archive = fp.toURL();
227             deployer.getArchives().add(archive);
228          }
229          if (deployer.getArchives().size() == 0)
230          {
231             deployer = null;
232             return;
233          }
234          deployer.create();
235          deployer.start();
236
237       }
238       catch (Exception JavaDoc e)
239       {
240          throw new RuntimeException JavaDoc(e);
241       }
242       if (!KernelErrors.validate(kernel)) throw new RuntimeException JavaDoc("Problems scanning classpath");
243
244    }
245
246    /**
247     * Scan java.class.path System property for jars that contain EJB3 files.
248     * <p/>
249     * This is unportable and unreliable. Use with caution.
250     */

251    public static void scanClasspath()
252    {
253       try
254       {
255          String JavaDoc classpath = System.getProperty("java.class.path");
256          StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(classpath, File.pathSeparator);
257
258          deployer = createDeployer();
259
260          while (tokenizer.hasMoreTokens())
261          {
262             String JavaDoc path = tokenizer.nextToken();
263             File JavaDoc fp = new File JavaDoc(path);
264             if (ignoredJars.contains(fp.getName())) continue;
265             URL JavaDoc archive = fp.toURL();
266             deployer.getArchives().add(archive);
267          }
268          if (deployer.getArchives().size() == 0)
269          {
270             deployer = null;
271             return;
272          }
273          deployer.create();
274          deployer.start();
275
276       }
277       catch (Exception JavaDoc e)
278       {
279          throw new RuntimeException JavaDoc(e);
280       }
281       if (!KernelErrors.validate(kernel)) throw new RuntimeException JavaDoc("Problems scanning classpath");
282
283    }
284
285    /**
286     * Create a deployer that you can start deploying to. You must call create/start and stop/destroy to bootstrap
287     * and shutdown
288     *
289     * @return
290     */

291    public static EJB3StandaloneDeployer createDeployer()
292    {
293       EJB3StandaloneDeployer deployer;
294       deployer = new EJB3StandaloneDeployer();
295       deployer.setJndiProperties(getInitialContextProperties());
296       deployer.setKernel(kernel);
297       deployer.setMbeanServer(mbeanServer);
298       
299       return deployer;
300    }
301
302    public static void shutdown()
303    {
304       try
305       {
306          if (deployer != null)
307          {
308             deployer.stop();
309             deployer.destroy();
310             deployer = null;
311          }
312          BeanXMLDeployer deployer = new BeanXMLDeployer(kernel);
313          for (KernelDeployment deployment : deployments)
314          {
315             deployer.undeploy(deployment);
316          }
317
318          for (URL JavaDoc url : aopDeployments)
319          {
320             try
321             {
322                AspectXmlLoader.undeployXML(url);
323             }
324             catch (Exception JavaDoc e)
325             {
326                log.warn(e);
327             }
328          }
329          kernel = null;
330       }
331       catch (Throwable JavaDoc throwable)
332       {
333          log.warn(throwable);
334       }
335
336    }
337    
338    private static void initInitialContext()
339    {
340       Properties JavaDoc jndiProps = new Properties JavaDoc();
341       jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.LocalOnlyContextFactory");
342       jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
343       InitialContextFactory.setProperties(jndiProps);
344    }
345
346    /**
347     * Bootstrap the EJB3 container by loading up JBoss Microcontainer files.
348     *
349     * @param configPath base path for default resources. null or "" should be good enough.
350     */

351    public static void boot(String JavaDoc configPath)
352    {
353       initInitialContext();
354       
355       String JavaDoc basePath = "";
356       if (configPath != null && !configPath.equals(""))
357       {
358          basePath = configPath;
359          if (!configPath.endsWith("/"))
360          {
361             basePath += "/";
362          }
363       }
364       
365       try
366       {
367          URL JavaDoc bootstrap = Thread.currentThread().getContextClassLoader().getResource(basePath + "embedded-jboss-beans.xml");
368          createKernel();
369          loadMBeanServer();
370          BeanXMLDeployer deployer = new BeanXMLDeployer(kernel);
371          if (bootstrap == null)
372             throw new RuntimeException JavaDoc("Cannot find embedded-jboss-beans.xml");
373          deployments.add(0, deployer.deploy(bootstrap));
374          deployer.validate();
375
376          URL JavaDoc ejb3_interceptors = Thread.currentThread().getContextClassLoader().getResource(basePath + "ejb3-interceptors-aop.xml");
377          if (ejb3_interceptors == null)
378             throw new RuntimeException JavaDoc("Cannot find ejb3-interceptors-aop.xml");
379          AspectXmlLoader.deployXML(ejb3_interceptors);
380          aopDeployments.add(ejb3_interceptors);
381          
382          getDeployment("");
383       }
384       catch (Throwable JavaDoc throwable)
385       {
386          throw new RuntimeException JavaDoc(throwable);
387       }
388    }
389    
390    public static void loadMBeanServer() throws Exception JavaDoc
391    {
392       ControllerContext context = kernel.getController().getInstalledContext("MBeanServer");
393        
394       if (context != null)
395       {
396          mbeanServer = (MBeanServer JavaDoc) context.getTarget();
397       }
398       else
399       {
400          ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
401          if (servers.size() == 0)
402          {
403             mbeanServer = MBeanServerFactory.createMBeanServer();
404             org.jboss.mx.util.MBeanServerLocator.setJBoss(mbeanServer);
405          }
406       }
407    }
408
409
410    public static void createKernel()
411            throws Exception JavaDoc
412    {
413       BasicBootstrap bootstrap1 = new BasicBootstrap();
414       bootstrap1.run();
415       kernel = bootstrap1.getKernel();
416       KernelAbstractionFactory.setKernel(kernel);
417    }
418 }
419
Popular Tags