KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > EntityManagerFactoryWrapper


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 package com.sun.enterprise.util;
24
25 import java.io.Serializable JavaDoc;
26 import java.util.logging.*;
27 import java.util.Set JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.naming.Reference JavaDoc;
31
32 import javax.persistence.*;
33
34 import com.sun.enterprise.deployment.types.EntityManagerFactoryReference;
35 import com.sun.enterprise.deployment.EjbDescriptor;
36 import com.sun.enterprise.deployment.WebBundleDescriptor;
37 import com.sun.enterprise.deployment.ApplicationClientDescriptor;
38 import com.sun.enterprise.deployment.BundleDescriptor;
39 import com.sun.enterprise.deployment.Application;
40
41 import com.sun.enterprise.InvocationManager;
42 import com.sun.enterprise.Switch;
43 import com.sun.enterprise.ComponentInvocation;
44
45 import com.sun.logging.*;
46
47 /**
48  * Wrapper for application references to entity manager factories.
49  * A new instance of this class will be created for each injected
50  * EntityManagerFactory reference or each lookup of an EntityManagerFactory
51  * reference within the component jndi environment.
52  *
53  * @author Kenneth Saks
54  */

55 public class EntityManagerFactoryWrapper implements EntityManagerFactory,
56     Serializable JavaDoc {
57
58     static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
59
60     static private LocalStringManagerImpl localStrings =
61         new LocalStringManagerImpl(EntityManagerFactoryWrapper.class);
62
63     private String JavaDoc unitName;
64     transient private EntityManagerFactory entityManagerFactory;
65
66     public EntityManagerFactoryWrapper(EntityManagerFactoryReference
67                                        referenceDescriptor) {
68
69         unitName = referenceDescriptor.getUnitName();
70
71     }
72
73     private EntityManagerFactory getDelegate() {
74
75         if( entityManagerFactory == null ) {
76         
77             entityManagerFactory = lookupEntityManagerFactory(unitName);
78             
79             if( entityManagerFactory == null ) {
80                 throw new IllegalStateException JavaDoc
81                     ("Unable to retrieve EntityManagerFactory for unitName "
82                      + unitName);
83             }
84
85         }
86
87         return entityManagerFactory;
88     }
89
90     public EntityManager createEntityManager() {
91         return getDelegate().createEntityManager();
92     }
93
94     public EntityManager createEntityManager(Map JavaDoc map) {
95         return getDelegate().createEntityManager(map);
96     }
97
98     public void close() {
99         getDelegate().close();
100     }
101
102     public boolean isOpen() {
103         return getDelegate().isOpen();
104     }
105
106
107     /**
108      * Lookup physical EntityManagerFactory based on current component
109      * invocation.
110      * @param emfUnitName unit name of entity manager factory or null if not
111      * specified.
112      * @return EntityManagerFactory or null if no matching factory could be
113      * found.
114      **/

115     static EntityManagerFactory lookupEntityManagerFactory(String JavaDoc emfUnitName)
116     {
117
118         InvocationManager invMgr = Switch.getSwitch().getInvocationManager();
119         ComponentInvocation inv = invMgr.getCurrentInvocation();
120
121         EntityManagerFactory emf = null;
122
123         if( inv != null ) {
124
125             Object JavaDoc descriptor =
126                 Switch.getSwitch().getDescriptorFor(inv.getContainerContext());
127
128             emf = lookupEntityManagerFactory(inv.getInvocationType(),
129                     emfUnitName, descriptor);
130         }
131         
132         return emf;
133     }
134     
135     public static EntityManagerFactory lookupEntityManagerFactory(int invType,
136             String JavaDoc emfUnitName, Object JavaDoc descriptor) {
137
138         Application app = null;
139         BundleDescriptor module = null;
140
141         EntityManagerFactory emf = null;
142
143         switch (invType) {
144
145         case ComponentInvocation.EJB_INVOCATION:
146
147             EjbDescriptor ejbDesc = (EjbDescriptor) descriptor;
148             module = ejbDesc.getEjbBundleDescriptor();
149             app = module.getApplication();
150
151             break;
152
153         case ComponentInvocation.SERVLET_INVOCATION:
154
155             module = (WebBundleDescriptor) descriptor;
156             app = module.getApplication();
157
158             break;
159
160         case ComponentInvocation.APP_CLIENT_INVOCATION:
161
162             module = (ApplicationClientDescriptor) descriptor;
163             app = module.getApplication();
164
165             break;
166
167         default:
168
169             break;
170         }
171
172         // First check module-level for a match.
173
if (module != null) {
174             if (emfUnitName != null) {
175                 emf = module.getEntityManagerFactory(emfUnitName);
176             } else {
177                 Set JavaDoc<EntityManagerFactory> emFactories = module
178                         .getEntityManagerFactories();
179                 if (emFactories.size() == 1) {
180                     emf = emFactories.iterator().next();
181                 }
182             }
183         }
184
185         // If we're in an .ear and no module-level persistence unit was
186
// found, check for an application-level match.
187
if ((app != null) && (emf == null)) {
188             if (emfUnitName != null) {
189
190                 emf = app.getEntityManagerFactory(emfUnitName, module);
191
192             } else {
193                 Set JavaDoc<EntityManagerFactory> emFactories = app
194                         .getEntityManagerFactories();
195                 if (emFactories.size() == 1) {
196                     emf = emFactories.iterator().next();
197                 }
198             }
199         }
200
201         return emf;
202     }
203
204 }
205
Popular Tags