KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > appclient > 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
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
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 in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22
23 package com.sun.enterprise.appclient;
24
25 import com.sun.enterprise.deployment.*;
26 import com.sun.enterprise.loader.InstrumentableClassLoader;
27 import com.sun.enterprise.server.PersistenceUnitInfoImpl;
28 import com.sun.logging.LogDomains;
29
30 import javax.persistence.EntityManagerFactory;
31 import javax.persistence.spi.PersistenceProvider;
32 import javax.persistence.spi.PersistenceUnitInfo;
33 import java.util.Collection JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * @author Sanjeeb.Sahoo@Sun.COM
41  */

42 public class PersistenceUnitLoaderImpl {
43
44     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.ACC_LOGGER);
45
46     private String JavaDoc applicationLocation;
47
48     private InstrumentableClassLoader classLoader;
49
50     private ApplicationClientDescriptor appClient;
51
52     public PersistenceUnitLoaderImpl(
53             String JavaDoc applicationLocation, InstrumentableClassLoader classLoader,
54             ApplicationClientDescriptor appClient) {
55         this.applicationLocation = applicationLocation;
56         this.classLoader = classLoader;
57         this.appClient = appClient;
58     }
59
60     /**
61      * {@inheritDoc}
62      */

63     public void load() {
64         logger.info("Loading persistence units for application: " +
65                 applicationLocation);
66         for (PersistenceUnitDescriptor pu : findReferencedPUs()) {
67             load(pu);
68         }
69     }
70
71     public void unload() {
72         logger.info("Unloading persistence units for application: " +
73                 applicationLocation);
74         // step #1: unload ear level PUs
75
if (appClient.getApplication() != null) {
76             closeEMFs(appClient.getApplication().getEntityManagerFactories());
77         }
78
79         // step #2: unload appclient-jar level PUs
80
logger.info("Unloading persistence units for appclient module called " +
81                 appClient.getModuleDescriptor().getArchiveUri() +
82                 " in application " + applicationLocation);
83         closeEMFs(appClient.getEntityManagerFactories());
84         logger.info("Finished unloading persistence units for application: " +
85                 applicationLocation);
86     }
87
88     /**
89      * This method is used to find out the precise list of PUs that are
90      * referenced by the appclient.
91      *
92      * @return list of PU that are actually referenced by the appclient.
93      */

94     private List JavaDoc<PersistenceUnitDescriptor> findReferencedPUs() {
95         List JavaDoc<PersistenceUnitDescriptor> result =
96                 new ArrayList JavaDoc<PersistenceUnitDescriptor>();
97
98         for(EntityManagerFactoryReferenceDescriptor emfRef :
99                 appClient.getEntityManagerFactoryReferenceDescriptors()) {
100             final String JavaDoc unitName = emfRef.getUnitName();
101             PersistenceUnitDescriptor pu = appClient.findReferencedPU(unitName);
102             if(pu == null) {
103                 throw new RuntimeException JavaDoc("No suitable persistence unit " +
104                         "called " + unitName + " found in the scope of this " +
105                         "application client.");
106             }
107             if(pu.getTransactionType() == "JTA") {
108                 throw new RuntimeException JavaDoc("Application client is referencing " +
109                         "a persistence unit called " + unitName + ", but that" +
110                         "is of transaction-type JTA. Application client " +
111                         "can only use RESOURCE_LOCAL persistence unit.");
112
113             }
114             if (!result.contains(pu)) {
115                 result.add(pu);
116             }
117         }
118         return result;
119     }
120
121     /**
122      * Loads an individual PersistenceUnitDescriptor and registers the
123      * EntityManagerFactory in appropriate DOL structure.
124      *
125      * @param pud PersistenceUnitDescriptor to be loaded.
126      */

127     private void load(PersistenceUnitDescriptor pud) {
128         logger.info("loading pud " + pud.getAbsolutePuRoot());
129         PersistenceUnitInfo pInfo = new PersistenceUnitInfoImpl(pud,
130                 applicationLocation,
131                 classLoader);
132         logger.info("PersistenceInfo for this pud is :\n" + pInfo);
133         PersistenceProvider provider;
134         try {
135             provider =
136                     PersistenceProvider.class.cast(ClassLoader JavaDoc.class.cast(
137                             classLoader)
138                     .loadClass(pInfo.getPersistenceProviderClassName())
139                     .newInstance());
140         } catch (ClassNotFoundException JavaDoc e) {
141             throw new RuntimeException JavaDoc(e);
142         } catch (InstantiationException JavaDoc e) {
143             throw new RuntimeException JavaDoc(e);
144         } catch (IllegalAccessException JavaDoc e) {
145             throw new RuntimeException JavaDoc(e);
146         }
147         EntityManagerFactory emf = provider.createContainerEntityManagerFactory(
148                 pInfo, null);
149         logger.logp(Level.INFO, "PersistenceUnitLoaderImpl", "load",
150                 "emf = {0}", emf);
151         RootDeploymentDescriptor rootDD = pud.getParent().getParent();
152         if (rootDD.isApplication()) {
153             Application.class.cast(rootDD).addEntityManagerFactory(
154                     pInfo.getPersistenceUnitName(), pud.getPuRoot(), emf);
155         } else {
156             BundleDescriptor.class.cast(rootDD).addEntityManagerFactory(
157                     pInfo.getPersistenceUnitName(), emf);
158         }
159     }
160
161     private void closeEMFs(
162             Collection JavaDoc<EntityManagerFactory> entityManagerFactories) {
163         for (EntityManagerFactory emf : entityManagerFactories) {
164             try {
165                 emf.close();
166             } catch (Exception JavaDoc e) {
167                 logger.log(Level.WARNING, e.getMessage(), e);
168             }
169         }
170     }
171 }
172
Popular Tags