KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > Ejb3Deployment


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;
23
24 import javassist.bytecode.ClassFile;
25 import org.hibernate.cfg.EJB3DTDEntityResolver;
26 import org.hibernate.ejb.packaging.PersistenceMetadata;
27 import org.hibernate.ejb.packaging.PersistenceXmlLoader;
28 import org.jboss.ejb3.enc.EjbModuleEjbResolver;
29 import org.jboss.ejb3.enc.EjbModulePersistenceUnitResolver;
30 import org.jboss.ejb3.entity.PersistenceUnitDeployment;
31 import org.jboss.logging.Logger;
32 import org.jboss.util.file.ArchiveBrowser;
33 import org.jboss.util.file.ClassFileFilter;
34 import org.jboss.virtual.VirtualFile;
35 import org.jboss.virtual.VFS;
36
37 import javax.management.MBeanServer JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40 import javax.naming.NameNotFoundException JavaDoc;
41 import javax.naming.NamingException JavaDoc;
42 import javax.persistence.Entity;
43 import javax.security.jacc.PolicyConfiguration JavaDoc;
44 import javax.security.jacc.PolicyConfigurationFactory JavaDoc;
45 import java.io.DataInputStream JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.net.URL JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.LinkedHashMap JavaDoc;
52 import java.util.List JavaDoc;
53
54 /**
55  * An EjbModule represents a collection of beans that are deployed as a unit.
56  *
57  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
58  * @version $Revision: 58135 $
59  */

