KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > persistence > PersistenceUnitGBean


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.persistence;
19
20 import java.util.List JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import javax.persistence.spi.PersistenceUnitInfo;
27 import javax.persistence.spi.PersistenceUnitTransactionType;
28 import javax.persistence.spi.ClassTransformer;
29 import javax.persistence.spi.PersistenceProvider;
30 import javax.persistence.EntityManagerFactory;
31 import javax.persistence.PersistenceException;
32 import javax.persistence.EntityManager;
33 import javax.sql.DataSource JavaDoc;
34
35 import org.apache.geronimo.gbean.GBeanInfo;
36 import org.apache.geronimo.gbean.GBeanInfoBuilder;
37 import org.apache.geronimo.gbean.GBeanLifecycle;
38 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
39 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
40 import org.apache.geronimo.connector.outbound.ConnectionFactorySource;
41 import org.apache.geronimo.transformer.TransformerAgent;
42 import org.apache.geronimo.kernel.classloader.JarFileClassLoader;
43
44 /**
45  * @version $Rev: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 2006) $
46  */

47 public class PersistenceUnitGBean implements GBeanLifecycle {
48
49     private final PersistenceUnitInfoImpl persistenceUnitInfo;
50     private final EntityManagerFactory entityManagerFactory;
51     private final TransactionManagerImpl transactionManager;
52
53
54     public PersistenceUnitGBean() {
55         persistenceUnitInfo = null;
56         entityManagerFactory = null;
57         transactionManager = null;
58     }
59
60     public PersistenceUnitGBean(String JavaDoc persistenceUnitName,
61             String JavaDoc persistenceProviderClassName,
62             String JavaDoc persistenceUnitTransactionTypeString,
63             ConnectionFactorySource jtaDataSourceWrapper,
64             ConnectionFactorySource nonJtaDataSourceWrapper,
65             List JavaDoc mappingFileNamesUntyped,
66             List JavaDoc jarFileUrlsUntyped,
67             URL JavaDoc persistenceUnitRootUrl,
68             List JavaDoc managedClassNamesUntyped,
69             boolean excludeUnlistedClassesValue,
70             Properties JavaDoc properties,
71             TransactionManagerImpl transactionManager,
72             ClassLoader JavaDoc classLoader) {
73         List JavaDoc<String JavaDoc> mappingFileNames = mappingFileNamesUntyped == null? new ArrayList JavaDoc<String JavaDoc>(): new ArrayList JavaDoc<String JavaDoc>(mappingFileNamesUntyped);
74         List JavaDoc<URL JavaDoc> jarFileUrls = jarFileUrlsUntyped == null? new ArrayList JavaDoc<URL JavaDoc>(): new ArrayList JavaDoc<URL JavaDoc>(jarFileUrlsUntyped);
75         List JavaDoc<String JavaDoc> managedClassNames = managedClassNamesUntyped == null? new ArrayList JavaDoc<String JavaDoc>(): new ArrayList JavaDoc<String JavaDoc>(managedClassNamesUntyped);
76         PersistenceUnitTransactionType persistenceUnitTransactionType = persistenceUnitTransactionTypeString == null? PersistenceUnitTransactionType.JTA: PersistenceUnitTransactionType.valueOf(persistenceUnitTransactionTypeString);
77
78         persistenceUnitInfo = new PersistenceUnitInfoImpl(persistenceUnitName,
79                 persistenceProviderClassName,
80                 persistenceUnitTransactionType,
81                 jtaDataSourceWrapper == null? null: (DataSource JavaDoc)jtaDataSourceWrapper.$getResource(),
82                 nonJtaDataSourceWrapper == null? null: (DataSource JavaDoc)nonJtaDataSourceWrapper.$getResource(),
83                 mappingFileNames,
84                 jarFileUrls,
85                 persistenceUnitRootUrl,
86                 managedClassNames,
87                 excludeUnlistedClassesValue,
88                 properties,
89                 classLoader);
90         try {
91             Class JavaDoc clazz = classLoader.loadClass(persistenceProviderClassName);
92             PersistenceProvider persistenceProvider = (PersistenceProvider) clazz.newInstance();
93             entityManagerFactory = persistenceProvider.createContainerEntityManagerFactory(persistenceUnitInfo, properties);
94         } catch (ClassNotFoundException JavaDoc e) {
95             throw new PersistenceException("Could not locate PersistenceProvider class: " + persistenceProviderClassName + " in classloader " + classLoader, e);
96         } catch (InstantiationException JavaDoc e) {
97             throw new PersistenceException("Could not create PersistenceProvider instance: " + persistenceProviderClassName + " loaded from classloader " + classLoader, e);
98         } catch (IllegalAccessException JavaDoc e) {
99             throw new PersistenceException("Could not create PersistenceProvider instance: " + persistenceProviderClassName + " loaded from classloader " + classLoader, e);
100         }
101         this.transactionManager = transactionManager;
102     }
103
104     public EntityManagerFactory getEntityManagerFactory() {
105         return entityManagerFactory;
106     }
107
108     public EntityManager getEntityManager(boolean transactionScoped, Map JavaDoc properties) {
109         if (transactionScoped) {
110             return new CMPEntityManagerTxScoped(transactionManager, getPersistenceUnitName(), entityManagerFactory, properties);
111         } else {
112             return new CMPEntityManagerExtended(transactionManager, getPersistenceUnitName(), entityManagerFactory, properties);
113         }
114     }
115
116     public String JavaDoc getPersistenceUnitName() {
117         return persistenceUnitInfo.getPersistenceUnitName();
118     }
119
120     public String JavaDoc getPersistenceProviderClassName() {
121         return persistenceUnitInfo.getPersistenceProviderClassName();
122     }
123
124     public PersistenceUnitTransactionType getTransactionType() {
125         return persistenceUnitInfo.getTransactionType();
126     }
127
128     public DataSource JavaDoc getJtaDataSource() {
129         return persistenceUnitInfo.getJtaDataSource();
130     }
131
132     public DataSource JavaDoc getNonJtaDataSource() {
133         return persistenceUnitInfo.getNonJtaDataSource();
134     }
135
136     public List JavaDoc<String JavaDoc> getMappingFileNames() {
137         return persistenceUnitInfo.getMappingFileNames();
138     }
139
140     public List JavaDoc<URL JavaDoc> getJarFileUrls() {
141         return persistenceUnitInfo.getJarFileUrls();
142     }
143
144     public URL JavaDoc getPersistenceUnitRootUrl() {
145         return persistenceUnitInfo.getPersistenceUnitRootUrl();
146     }
147
148     public List JavaDoc<String JavaDoc> getManagedClassNames() {
149         return persistenceUnitInfo.getManagedClassNames();
150     }
151
152     public boolean excludeUnlistedClasses() {
153         return persistenceUnitInfo.excludeUnlistedClasses();
154     }
155
156     public Properties JavaDoc getProperties() {
157         return persistenceUnitInfo.getProperties();
158     }
159
160     public ClassLoader JavaDoc getClassLoader() {
161         return persistenceUnitInfo.getClassLoader();
162     }
163
164     public void addTransformer(ClassTransformer classTransformer) {
165         persistenceUnitInfo.addTransformer(classTransformer);
166     }
167
168     public ClassLoader JavaDoc getNewTempClassLoader() {
169         return persistenceUnitInfo.getNewTempClassLoader();
170     }
171
172     public void doStart() throws Exception JavaDoc {
173     }
174
175     public void doStop() throws Exception JavaDoc {
176         //TODO remove any classtransformers added
177
entityManagerFactory.close();
178     }
179
180     public void doFail() {
181         entityManagerFactory.close();
182     }
183
184     private static class PersistenceUnitInfoImpl implements PersistenceUnitInfo {
185         private final String JavaDoc persistenceUnitName;
186         private final String JavaDoc persistenceProviderClassName;
187         private final PersistenceUnitTransactionType persistenceUnitTransactionType;
188         private final DataSource JavaDoc jtaDataSource;
189         private final DataSource JavaDoc nonJtaDataSource;
190         private final List JavaDoc<String JavaDoc> mappingFileNames;
191         private final List JavaDoc<URL JavaDoc> jarFileUrls;
192         private final URL JavaDoc persistenceUnitRootUrl;
193         private final List JavaDoc<String JavaDoc> managedClassNames;
194         private final boolean excludeUnlistedClassesValue;
195         private final Properties JavaDoc properties;
196         private final ClassLoader JavaDoc classLoader;
197
198
199         public PersistenceUnitInfoImpl(String JavaDoc persistenceUnitName, String JavaDoc persistenceProviderClassName, PersistenceUnitTransactionType persistenceUnitTransactionType, DataSource JavaDoc jtaDataSource, DataSource JavaDoc nonJtaDataSource, List JavaDoc<String JavaDoc> mappingFileNames, List JavaDoc<URL JavaDoc> jarFileUrls, URL JavaDoc persistenceUnitRootUrl, List JavaDoc<String JavaDoc> managedClassNames, boolean excludeUnlistedClassesValue, Properties JavaDoc properties, ClassLoader JavaDoc classLoader) {
200             this.persistenceUnitName = persistenceUnitName;
201             this.persistenceProviderClassName = persistenceProviderClassName;
202             this.persistenceUnitTransactionType = persistenceUnitTransactionType;
203             this.jtaDataSource = jtaDataSource;
204             this.nonJtaDataSource = nonJtaDataSource;
205             this.mappingFileNames = mappingFileNames;
206             this.jarFileUrls = jarFileUrls;
207             this.persistenceUnitRootUrl = persistenceUnitRootUrl;
208             this.managedClassNames = managedClassNames;
209             this.excludeUnlistedClassesValue = excludeUnlistedClassesValue;
210             this.properties = properties;
211             this.classLoader = classLoader;
212         }
213
214         public String JavaDoc getPersistenceUnitName() {
215             return persistenceUnitName;
216         }
217
218         public String JavaDoc getPersistenceProviderClassName() {
219             return persistenceProviderClassName;
220         }
221
222         public PersistenceUnitTransactionType getTransactionType() {
223             return persistenceUnitTransactionType;
224         }
225
226         public DataSource JavaDoc getJtaDataSource() {
227             return jtaDataSource;
228         }
229
230         public DataSource JavaDoc getNonJtaDataSource() {
231             return nonJtaDataSource;
232         }
233
234         public List JavaDoc<String JavaDoc> getMappingFileNames() {
235             return mappingFileNames;
236         }
237
238         public List JavaDoc<URL JavaDoc> getJarFileUrls() {
239             return jarFileUrls;
240         }
241
242         public URL JavaDoc getPersistenceUnitRootUrl() {
243             return persistenceUnitRootUrl;
244         }
245
246         public List JavaDoc<String JavaDoc> getManagedClassNames() {
247             return managedClassNames;
248         }
249
250         public boolean excludeUnlistedClasses() {
251             return excludeUnlistedClassesValue;
252         }
253
254         public Properties JavaDoc getProperties() {
255             return properties;
256         }
257
258         public ClassLoader JavaDoc getClassLoader() {
259             return classLoader;
260         }
261
262         public void addTransformer(ClassTransformer classTransformer) {
263             TransformerAgent.addTransformer(new TransformerWrapper(classTransformer, classLoader));
264         }
265
266         public ClassLoader JavaDoc getNewTempClassLoader() {
267             return JarFileClassLoader.copy(classLoader);
268         }
269
270     }
271
272     public static final GBeanInfo GBEAN_INFO;
273
274     static {
275         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(PersistenceUnitGBean.class);
276         infoBuilder.setPriority(GBeanInfo.PRIORITY_CLASSLOADER);
277
278         infoBuilder.addAttribute("persistenceUnitName", String JavaDoc.class, true, true);
279         infoBuilder.addAttribute("persistenceProviderClassName", String JavaDoc.class, true, true);
280         infoBuilder.addAttribute("persistenceUnitTransactionType", String JavaDoc.class, true, true);
281         infoBuilder.addAttribute("mappingFileNames", List JavaDoc.class, true, true);
282         infoBuilder.addAttribute("jarFileUrls", List JavaDoc.class, true, true);
283         infoBuilder.addAttribute("persistenceUnitRootUrl", URL JavaDoc.class, true, true);
284         infoBuilder.addAttribute("managedClassNames", List JavaDoc.class, true, true);
285         infoBuilder.addAttribute("excludeUnlistedClasses", boolean.class, true, true);
286         infoBuilder.addAttribute("properties", Properties JavaDoc.class, true, true);
287         infoBuilder.addReference("TransactionManager", TransactionManagerImpl.class, NameFactory.TRANSACTION_MANAGER);
288         infoBuilder.addReference("JtaDataSourceWrapper", ConnectionFactorySource.class, NameFactory.JCA_MANAGED_CONNECTION_FACTORY);
289         infoBuilder.addReference("NonJtaDataSourceWrapper", ConnectionFactorySource.class, NameFactory.JCA_MANAGED_CONNECTION_FACTORY);
290         infoBuilder.addOperation("getEntityManagerFactory");
291         infoBuilder.addOperation("getEntityManager", new Class JavaDoc[] {boolean.class, Map JavaDoc.class});
292
293         infoBuilder.setConstructor(new String JavaDoc[] {
294                 "persistenceUnitName",
295                 "persistenceProviderClassName",
296                 "persistenceUnitTransactionType",
297                 "JtaDataSourceWrapper",
298                 "NonJtaDataSourceWrapper",
299                 "mappingFileNames",
300                 "jarFileUrls",
301                 "persistenceUnitRootUrl",
302                 "managedClassNames",
303                 "excludeUnlistedClasses",
304                 "properties",
305                 "TransactionManager",
306                 "classLoader"
307         });
308
309         GBEAN_INFO = infoBuilder.getBeanInfo();
310
311     }
312
313     public static GBeanInfo getGBeanInfo() {
314         return GBEAN_INFO;
315     }
316
317 }
318
Popular Tags