KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.ejb3;
8
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.LinkedHashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Properties JavaDoc;
17 import javax.ejb.Stateful JavaDoc;
18 import javax.ejb.Stateless JavaDoc;
19 import javax.persistence.Entity;
20 import org.jboss.aop.AspectManager;
21 import org.jboss.aop.DomainDefinition;
22 import org.jboss.ejb3.entity.EntityManagerFactoryLoader;
23 import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
24 import org.jboss.ejb3.stateful.StatefulContainer;
25 import org.jboss.ejb3.stateless.StatelessContainer;
26 import org.jboss.logging.Logger;
27 import org.jboss.mx.util.MBeanProxy;
28
29 /**
30  * Comment
31  *
32  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
33  * @version $Revision: 1.3.2.1 $
34  */

35 public class StandaloneDeploymentUnit
36 {
37    protected static final Logger log = Logger
38          .getLogger(StandaloneDeploymentUnit.class);
39
40    protected LinkedHashMap JavaDoc<String JavaDoc, Container> containers = new LinkedHashMap JavaDoc<String JavaDoc, Container>();
41
42    protected List JavaDoc<String JavaDoc> entities = new ArrayList JavaDoc<String JavaDoc>();
43
44    protected List JavaDoc<String JavaDoc> packages = new ArrayList JavaDoc<String JavaDoc>();
45
46    protected List JavaDoc<InputStream JavaDoc> hbmXmlFiles = new ArrayList JavaDoc<InputStream JavaDoc>();
47    
48    protected ManagedEntityManagerFactory factory;
49
50    protected ClassLoader JavaDoc cl;
51
52    protected Properties JavaDoc defaultHibernateProperties;
53
54    protected InputStream JavaDoc hibernateConfigFile;
55
56    protected Properties JavaDoc hibernateProperties;
57
58    public static final String JavaDoc ENTITY_CLASSNAMES = "ENTITIES_CLASSNAMES";
59
60    public StandaloneDeploymentUnit(ClassLoader JavaDoc cl,
61          Properties JavaDoc defaultHibernateProperties)
62    {
63       this.cl = cl;
64       this.defaultHibernateProperties = defaultHibernateProperties;
65    }
66
67    public InputStream JavaDoc getHibernateConfigFile()
68    {
69       return hibernateConfigFile;
70    }
71
72    public void setHibernateConfigFile(InputStream JavaDoc hibernateConfigFile)
73    {
74       this.hibernateConfigFile = hibernateConfigFile;
75    }
76
77    public Properties JavaDoc getHibernateProperties()
78    {
79       return hibernateProperties;
80    }
81
82    public void setHibernateProperties(Properties JavaDoc hibernateProperties)
83    {
84       this.hibernateProperties = hibernateProperties;
85    }
86
87    public void addHtmXmlFile(InputStream JavaDoc stream)
88    {
89       hbmXmlFiles.add(stream);
90    }
91
92    public void addEntity(String JavaDoc entityClass)
93    {
94       entities.add(entityClass);
95    }
96
97    public void addPackage(String JavaDoc pkg)
98    {
99       packages.add(pkg);
100    }
101
102    public void addClass(Class JavaDoc clazz)
103    {
104       if (clazz.isAnnotationPresent(Stateless JavaDoc.class))
105       {
106          Stateless JavaDoc stateless = (Stateless JavaDoc) clazz.getAnnotation(Stateless JavaDoc.class);
107          String JavaDoc ejbName = stateless.name();
108          if (ejbName.equals(""))
109             ejbName = clazz.getName();
110          String JavaDoc containerName = "Stateless Bean";
111          DomainDefinition def = AspectManager.instance().getContainer(
112                containerName);
113          if (def == null)
114             throw new RuntimeException JavaDoc("Domain " + containerName + " not found");
115          StatelessContainer container = new StatelessContainer(clazz
116                .getClassLoader(), clazz.getName(), ejbName, def.getManager());
117          containers.put(ejbName, container);
118       } else if (clazz.isAnnotationPresent(Stateful JavaDoc.class))
119       {
120          Stateful JavaDoc stateless = (Stateful JavaDoc) clazz.getAnnotation(Stateless JavaDoc.class);
121          String JavaDoc ejbName = stateless.name();
122          if (ejbName.equals(""))
123             ejbName = clazz.getName();
124          String JavaDoc containerName = "Stateful Bean";
125          DomainDefinition def = AspectManager.instance().getContainer(
126                containerName);
127          if (def == null)
128             throw new RuntimeException JavaDoc("Domain " + containerName + " not found");
129          StatefulContainer container = new StatefulContainer(clazz
130                .getClassLoader(), clazz.getName(), ejbName, def.getManager());
131          containers.put(ejbName, container);
132       } else if (clazz.isAnnotationPresent(Entity.class))
133       {
134          entities.add(clazz.getName());
135       }
136    }
137
138    public void create() throws Exception JavaDoc
139    {
140       Iterator JavaDoc it = containers.keySet().iterator();
141
142       while (it.hasNext())
143       {
144          String JavaDoc on = (String JavaDoc) it.next();
145          Container con = (Container) containers.get(on);
146          try
147          {
148             con.create();
149          } catch (Exception JavaDoc e)
150          {
151             throw new RuntimeException JavaDoc(e);
152          }
153       }
154    }
155
156    public void start() throws Exception JavaDoc
157    {
158       Iterator JavaDoc it = containers.keySet().iterator();
159       factory = createManagedEntityManagerFactory();
160       while (it.hasNext())
161       {
162          String JavaDoc on = (String JavaDoc) it.next();
163          Container con = (Container) containers.get(on);
164          con.setManagedEntityManagerFactory(factory);
165          try
166          {
167             con.start();
168          } catch (Exception JavaDoc e)
169          {
170             throw new RuntimeException JavaDoc(e);
171          }
172       }
173    }
174 /*
175    protected ManagedEntityManagerFactory createManagedEntityManagerFactory()
176          throws Exception
177    {
178       if (entities.size() == 0)
179          return null;
180       List<Class> classes = new ArrayList<Class>();
181       for (int i = 0; i < entities.size(); i++)
182       {
183          String entity = (String) entities.get(i);
184          try
185          {
186             classes.add(di.ucl.loadClass(entity));
187          } catch (ClassNotFoundException e)
188          {
189             throw new RuntimeException(e); // To change body of catch
190             // statement use Options | File
191             // Templates.
192          }
193       }
194       entities.clear();
195       URL propsUrl = null;
196       propsUrl = di.localCl.getResource("META-INF/persistence.properties");
197       Properties props = null;
198
199       if (propsUrl != null)
200       {
201          log.info("Found persistence.properties file in EJB3 jar");
202          props = new Properties();
203          try
204          {
205             props.load(propsUrl.openStream());
206          } catch (IOException e)
207          {
208             throw new RuntimeException(e); // To change body of catch
209             // statement use Options | File
210             // Templates.
211          }
212       } else
213       {
214          EJB3DeployerMBean deployer = (EJB3DeployerMBean) MBeanProxy.get(
215                EJB3DeployerMBean.class, EJB3DeployerMBean.OBJECT_NAME,
216                getServer());
217
218          props = new Properties();
219          props.putAll(deployer.getDefaultProperties());
220          log.info("EJB3 jar using default persistence.properties: " + props);
221       }
222       return new ManagedEntityManagerFactory(EntityManagerFactoryLoader
223             .loadFactory(hbmXmlFiles, packages, classes, props));
224    } */

225
226    protected ManagedEntityManagerFactory createManagedEntityManagerFactory()
227          throws Exception JavaDoc
228    {
229       if (entities.size() == 0)
230          return null;
231       List JavaDoc<Class JavaDoc> classes = new ArrayList JavaDoc<Class JavaDoc>();
232       for (int i = 0; i < entities.size(); i++)
233       {
234          String JavaDoc entity = (String JavaDoc) entities.get(i);
235          classes.add(cl.loadClass(entity));
236       }
237       entities.clear();
238       if (hibernateConfigFile != null && hibernateProperties != null)
239          throw new RuntimeException JavaDoc(
240                "You cannot have both a hibernate.properties and hibernate.cfg.xml file in META-INF");
241
242       if (hibernateConfigFile != null)
243       {
244          throw new RuntimeException JavaDoc("NOT SUPPORTED");
245       }
246
247       Properties JavaDoc props = null;
248       if (hibernateProperties != null)
249       {
250          log.info("Found hibernate.properties file in EJB3 jar");
251          props = hibernateProperties;
252       } else
253       {
254          props = new Properties JavaDoc();
255          props.putAll(defaultHibernateProperties);
256          log.info("EJB3 jar using default hibernate.properties: " + props);
257       }
258       return new ManagedEntityManagerFactory(EntityManagerFactoryLoader
259             .loadFactory(hbmXmlFiles, packages, classes, props));
260    }
261
262    public void stop() throws Exception JavaDoc
263    {
264       Iterator JavaDoc<String JavaDoc> it = containers.keySet().iterator();
265       while (it.hasNext())
266       {
267          String JavaDoc name = it.next();
268          Container con = containers.get(name);
269          con.stop();
270          if (log.isDebugEnabled())
271          {
272             log.debug("stopService, stopping container: " + con.getEjbName());
273          }
274       }
275       /*
276        * if (factory != null) factory.destroy();
277        */

278    }
279
280    public void destroy() throws Exception JavaDoc
281    {
282       for (InputStream JavaDoc is : hbmXmlFiles)
283       {
284          try
285          {
286             is.close();
287          } catch (IOException JavaDoc ignored)
288          {
289          }
290       }
291       Iterator JavaDoc<String JavaDoc> it = containers.keySet().iterator();
292       while (it.hasNext())
293       {
294          String JavaDoc name = it.next();
295          Container con = containers.get(name);
296          con.destroy();
297          if (log.isDebugEnabled())
298          {
299             log.debug("destroyService, destroying container: "
300                   + con.getEjbName());
301          }
302       }
303    }
304
305 }
306
Popular Tags