KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > persistence > xml > JPersistenceUnitInfo


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JPersistenceUnitInfo.java 536 2006-05-29 14:07:54Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.persistence.xml;
27
28 import java.net.URL JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import javax.persistence.spi.ClassTransformer;
34 import javax.persistence.spi.PersistenceProvider;
35 import javax.persistence.spi.PersistenceUnitInfo;
36 import javax.persistence.spi.PersistenceUnitTransactionType;
37 import javax.sql.DataSource JavaDoc;
38
39 import org.objectweb.easybeans.loader.EasyBeansClassLoader;
40
41 /**
42  * Implementation of the PersistenceUnitInfo interface. It is given to the
43  * persistence provider to create entity managers.
44  * @author Florent Benoit
45  */

46 public class JPersistenceUnitInfo implements PersistenceUnitInfo {
47
48     /**
49      * Name of the persistence unit. Corresponds to the <name> element in
50      * the persistence.xml file.
51      */

52     private String JavaDoc persistenceUnitName = null;
53
54     /**
55      * Persistence provider implementation class name.
56      */

57     private String JavaDoc persistenceProviderClassName = null;
58
59     /**
60      * Transaction type of the entity managers created by the
61      * EntityManagerFactory. The transaction type corresponds to the
62      * transaction-type attribute in the persistence.xml file.
63      */

64     private PersistenceUnitTransactionType transactionType = null;
65
66     /**
67      * JTA-enabled data source.
68      */

69     private DataSource JavaDoc jtaDataSource = null;
70
71     /**
72      * The non-JTA-enabled data source.
73      */

74     private DataSource JavaDoc nonJtaDataSource = null;
75
76     /**
77      * JTA-enabled data source name.
78      */

79     private String JavaDoc jtaDataSourceName = null;
80
81     /**
82      * The non-JTA-enabled data source name.
83      */

84     private String JavaDoc nonJtaDataSourceName = null;
85
86     /**
87      * The list of mapping file names that the persistence provider must load to
88      * determine the mappings for the entity classes.
89      */

90     private List JavaDoc<String JavaDoc> mappingFileNames = null;
91
92     /**
93      * The list of JAR file URLs that the persistence provider must look in to
94      * find the entity classes that must be managed by EntityManagers of this
95      * name.
96      */

97     private List JavaDoc<URL JavaDoc> jarFiles = null;
98
99     /**
100      * URL for the jar file or directory that is the root of the persistence
101      * unit. (If the persistence unit is rooted in the WEB-INF/classes
102      * directory, this will be the URL of that directory.)
103      */

104     private URL JavaDoc persistenceUnitRootUrl = null;
105
106     /**
107      * The list of the names of the classes that the persistence provider must
108      * add it to its set of managed classes. Each name corresponds to a named
109      * &lt;class&gt; element in the persistence.xml file.
110      */

111     private List JavaDoc<String JavaDoc> managedClassNames = null;
112
113     /**
114      * Whether classes in the root of the persistence unit that have not been
115      * explicitly listed are to be included in the set of managed classes. This
116      * value corresponds to the &lt;exclude-unlisted-classes&gt; element in the
117      * persistence.xml file.
118      */

119     private boolean excludeUnlistedClasses = false;
120
121     /**
122      * Properties object that may contain vendor-specific properties contained
123      * in the persistence.xml file.
124      */

125     private Properties JavaDoc properties = null;
126
127     /**
128      * ClassLoader that the provider may use to load any classes, resources, or
129      * open URLs.
130      */

131     private ClassLoader JavaDoc classLoader = null;
132
133     /**
134      * URL object that points to the persistence.xml file.
135      */

136     private URL JavaDoc persistenceXmlFileUrl = null;
137
138     /**
139      * List of Class Transformers. Transformer is called when the Container
140      * invokes at class-(re)definition time
141      */

142     private List JavaDoc<ClassTransformer> classTransformers = null;
143
144     /**
145      * Persistence provider (instantiated object).
146      */

147     private PersistenceProvider persistenceProvider = null;
148
149
150     /**
151      * Default constructor.
152      */

153     public JPersistenceUnitInfo() {
154         this.properties = new Properties JavaDoc();
155         mappingFileNames = new ArrayList JavaDoc<String JavaDoc>();
156         managedClassNames = new ArrayList JavaDoc<String JavaDoc>();
157         classTransformers = new ArrayList JavaDoc<ClassTransformer>();
158         jarFiles = new ArrayList JavaDoc<URL JavaDoc>();
159     }
160
161     /**
162      * Sets the classloader.
163      * @param classLoader that the provider may use to load any classes,
164      * resources, or open URLs.
165      */

166     public void setClassLoader(final ClassLoader JavaDoc classLoader) {
167         this.classLoader = classLoader;
168     }
169
170     /**
171      * Sets the name of the persistence unit.
172      * @param persistenceUnitName the given name
173      */

174     public void setPersistenceUnitName(final String JavaDoc persistenceUnitName) {
175         this.persistenceUnitName = persistenceUnitName;
176     }
177
178     /**
179      * @return The name of the Persistence unit that is being created.
180      * Corresponds to the &lt;name&gt; element in persistence.xml
181      */

182     public String JavaDoc getPersistenceUnitName() {
183         return persistenceUnitName;
184     }
185
186     /**
187      * Adds a jar file to the list of JAR file URLs that the persistence
188      * provider must look in to find the entity classes that must be managed by
189      * EntityManagers of this name.
190      * @param jarFile URL of the jar file
191      */

192     public void addJarFile(final URL JavaDoc jarFile) {
193         this.jarFiles.add(jarFile);
194     }
195
196     /**
197      * Sets the JTA-enabled data source.
198      * @param jtaDataSource given datasource.
199      */

200     public void setJtaDataSource(final DataSource JavaDoc jtaDataSource) {
201         this.jtaDataSource = jtaDataSource;
202     }
203
204     /**
205      * Sets the non-JTA-enabled data source.
206      * @param nonJtaDataSource given datasource.
207      */

208     public void setNonJtaDataSource(final DataSource JavaDoc nonJtaDataSource) {
209         this.nonJtaDataSource = nonJtaDataSource;
210     }
211
212     /**
213      * Sets the JTA-enabled data source name.
214      * @param jtaDataSourceName given name.
215      */

216     public void setJtaDataSourceName(final String JavaDoc jtaDataSourceName) {
217         this.jtaDataSourceName = jtaDataSourceName;
218     }
219
220     /**
221      * Sets the non-JTA-enabled data source name.
222      * @param nonJtaDataSourceName given name.
223      */

224     public void setNonJtaDataSourceName(final String JavaDoc nonJtaDataSourceName) {
225         this.nonJtaDataSourceName = nonJtaDataSourceName;
226     }
227
228     /**
229      * Adds a filename to the list of mapping file names.
230      * @param mappingFileName name of the mapping file to add.
231      */

232     public void addMappingFileName(final String JavaDoc mappingFileName) {
233         this.mappingFileNames.add(mappingFileName);
234     }
235
236     /**
237      * Sets the persistence provider implementation class name.
238      * @param persistenceProviderClassName name of the class.
239      */

240     public void setPersistenceProviderClassName(final String JavaDoc persistenceProviderClassName) {
241         this.persistenceProviderClassName = persistenceProviderClassName;
242     }
243
244     /**
245      * Sets the persistence provider object.
246      * @param persistenceProvider the persistence provider object used.
247      */

248     public void setPersistenceProvider(final PersistenceProvider persistenceProvider) {
249         this.persistenceProvider = persistenceProvider;
250     }
251
252     /**
253      * Sets the URL object that points to the persistence.xml file.
254      * @param persistenceXmlFileUrl URL pointing to persistence.xml file
255      */

256     public void setPersistenceXmlFileUrl(final URL JavaDoc persistenceXmlFileUrl) {
257         this.persistenceXmlFileUrl = persistenceXmlFileUrl;
258     }
259
260     /**
261      * Sets the properties use by this unit.
262      * @param properties object with key/value.
263      */

264     public void setProperties(final Properties JavaDoc properties) {
265         this.properties = properties;
266     }
267
268     /**
269      * @return The name of the persistence provider implementation class.
270      * Corresponds to the &lt;provider&gt; element in persistence.xml
271      */

272     public String JavaDoc getPersistenceProviderClassName() {
273         return persistenceProviderClassName;
274     }
275
276     /**
277      * @return The persistence provider implementation object.
278      */

279     public PersistenceProvider getPersistenceProvider() {
280         return persistenceProvider;
281     }
282
283     /**
284      * @return the JTA-enabled data source to be used by the persistence
285      * provider. The data source corresponds to the named
286      * &lt;jta-data-source&gt; element in persistence.xml
287      */

288     public DataSource JavaDoc getJtaDataSource() {
289         return jtaDataSource;
290     }
291
292     /**
293      * @return The non-JTA-enabled data source to be used by the persistence
294      * provider when outside the container, or inside the container when
295      * accessing data outside the global transaction. The data source
296      * corresponds to the named &lt;non-jta-data-source&gt; element in
297      * persistence.xml
298      */

299     public DataSource JavaDoc getNonJtaDataSource() {
300         return nonJtaDataSource;
301     }
302
303     /**
304      * @return The list of mapping file names that the persistence provider must
305      * load to determine the mappings for the entity classes. The
306      * mapping files must be in the standard XML mapping format, be
307      * uniquely named and be resource-loadable from the application
308      * classpath. This list will not include the entity-mappings.xml
309      * file if one was specified. Each mapping file name corresponds to
310      * a &lt;mapping-file&gt; element in persistence.xml
311      */

312     public List JavaDoc<String JavaDoc> getMappingFileNames() {
313         return mappingFileNames;
314     }
315
316     /**
317      * @return The list of JAR file URLs that the persistence provider must look
318      * in to find the entity classes that must be managed by
319      * EntityManagers of this name. The persistence archive jar itself
320      * will always be the last entry in the list. Each jar file URL
321      * corresponds to a named &lt;jar-file&gt; element in
322      * persistence.xml
323      */

324     public List JavaDoc<URL JavaDoc> getJarFiles() {
325         return jarFiles;
326     }
327
328     /**
329      * @return Properties object that may contain vendor-specific properties
330      * contained in the persistence.xml file. Each property corresponds
331      * to a &lt;property&gt; element in persistence.xml
332      */

333     public Properties JavaDoc getProperties() {
334         return properties;
335     }
336
337     /**
338      * @return ClassLoader that the provider may use to load any classes,
339      * resources, or open URLs.
340      */

341     public ClassLoader JavaDoc getClassLoader() {
342         return classLoader;
343     }
344
345     /**
346      * @return URL object that points to the persistence.xml file; useful for
347      * providers that may need to re-read the persistence.xml file. If
348      * no persistence.xml file is present in the persistence archive,
349      * null is returned.
350      */

351     public URL JavaDoc getPersistenceXmlFileUrl() {
352         return persistenceXmlFileUrl;
353     }
354
355     /**
356      * Gets the jta datasource name.
357      * @return jta datasource name.
358      */

359     public String JavaDoc getJtaDataSourceName() {
360         return jtaDataSourceName;
361     }
362
363     /**
364      * Gets the non jta datasource name.
365      * @return non jta datasource name
366      */

367     public String JavaDoc getNonJtaDataSourceName() {
368         return nonJtaDataSourceName;
369     }
370
371     /**
372      * @return The transaction type of the entity managers created by the
373      * EntityManagerFactory. The transaction type corresponds to the
374      * transaction-type attribute in the persistence.xml file.
375      */

376     public PersistenceUnitTransactionType getTransactionType() {
377         return transactionType;
378     }
379
380     /**
381      * Sets the transaction type of the entity managers created by the
382      * EntityManagerFactory.
383      * @param transactionType The transaction type corresponds to the
384      * transaction-type attribute in the persistence.xml file.
385      */

386     public void setTransactionType(final PersistenceUnitTransactionType transactionType) {
387         this.transactionType = transactionType;
388     }
389
390     /**
391      * @return The list of JAR file URLs that the persistence provider must
392      * examine for managed classes of the persistence unit. Each jar
393      * file URL corresponds to a named &lt;jar-file&gt; element in the
394      * persistence.xml file.
395      */

396     public List JavaDoc<URL JavaDoc> getJarFileUrls() {
397         return jarFiles;
398     }
399
400     /**
401      * Sets the URL for the jar file or directory that is the root of the
402      * persistence unit.
403      * @param persistenceUnitRootUrl root url of persistence unit
404      */

405     public void setPersistenceUnitRootUrl(final URL JavaDoc persistenceUnitRootUrl) {
406         this.persistenceUnitRootUrl = persistenceUnitRootUrl;
407     }
408
409     /**
410      * @return The URL for the jar file or directory that is the root of the
411      * persistence unit. (If the persistence unit is rooted in the
412      * WEB-INF/classes directory, this will be the URL of that
413      * directory.)
414      */

415     public URL JavaDoc getPersistenceUnitRootUrl() {
416         return persistenceUnitRootUrl;
417     }
418
419     /**
420      * Adds a class that the persistence provider must add it to its set of
421      * managed classes.
422      * @param className name of the class
423      */

424     public void addClass(final String JavaDoc className) {
425         managedClassNames.add(className);
426     }
427
428     /**
429      * @return The list of the names of the classes that the persistence
430      * provider must add it to its set of managed classes. Each name
431      * corresponds to a named &lt;class&gt; element in the
432      * persistence.xml file.
433      */

434     public List JavaDoc<String JavaDoc> getManagedClassNames() {
435         return managedClassNames;
436     }
437
438     /**
439      * Sets the boolean defining if the persistence unit that have not been
440      * explicitly listed are to be included in the set of managed classes.
441      * @param excludeUnlistedClasses true/false
442      */

443     public void setExcludeUnlistedClasses(final boolean excludeUnlistedClasses) {
444         this.excludeUnlistedClasses = excludeUnlistedClasses;
445     }
446
447     /**
448      * @return Whether classes in the root of the persistence unit that have not
449      * been explicitly listed are to be included in the set of managed
450      * classes. This value corresponds to the
451      * &lt;exclude-unlisted-classes&gt; element in the persistence.xml
452      * file.
453      */

454     public boolean excludeUnlistedClasses() {
455         return excludeUnlistedClasses;
456     }
457
458     /**
459      * Add a transformer supplied by the provider that will be called for every
460      * new class definition or class redefinition that gets loaded by the loader
461      * returned by the PersistenceInfo.getClassLoader method. The transformer
462      * has no effect on the result returned by the
463      * PersistenceInfo.getTempClassLoader method. Classes are only transformed
464      * once within the same classloading scope, regardless of how many
465      * persistence units they may be a part of.
466      * @param transformer A provider-supplied transformer that the Container
467      * invokes at class-(re)definition time
468      */

469     public void addTransformer(final ClassTransformer transformer) {
470         classTransformers.add(transformer);
471     }
472
473     /**
474      * Return a new instance of a ClassLoader that the provider may use to
475      * temporarily load any classes, resources, or open URLs. The scope and
476      * classpath of this loader is exactly the same as that of the loader
477      * returned by PersistenceInfo.getClassLoader. None of the classes loaded by
478      * this class loader will be visible to application components. The
479      * container does not use or maintain references to this class loader after
480      * returning it to the provider.
481      * @return Temporary ClassLoader with same visibility as current loader
482      */

483     public ClassLoader JavaDoc getNewTempClassLoader() {
484         if (classLoader instanceof EasyBeansClassLoader) {
485             EasyBeansClassLoader currentCL = (EasyBeansClassLoader) classLoader;
486             return (EasyBeansClassLoader) currentCL.clone();
487         }
488         throw new IllegalStateException JavaDoc("Cannot build a new temporary classloader");
489     }
490
491 }
492
Popular Tags