60 public abstract class Ejb3Deployment
61 {
62
63    private static final Logger log = Logger.getLogger(Ejb3Deployment.class);
64    public static final String JavaDoc ACTUAL_ENTITY_MANAGER_FACTORY_CONTEXT = "java:/ActualEntityManagerFactories";
65    public static final String JavaDoc MANAGED_ENTITY_FACTORY_CONTEXT = "java:/managedEntityFactories";
66
67    protected DeploymentUnit di;
68
69    protected LinkedHashMap JavaDoc ejbContainers = new LinkedHashMap JavaDoc();
70
71    protected boolean hasEntities;
72    protected List JavaDoc<String JavaDoc> explicitEntityClasses = new ArrayList JavaDoc<String JavaDoc>();
73
74    protected List JavaDoc<PersistenceUnitDeployment> persistenceUnitDeployments = new ArrayList JavaDoc<PersistenceUnitDeployment>();
75 ;
76
77    protected String JavaDoc defaultSLSBDomain = "Stateless Bean";
78    protected String JavaDoc defaultSFSBDomain = "Stateful Bean";
79    protected String JavaDoc defaultMDBDomain = "Message Driven Bean";
80    protected String JavaDoc defaultConsumerDomain = "Consumer Bean";
81    protected String JavaDoc defaultServiceDomain = "Service Bean";
82    protected InitialContext JavaDoc initialContext;
83
84    protected KernelAbstraction kernelAbstraction;
85
86    // used for @Management interfaces
87
protected MBeanServer JavaDoc mbeanServer;
88
89    protected DeploymentScope deploymentScope;
90    protected EjbModuleEjbResolver ejbRefResolver;
91    protected EjbModulePersistenceUnitResolver persistenceUnitResolver;
92
93    //The JACC PolicyConfiguration
94
PolicyConfiguration JavaDoc pc;
95
96    public Ejb3Deployment(DeploymentUnit di, DeploymentScope deploymentScope)
97    {
98       this.di = di;
99       this.deploymentScope = deploymentScope;
100       try
101       {
102          initialContext = EJB3Util.getInitialContext(di.getJndiProperties());
103       }
104       catch (NamingException JavaDoc e)
105       {
106          throw new RuntimeException JavaDoc(e);
107       }
108       ejbRefResolver = new EjbModuleEjbResolver(deploymentScope, di.getShortName(), ejbContainers, this);
109       persistenceUnitResolver = new EjbModulePersistenceUnitResolver(persistenceUnitDeployments, deploymentScope, ejbContainers);
110    }
111
112    public DeploymentScope getEar()
113    {
114       return deploymentScope;
115    }
116
117    public KernelAbstraction getKernelAbstraction()
118    {
119       return kernelAbstraction;
120    }
121
122    public MBeanServer JavaDoc getMbeanServer()
123    {
124       return mbeanServer;
125    }
126
127    public void setMbeanServer(MBeanServer JavaDoc mbeanServer)
128    {
129       this.mbeanServer = mbeanServer;
130    }
131
132    public DeploymentUnit getDeploymentUnit()
133    {
134       return di;
135    }
136
137    public String JavaDoc getDefaultSLSBDomain()
138    {
139       return defaultSLSBDomain;
140    }
141
142
143    /**
144     * Returns a partial MBean attribute name of the form
145     * ",ear=foo.ear,jar=foo.jar"
146     *
147     * @return
148     */

149    public String JavaDoc getScopeKernelName()
150    {
151       String JavaDoc scopedKernelName = "";
152       if (deploymentScope != null) scopedKernelName += ",ear=" + deploymentScope.getShortName();
153       scopedKernelName += ",jar=" + di.getShortName();
154       return scopedKernelName;
155    }
156
157    /**
158     * The default AOP domain for stateless session beans
159     *
160     * @param defaultSLSBDomain
161     */

162    public void setDefaultSLSBDomain(String JavaDoc defaultSLSBDomain)
163    {
164       this.defaultSLSBDomain = defaultSLSBDomain;
165    }
166
167    public String JavaDoc getDefaultSFSBDomain()
168    {
169       return defaultSFSBDomain;
170    }
171
172    public String JavaDoc getDefaultConsumerDomain()
173    {
174       return defaultConsumerDomain;
175    }
176
177    /**
178     * The default stateful session bean aspect domain
179     *
180     * @param defaultSFSBDomain
181     */

182    public void setDefaultSFSBDomain(String JavaDoc defaultSFSBDomain)
183    {
184       this.defaultSFSBDomain = defaultSFSBDomain;
185    }
186
187    public String JavaDoc getDefaultMDBDomain()
188    {
189       return defaultMDBDomain;
190    }
191
192    /**
193     * The default AOP domain for message driven beans.
194     *
195     * @param defaultMDBDomain
196     */

197    public void setDefaultMDBDomain(String JavaDoc defaultMDBDomain)
198    {
199       this.defaultMDBDomain = defaultMDBDomain;
200    }
201
202    public String JavaDoc getDefaultServiceDomain()
203    {
204       return defaultServiceDomain;
205    }
206
207    /**
208     * default AOP domain for service beans.
209     *
210     * @param defaultServiceDomain
211     */

212    public void setDefaultServiceDomain(String JavaDoc defaultServiceDomain)
213    {
214       this.defaultServiceDomain = defaultServiceDomain;
215    }
216
217    protected String JavaDoc getJaccContextId()
218    {
219       return di.getShortName();
220    }
221
222    public Container getContainer(ObjectName JavaDoc name)
223    {
224       return (Container) ejbContainers.get(name);
225    }
226
227    public java.util.Map JavaDoc getEjbContainers()
228    {
229       return ejbContainers;
230    }
231
232
233    public PersistenceUnitDeployment getPersistenceUnitDeployment(String JavaDoc unitName) throws NameNotFoundException JavaDoc
234    {
235       return persistenceUnitResolver.getPersistenceUnitDeployment(unitName);
236    }
237
238    public PersistenceUnitDeployment getPersistenceUnitDeploymentInternal(String JavaDoc unitName)
239    {
240       return persistenceUnitResolver.getPersistenceUnitDeploymentInternal(unitName);
241    }
242
243    public List JavaDoc<PersistenceUnitDeployment> getPersistenceUnitDeployments()
244    {
245       return persistenceUnitDeployments;
246    }
247
248
249    public EJBContainer getEjbContainer(String JavaDoc ejbLink, Class JavaDoc businessIntf)
250    {
251       return ejbRefResolver.getEjbContainer(ejbLink, businessIntf);
252    }
253
254    public String JavaDoc getEjbJndiName(String JavaDoc ejbLink, Class JavaDoc businessIntf)
255    {
256       return ejbRefResolver.getEjbJndiName(ejbLink, businessIntf);
257    }
258
259    public EJBContainer getEjbContainer(Ejb3Deployment deployment, Class JavaDoc businessIntf) throws NameNotFoundException JavaDoc
260    {
261       return ejbRefResolver.getEjbContainer(deployment, businessIntf);
262    }
263
264    public EJBContainer getEjbContainer(Class JavaDoc businessIntf) throws NameNotFoundException JavaDoc
265    {
266       return ejbRefResolver.getEjbContainer(businessIntf);
267    }
268
269    public String JavaDoc getEjbJndiName(Class JavaDoc businessIntf) throws NameNotFoundException JavaDoc
270    {
271       return ejbRefResolver.getEjbJndiName(businessIntf);
272    }
273
274    protected void processEJBContainerMetadata(Container container)
275            throws Exception JavaDoc
276    {
277       ObjectName JavaDoc on = container.getObjectName();
278       ejbContainers.put(on, container);
279       DependencyPolicy policy = createDependencyPolicy();
280       container.processMetadata(policy);
281
282    }
283
284    protected void registerEJBContainer(Container container)
285            throws Exception JavaDoc
286    {
287       ObjectName JavaDoc on = container.getObjectName();
288       kernelAbstraction.install(on.getCanonicalName(), container.getDependencyPolicy(), container);
289       log.debug("Bound ejb3 container " + on);
290    }
291
292    protected abstract PolicyConfiguration JavaDoc createPolicyConfiguration() throws Exception JavaDoc;
293
294    protected abstract void putJaccInService(PolicyConfiguration JavaDoc pc, DeploymentUnit unit);
295
296
297    /**
298     * Create all EJB containers and Persistence Units
299     * The only things that should be initialized is metadata that does not need access to any
300     * other deployment. This is because we want the entire EAR to be initialized so that we do not
301     * have to guess on dependencies MBean names. This is because of the silly scoping rules for persistence units
302     * and EJBs.
303     *
304     * @throws Exception
305     */

306    public void create() throws Exception JavaDoc
307    {
308       long start = System.currentTimeMillis();
309
310       pc = createPolicyConfiguration();
311
312       deploy();
313
314       initializePersistenceUnits();
315
316       log.debug("EJB3 deployment time took: "
317               + (System.currentTimeMillis() - start));
318    }
319
320    public void start() throws Exception JavaDoc
321    {
322       try
323       {
324          startPersistenceUnits();
325
326          for (Object JavaDoc o : ejbContainers.values())
327          {
328             Container con = (Container) o;
329             processEJBContainerMetadata(con);
330          }
331
332          for (Object JavaDoc o : ejbContainers.values())
333          {
334             Container con = (Container) o;
335             registerEJBContainer(con);
336          }
337
338          putJaccInService(pc, di);
339       }
340       catch (Exception JavaDoc ex)
341       {
342          try
343          {
344             stop();
345             destroy();
346          }
347          catch (Exception JavaDoc ignored)
348          {
349          }
350          throw ex;
351       }
352    }
353
354    protected void deploy() throws Exception JavaDoc
355    {
356       Ejb3HandlerFactory factory = Ejb3HandlerFactory.getInstance(this);
357       if (di.getUrl() != null) deployUrl(factory);
358
359       if (di.getClasses() != null)
360       {
361          for (Class JavaDoc explicit : di.getClasses())
362          {
363             if (explicit.isAnnotationPresent(Entity.class))
364             {
365                continue;
366             }
367             String JavaDoc name = explicit.getName().replace('.', '/') + ".class";
368             InputStream JavaDoc stream = explicit.getClassLoader().getResourceAsStream(name);
369             deployElement(stream, factory, initialContext);
370          }
371       }
372    }
373
374    protected void deployUrl(Ejb3HandlerFactory factory)
375            throws Exception JavaDoc
376    {
377       InitialContext JavaDoc ctx = initialContext;
378       // need to look into every entry in the archive to see if anybody has tags
379
// defined.
380
List JavaDoc<VirtualFile> classes = di.getResources(new org.jboss.ejb3.ClassFileFilter());
381       for (VirtualFile classFile : classes)
382       {
383          InputStream JavaDoc stream = classFile.openStream();
384          deployElement(stream, factory, ctx);
385       }
386    }
387
388    protected void deployElement(InputStream JavaDoc stream, Ejb3HandlerFactory factory, InitialContext JavaDoc ctx)
389            throws Exception JavaDoc
390    {
391       DataInputStream JavaDoc dstream = new DataInputStream JavaDoc(stream);
392       ClassFile cf = null;
393       try
394       {
395          cf = new ClassFile(dstream);
396       }
397       finally
398       {
399          dstream.close();
400          stream.close();
401       }
402
403       deployElement(factory, cf, ctx);
404
405    }
406
407    protected void deployElement(Ejb3HandlerFactory factory, ClassFile cf, InitialContext JavaDoc ctx)
408            throws Exception JavaDoc
409    {
410       Ejb3Handler handler = factory.createHandler(cf);
411       handler.setCtxProperties(di.getJndiProperties());
412
413       if (handler.isEjb() || handler.isJBossBeanType())
414       {
415          List JavaDoc<Container> containers = handler.getContainers(cf, this);
416          for (Container con : containers)
417          {
418             // EJBContainer has finished with all metadata initialization from XML files and such.
419
// this is really a hook to do some processing after XML has been set up and before
420
// and processing of dependencies and such.
421
((EJBContainer) con).instantiated();
422             this.ejbContainers.put(con.getObjectName(), con);
423             Ejb3Registry.register(con);
424          }
425       }
426    }
427
428    protected void initializePersistenceUnits()
429            throws Exception JavaDoc
430    {
431       URL JavaDoc persistenceXmlUrl = null;
432       persistenceXmlUrl = di.getPersistenceXml();
433  
434       hasEntities = persistenceXmlUrl != null;
435
436       if (!hasEntities) return;
437
438       if (di.getClasses() != null)
439       {
440          for (Class JavaDoc explicit : di.getClasses())
441          {
442             if (explicit.isAnnotationPresent(Entity.class))
443             {
444                explicitEntityClasses.add(explicit.getName());
445             }
446          }
447       }
448
449       // scope the unitName if this is an ejb archive
450
List JavaDoc<PersistenceMetadata> persistenceMetadata = PersistenceXmlLoader.deploy(persistenceXmlUrl, new HashMap JavaDoc(), new EJB3DTDEntityResolver());
451       for (PersistenceMetadata metadata : persistenceMetadata)
452       {
453          String JavaDoc earShortName = deploymentScope == null ? null : deploymentScope.getShortName();
454          boolean isScoped = ejbContainers.size() > 0;
455          PersistenceUnitDeployment deployment = new PersistenceUnitDeployment(initialContext, this, explicitEntityClasses, persistenceXmlUrl, metadata, earShortName, di.getShortName(), isScoped);
456          PersistenceUnitRegistry.register(deployment);
457          persistenceUnitDeployments.add(deployment);
458       }
459    }
460
461    public abstract DependencyPolicy createDependencyPolicy();
462
463    protected void startPersistenceUnits()
464    {
465       if (persistenceUnitDeployments == null) return;
466
467       for (PersistenceUnitDeployment entityDeployment : persistenceUnitDeployments)
468       {
469          if (entityDeployment != null)
470          {
471             DependencyPolicy policy = createDependencyPolicy();
472             entityDeployment.addDependencies(policy);
473             kernelAbstraction.install(entityDeployment.getKernelName(), policy, entityDeployment);
474          }
475       }
476    }
477
478    protected void stopPersistenceUnits()
479    {
480       if (persistenceUnitDeployments == null) return;
481
482       for (PersistenceUnitDeployment entityDeployment : persistenceUnitDeployments)
483       {
484          try
485          {
486             PersistenceUnitRegistry.unregister(entityDeployment);
487             if (entityDeployment != null)
488             {
489                kernelAbstraction.uninstall(entityDeployment.getKernelName());
490             }
491          }
492          catch (Exception JavaDoc e)
493          {
494             log.debug("error trying to shut down persistence unit", e);
495          }
496       }
497
498    }
499
500    public void stop() throws Exception JavaDoc
501    {
502       for (Object JavaDoc o : ejbContainers.keySet())
503       {
504          try
505          {
506             ObjectName JavaDoc on = (ObjectName JavaDoc) o;
507             kernelAbstraction.uninstall(on.getCanonicalName());
508             Container container = (Container) ejbContainers.get(on);
509             Ejb3Registry.unregister(container);
510          }
511          catch (Exception JavaDoc e)
512          {
513             log.debug("error trying to shut down ejb container", e);
514          }
515       }
516       stopPersistenceUnits();
517    }
518
519    public void destroy() throws Exception JavaDoc
520    {
521       PolicyConfigurationFactory JavaDoc pcFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory();
522       PolicyConfiguration JavaDoc pc = pcFactory.getPolicyConfiguration(getJaccContextId(), true);
523       pc.delete();
524    }
525 }
526
Popular Tags