KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > entity > PersistenceUnitDeployment


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.entity;
23
24 import java.io.File JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Properties JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityManagerFactory;
33 import javax.persistence.spi.PersistenceProvider;
34 import javax.persistence.spi.PersistenceUnitTransactionType;
35 import org.hibernate.ejb.HibernatePersistence;
36 import org.hibernate.ejb.packaging.PersistenceMetadata;
37 import org.jboss.ejb3.DependencyPolicy;
38 import org.jboss.ejb3.DeploymentUnit;
39 import org.jboss.ejb3.Ejb3Deployment;
40 import org.jboss.ejb3.NonSerializableFactory;
41 import org.jboss.logging.Logger;
42
43 /**
44  * Comment
45  *
46  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
47  * @version $Revision: 57946 $
48  */

49 public class PersistenceUnitDeployment
50 {
51    private static final Logger log = Logger.getLogger(PersistenceUnitDeployment.class);
52
53    protected InitialContext JavaDoc initialContext;
54    protected DeploymentUnit di;
55    protected List JavaDoc<String JavaDoc> explicitEntityClasses = new ArrayList JavaDoc<String JavaDoc>();
56    protected ManagedEntityManagerFactory managedFactory;
57    protected EntityManagerFactory actualFactory;
58    protected URL JavaDoc persistenceXmlUrl;
59    protected PersistenceMetadata xml;
60    protected String JavaDoc kernelName;
61    protected Ejb3Deployment deployment;
62    protected boolean scoped;
63
64    public PersistenceUnitDeployment(InitialContext JavaDoc initialContext, Ejb3Deployment deployment, List JavaDoc<String JavaDoc> explicitEntityClasses, URL JavaDoc persistenceXmlUrl, PersistenceMetadata metadata, String JavaDoc ear, String JavaDoc jar, boolean isScoped)
65    {
66       this.scoped = isScoped;
67       this.deployment = deployment;
68       this.initialContext = initialContext;
69       this.di = deployment.getDeploymentUnit();
70       this.explicitEntityClasses = explicitEntityClasses;
71       this.persistenceXmlUrl = persistenceXmlUrl;
72       xml = metadata;
73       kernelName = "persistence.units:";
74       String JavaDoc name = getEntityManagerName();
75       if (name == null || name.equals(""))
76          throw new RuntimeException JavaDoc("Empty string is not allowed for a persistence unit. Fix your persistence.xml file");
77       if (ear != null)
78       {
79          kernelName += "ear=" + ear;
80          if (!ear.endsWith(".ear")) kernelName += ".ear";
81          kernelName += ",";
82       }
83       if (isScoped)
84       {
85          kernelName += "jar=" + jar;
86          if (!jar.endsWith(".jar")) kernelName += ".jar";
87          kernelName += ",";
88       }
89       kernelName += "unitName=" + name;
90    }
91
92    public static String JavaDoc getDefaultKernelName(String JavaDoc unitName)
93    {
94       int hashIndex = unitName.indexOf('#');
95       if (hashIndex != -1)
96       {
97          String JavaDoc relativePath = unitName.substring(3, hashIndex);
98          String JavaDoc name = unitName.substring(hashIndex + 1);
99          return "persistence.units:jar=" + relativePath + "," + "unitName=" + name;
100       }
101       return "persistence.units:unitName=" + unitName;
102    }
103
104    public boolean isScoped()
105    {
106       return scoped;
107    }
108
109    public Ejb3Deployment getDeployment()
110    {
111       return deployment;
112    }
113
114    protected String JavaDoc getJaccContextId()
115    {
116       return di.getShortName();
117    }
118
119    public EntityManagerFactory getActualFactory()
120    {
121       return actualFactory;
122    }
123
124    public PersistenceMetadata getXml()
125    {
126       return xml;
127    }
128
129    public String JavaDoc getKernelName()
130    {
131       return kernelName;
132    }
133
134    public String JavaDoc getEntityManagerName()
135    {
136       return xml.getName() == null ? "" : xml.getName();
137    }
138
139    public ManagedEntityManagerFactory getManagedFactory()
140    {
141       return managedFactory;
142    }
143    
144    public void addDependencies(DependencyPolicy policy)
145    {
146       Properties JavaDoc props = getXml().getProps();
147       if (!props.containsKey("jboss.no.implicit.datasource.dependency"))
148       {
149          if (getXml().getJtaDatasource() != null)
150          {
151             String JavaDoc ds = getXml().getJtaDatasource();
152             policy.addDatasource(ds);
153          }
154          if (getXml().getNonJtaDatasource() != null)
155          {
156             String JavaDoc ds = getXml().getNonJtaDatasource();
157             policy.addDatasource(ds);
158          }
159       }
160       for (Object JavaDoc prop : props.keySet())
161       {
162          String JavaDoc property = (String JavaDoc)prop;
163          if (property.startsWith("jboss.depends"))
164          {
165             policy.addDependency(props.getProperty(property));
166          }
167       }
168
169    }
170
171
172    public void start()
173            throws Exception JavaDoc
174    {
175       Properties JavaDoc props = new Properties JavaDoc();
176       props.putAll(di.getDefaultPersistenceProperties());
177       props.put(HibernatePersistence.JACC_CONTEXT_ID, getJaccContextId());
178
179       PersistenceUnitInfoImpl pi = new PersistenceUnitInfoImpl();
180       pi.setClassLoader(di.getClassLoader());
181
182       ArrayList JavaDoc<URL JavaDoc> jarFiles = new ArrayList JavaDoc<URL JavaDoc>();
183       pi.setJarFiles(jarFiles);
184       pi.setPersistenceProviderClassName(HibernatePersistence.class.getName());
185       log.debug("Found persistence.xml file in EJB3 jar");
186       props.putAll(xml.getProps());
187       pi.setManagedClassnames(xml.getClasses());
188       pi.setPersistenceUnitName(xml.getName());
189       pi.setMappingFileNames(xml.getMappingFiles());
190       pi.setExcludeUnlistedClasses(xml.getExcludeUnlistedClasses());
191       pi.setPersistenceUnitRootUrl(di.getUrl());
192       PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.JTA;
193       if ("RESOURCE_LOCAL".equals(xml.getTransactionType()))
194          transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
195       pi.setTransactionType(transactionType);
196
197       for (String JavaDoc jar : xml.getJarFiles())
198       {
199          jarFiles.add(deployment.getDeploymentUnit().getRelativeURL(jar));
200       }
201
202
203       if (xml.getProvider() != null) pi.setPersistenceProviderClassName(xml.getProvider());
204       if (explicitEntityClasses.size() > 0)
205       {
206          List JavaDoc<String JavaDoc> classes = pi.getManagedClassNames();
207          if (classes == null) classes = explicitEntityClasses;
208          else classes.addAll(explicitEntityClasses);
209          pi.setManagedClassnames(classes);
210       }
211       if (xml.getJtaDatasource() != null)
212       {
213          pi.setJtaDataSource((javax.sql.DataSource JavaDoc) initialContext.lookup(xml.getJtaDatasource()));
214       }
215       else if (transactionType == PersistenceUnitTransactionType.JTA)
216       {
217          throw new RuntimeException JavaDoc("You have not defined a jta-data-source for a JTA enabled persistence context named: " + xml.getName());
218       }
219       if (xml.getNonJtaDatasource() != null)
220       {
221          pi.setNonJtaDataSource((javax.sql.DataSource JavaDoc) initialContext.lookup(xml.getNonJtaDatasource()));
222       }
223       else if (transactionType == PersistenceUnitTransactionType.RESOURCE_LOCAL)
224       {
225          throw new RuntimeException JavaDoc("You have not defined a non-jta-data-source for a RESOURCE_LOCAL enabled persistence context named: " + xml.getName());
226       }
227       pi.setProperties(props);
228
229       if (pi.getPersistenceUnitName() == null)
230       {
231          throw new RuntimeException JavaDoc("you must specify a name in persistence.xml");
232       }
233
234       Class JavaDoc providerClass = Thread.currentThread().getContextClassLoader().loadClass(pi.getPersistenceProviderClassName());
235
236       PersistenceProvider pp = (PersistenceProvider) providerClass.newInstance();
237       actualFactory = pp.createContainerEntityManagerFactory(pi, null);
238
239       managedFactory = new ManagedEntityManagerFactory(actualFactory, kernelName);
240
241       String JavaDoc entityManagerJndiName = (String JavaDoc) props.get("jboss.entity.manager.jndi.name");
242       if (entityManagerJndiName != null)
243       {
244          EntityManager injectedManager = new TransactionScopedEntityManager(managedFactory);
245          NonSerializableFactory.rebind(initialContext, entityManagerJndiName, injectedManager);
246       }
247       String JavaDoc entityManagerFactoryJndiName = (String JavaDoc) props.get("jboss.entity.manager.factory.jndi.name");
248       if (entityManagerFactoryJndiName != null)
249       {
250          EntityManagerFactory injectedFactory = new InjectedEntityManagerFactory(managedFactory);
251          NonSerializableFactory.rebind(initialContext, entityManagerFactoryJndiName, injectedFactory);
252       }
253    }
254
255    public void stop() throws Exception JavaDoc
256    {
257       String JavaDoc entityManagerJndiName = (String JavaDoc) xml.getProps().get("jboss.entity.manager.jndi.name");
258       if (entityManagerJndiName != null)
259       {
260          NonSerializableFactory.unbind(initialContext, entityManagerJndiName);
261       }
262       String JavaDoc entityManagerFactoryJndiName = (String JavaDoc) xml.getProps().get("jboss.entity.manager.factory.jndi.name");
263       if (entityManagerFactoryJndiName != null)
264       {
265          NonSerializableFactory.unbind(initialContext, entityManagerFactoryJndiName);
266       }
267       managedFactory.destroy();
268    }
269
270
271 }
272
Popular Tags