KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > PersistenceUnitLoaderImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.enterprise.server;
26
27 import com.sun.enterprise.deployment.*;
28 import com.sun.enterprise.loader.InstrumentableClassLoader;
29 import com.sun.logging.LogDomains;
30
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.spi.PersistenceProvider;
33 import javax.persistence.spi.PersistenceUnitInfo;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Collections JavaDoc;
40
41 /**
42  * {@inheritDoc}
43  *
44  * @author Sanjeeb.Sahoo@Sun.COM
45  */

46 public class PersistenceUnitLoaderImpl implements PersistenceUnitLoader {
47
48     /**
49      * logger to log loader messages
50      */

51     private static Logger JavaDoc logger = LogDomains.getLogger(
52             LogDomains.LOADER_LOGGER);
53
54     private String JavaDoc applicationLocation;
55
56     private InstrumentableClassLoader classLoader;
57
58     private Application application;
59
60     private static Map JavaDoc<String JavaDoc, String JavaDoc> integrationProperties;
61
62     /**
63      * {@inheritDoc}
64      */

65     public void load(ApplicationInfo appInfo) {
66         application = appInfo.getApplication();
67         applicationLocation = appInfo.getApplicationLocation();
68         classLoader = appInfo.getClassLoader();
69         if(logger.isLoggable(Level.FINE)) {
70             logger.fine("Loading persistence units for application: " +
71                     applicationLocation);
72         }
73         // step #1: load ear level PUs
74
for (PersistenceUnitsDescriptor pus :
75                 application.getPersistenceUnitsDescriptors()) {
76             for (PersistenceUnitDescriptor pu : pus.getPersistenceUnitDescriptors()) {
77                 load(pu);
78             }
79         }
80
81         // step #2: load ejb-jar level PUs
82
for (Object JavaDoc o : application.getEjbBundleDescriptors()) {
83             EjbBundleDescriptor bundle = EjbBundleDescriptor.class.cast(o);
84             if(logger.isLoggable(Level.FINE)) {
85                 logger.fine("Loading persistence units for ejb module called " +
86                         bundle.getModuleDescriptor().getArchiveUri() +
87                         " in application " + applicationLocation);
88             }
89             for (PersistenceUnitsDescriptor pus : bundle.getPersistenceUnitsDescriptors()) {
90                 for (PersistenceUnitDescriptor pu : pus.getPersistenceUnitDescriptors()) {
91                     load(pu);
92                 }
93             }
94         }
95
96         // step #3: load war level PUs
97
for (Object JavaDoc o : application.getWebBundleDescriptors()) {
98             WebBundleDescriptor bundle = WebBundleDescriptor.class.cast(o);
99             if(logger.isLoggable(Level.FINE)) {
100                 logger.fine("Loading persistence units for web module called " +
101                         bundle.getModuleDescriptor().getArchiveUri() +
102                         " in application " + applicationLocation);
103             }
104             for (PersistenceUnitsDescriptor pus : bundle.getPersistenceUnitsDescriptors()) {
105                 for (PersistenceUnitDescriptor pu : pus.getPersistenceUnitDescriptors()) {
106                     load(pu);
107                 }
108             }
109         }
110         if(logger.isLoggable(Level.FINE)) {
111             logger.fine("Finished loading persistence units for application: " +
112                     applicationLocation);
113         }
114     }
115
116     /**
117      * {@inheritDoc}
118      */

119     public void unload(ApplicationInfo appInfo) {
120         application = appInfo.getApplication();
121         applicationLocation = appInfo.getApplicationLocation();
122         classLoader = appInfo.getClassLoader();
123         if(logger.isLoggable(Level.FINE)) {
124             logger.fine("Unloading persistence units for application: " +
125                     applicationLocation);
126         }
127         // step #1: unload ear level PUs
128
closeEMFs(application.getEntityManagerFactories());
129
130         // step #2: unload ejb-jar level PUs
131
for (Object JavaDoc o : application.getEjbBundleDescriptors()) {
132             EjbBundleDescriptor bundle = EjbBundleDescriptor.class.cast(o);
133             if(logger.isLoggable(Level.FINE)) {
134                 logger.fine("Unloading persistence units for ejb module called " +
135                         bundle.getModuleDescriptor().getArchiveUri() +
136                         " in application " + applicationLocation);
137             }
138             closeEMFs(bundle.getEntityManagerFactories());
139         }
140
141         // step #3: unload war level PUs
142
for (Object JavaDoc o : application.getWebBundleDescriptors()) {
143             WebBundleDescriptor bundle = WebBundleDescriptor.class.cast(o);
144             if(logger.isLoggable(Level.FINE)) {
145                 logger.fine("Unloading persistence units for web module called " +
146                         bundle.getModuleDescriptor().getArchiveUri() +
147                         " in application " + applicationLocation);
148             }
149             closeEMFs(bundle.getEntityManagerFactories());
150         }
151         if(logger.isLoggable(Level.FINE)) {
152             logger.fine("Finished unloading persistence units for application: " +
153                     applicationLocation);
154         }
155     }
156
157     /**
158      * Loads an individual PersistenceUnitDescriptor and registers the
159      * EntityManagerFactory in appropriate DOL structure.
160      *
161      * @param pud PersistenceUnitDescriptor to be loaded.
162      */

163     private void load(PersistenceUnitDescriptor pud) {
164         if(logger.isLoggable(Level.FINE)) {
165             logger.fine("loading pud " + pud.getAbsolutePuRoot());
166         }
167         PersistenceUnitInfo pInfo = new PersistenceUnitInfoImpl(
168                 pud,
169                 applicationLocation,
170                 classLoader);
171         if(logger.isLoggable(Level.FINE)) {
172             logger.fine("PersistenceInfo for this pud is :\n" + pInfo);
173         }
174         PersistenceProvider provider;
175         try {
176             // See we use application CL as opposed to system CL to load
177
// provider. This allows user to get hold of provider specific
178
// implementation classes in their code. But this also means
179
// provider must not use appserver implementation classes directly
180
// because once we implement isolation in our class loader hierarchy
181
// the only classes available to application class loader would be
182
// our appserver interface classes. By Sahoo
183
provider =
184                     PersistenceProvider.class.cast(
185                     ClassLoader JavaDoc.class.cast(classLoader)
186                     .loadClass(pInfo.getPersistenceProviderClassName())
187                     .newInstance());
188         } catch (ClassNotFoundException JavaDoc e) {
189             throw new RuntimeException JavaDoc(e);
190         } catch (InstantiationException JavaDoc e) {
191             throw new RuntimeException JavaDoc(e);
192         } catch (IllegalAccessException JavaDoc e) {
193             throw new RuntimeException JavaDoc(e);
194         }
195         EntityManagerFactory emf = provider.createContainerEntityManagerFactory(
196                 pInfo, integrationProperties);
197         logger.logp(Level.FINE, "PersistenceUnitLoaderImpl", "load",
198                     "emf = {0}", emf);
199
200         RootDeploymentDescriptor rootDD = pud.getParent().getParent();
201         if (rootDD.isApplication()) {
202             Application.class.cast(rootDD).addEntityManagerFactory(
203                     pInfo.getPersistenceUnitName(), pud.getPuRoot(), emf);
204         } else {
205             BundleDescriptor.class.cast(rootDD).addEntityManagerFactory(
206                     pInfo.getPersistenceUnitName(), emf);
207         }
208     }
209
210     private void closeEMFs(
211             Collection JavaDoc<EntityManagerFactory> entityManagerFactories) {
212         for (EntityManagerFactory emf : entityManagerFactories) {
213             try {
214                 emf.close();
215             } catch (Exception JavaDoc e) {
216                 logger.log(Level.WARNING, e.getMessage(), e);
217             }
218         }
219     }
220
221     static {
222         /*
223          * We set all the provider specific integration level properties here.
224          * It knows about all the integration level properties that
225          * are needed to integrate a provider with our container. When we add
226          * support for other containers, we should modify this code so that user
227          * does not have to specify such properties in their persistence.xml file.
228          * These properties can be overriden by persistence.xml as per
229          * the spec. Before applying default values for properties, this method
230          * first checks if the properties have been set in the system
231          * (typically done using -D option in domain.xml).
232          *
233          */

234         Map JavaDoc<String JavaDoc, String JavaDoc> props = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
235
236         // TopLink specific properties:
237
// See https://glassfish.dev.java.net/issues/show_bug.cgi?id=249
238
final String JavaDoc TOPLINK_SERVER_PLATFORM_CLASS_NAME_PROPERTY =
239                 "toplink.server.platform.class.name"; // NOI18N
240
props.put(TOPLINK_SERVER_PLATFORM_CLASS_NAME_PROPERTY,
241                 System.getProperty(TOPLINK_SERVER_PLATFORM_CLASS_NAME_PROPERTY,
242                         "oracle.toplink.essentials.platform.server.sunas.SunAS9ServerPlatform")); // NOI18N
243

244         // These constants are defined in the entity-persistence module. Redefining them for now.
245
final String JavaDoc TOPLINK_DDL_GENERATION_MODE_PROPERTY =
246                 "toplink.ddl-generation.output-mode"; // NOI18N
247
props.put(TOPLINK_DDL_GENERATION_MODE_PROPERTY,
248                 System.getProperty(TOPLINK_DDL_GENERATION_MODE_PROPERTY,
249                         "none")); // NOI18N
250

251         // Hibernate specific properties:
252
final String JavaDoc HIBERNATE_TRANSACTION_MANAGER_LOOKUP_CLASS_PROPERTY =
253                 "hibernate.transaction.manager_lookup_class"; // NOI18N
254
props.put(HIBERNATE_TRANSACTION_MANAGER_LOOKUP_CLASS_PROPERTY,
255                 System.getProperty(HIBERNATE_TRANSACTION_MANAGER_LOOKUP_CLASS_PROPERTY,
256                         "org.hibernate.transaction.SunONETransactionManagerLookup")); // NOI18N
257

258         // use an unmodifiable map as we pass this to provider and we don't
259
// provider to change this.
260
integrationProperties = Collections.unmodifiableMap(props);
261     }
262
263 }
264
Popular Tags