001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JPersistenceUnitInfo.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.persistence.xml;
025:
026: import java.net.URL;
027: import java.util.ArrayList;
028: import java.util.List;
029: import java.util.Properties;
030:
031: import javax.persistence.spi.ClassTransformer;
032: import javax.persistence.spi.PersistenceProvider;
033: import javax.persistence.spi.PersistenceUnitInfo;
034: import javax.persistence.spi.PersistenceUnitTransactionType;
035: import javax.sql.DataSource;
036:
037: import org.ow2.easybeans.loader.EasyBeansClassLoader;
038:
039: /**
040: * Implementation of the PersistenceUnitInfo interface. It is given to the
041: * persistence provider to create entity managers.
042: * @author Florent Benoit
043: */
044: public class JPersistenceUnitInfo implements PersistenceUnitInfo {
045:
046: /**
047: * Name of the persistence unit. Corresponds to the <name> element in
048: * the persistence.xml file.
049: */
050: private String persistenceUnitName = null;
051:
052: /**
053: * Persistence provider implementation class name.
054: */
055: private String persistenceProviderClassName = null;
056:
057: /**
058: * Transaction type of the entity managers created by the
059: * EntityManagerFactory. The transaction type corresponds to the
060: * transaction-type attribute in the persistence.xml file.
061: */
062: private PersistenceUnitTransactionType transactionType = null;
063:
064: /**
065: * JTA-enabled data source.
066: */
067: private DataSource jtaDataSource = null;
068:
069: /**
070: * The non-JTA-enabled data source.
071: */
072: private DataSource nonJtaDataSource = null;
073:
074: /**
075: * JTA-enabled data source name.
076: */
077: private String jtaDataSourceName = null;
078:
079: /**
080: * The non-JTA-enabled data source name.
081: */
082: private String nonJtaDataSourceName = null;
083:
084: /**
085: * The list of mapping file names that the persistence provider must load to
086: * determine the mappings for the entity classes.
087: */
088: private List<String> mappingFileNames = null;
089:
090: /**
091: * The list of JAR file URLs that the persistence provider must look in to
092: * find the entity classes that must be managed by EntityManagers of this
093: * name.
094: */
095: private List<URL> jarFiles = null;
096:
097: /**
098: * URL for the jar file or directory that is the root of the persistence
099: * unit. (If the persistence unit is rooted in the WEB-INF/classes
100: * directory, this will be the URL of that directory.)
101: */
102: private URL persistenceUnitRootUrl = null;
103:
104: /**
105: * The list of the names of the classes that the persistence provider must
106: * add it to its set of managed classes. Each name corresponds to a named
107: * <class> element in the persistence.xml file.
108: */
109: private List<String> managedClassNames = null;
110:
111: /**
112: * Whether classes in the root of the persistence unit that have not been
113: * explicitly listed are to be included in the set of managed classes. This
114: * value corresponds to the <exclude-unlisted-classes> element in the
115: * persistence.xml file.
116: */
117: private boolean excludeUnlistedClasses = false;
118:
119: /**
120: * Properties object that may contain vendor-specific properties contained
121: * in the persistence.xml file.
122: */
123: private Properties properties = null;
124:
125: /**
126: * ClassLoader that the provider may use to load any classes, resources, or
127: * open URLs.
128: */
129: private ClassLoader classLoader = null;
130:
131: /**
132: * URL object that points to the persistence.xml file.
133: */
134: private URL persistenceXmlFileUrl = null;
135:
136: /**
137: * Persistence provider (instantiated object).
138: */
139: private PersistenceProvider persistenceProvider = null;
140:
141: /**
142: * Default constructor.
143: */
144: public JPersistenceUnitInfo() {
145: this .properties = new Properties();
146: mappingFileNames = new ArrayList<String>();
147: managedClassNames = new ArrayList<String>();
148: jarFiles = new ArrayList<URL>();
149: }
150:
151: /**
152: * Sets the classloader.
153: * @param classLoader that the provider may use to load any classes,
154: * resources, or open URLs.
155: */
156: public void setClassLoader(final ClassLoader classLoader) {
157: this .classLoader = classLoader;
158: }
159:
160: /**
161: * Sets the name of the persistence unit.
162: * @param persistenceUnitName the given name
163: */
164: public void setPersistenceUnitName(final String persistenceUnitName) {
165: this .persistenceUnitName = persistenceUnitName;
166: }
167:
168: /**
169: * @return The name of the Persistence unit that is being created.
170: * Corresponds to the <name> element in persistence.xml
171: */
172: public String getPersistenceUnitName() {
173: return persistenceUnitName;
174: }
175:
176: /**
177: * Adds a jar file to the list of JAR file URLs that the persistence
178: * provider must look in to find the entity classes that must be managed by
179: * EntityManagers of this name.
180: * @param jarFile URL of the jar file
181: */
182: public void addJarFile(final URL jarFile) {
183: this .jarFiles.add(jarFile);
184: }
185:
186: /**
187: * Sets the JTA-enabled data source.
188: * @param jtaDataSource given datasource.
189: */
190: public void setJtaDataSource(final DataSource jtaDataSource) {
191: this .jtaDataSource = jtaDataSource;
192: }
193:
194: /**
195: * Sets the non-JTA-enabled data source.
196: * @param nonJtaDataSource given datasource.
197: */
198: public void setNonJtaDataSource(final DataSource nonJtaDataSource) {
199: this .nonJtaDataSource = nonJtaDataSource;
200: }
201:
202: /**
203: * Sets the JTA-enabled data source name.
204: * @param jtaDataSourceName given name.
205: */
206: public void setJtaDataSourceName(final String jtaDataSourceName) {
207: this .jtaDataSourceName = jtaDataSourceName;
208: }
209:
210: /**
211: * Sets the non-JTA-enabled data source name.
212: * @param nonJtaDataSourceName given name.
213: */
214: public void setNonJtaDataSourceName(
215: final String nonJtaDataSourceName) {
216: this .nonJtaDataSourceName = nonJtaDataSourceName;
217: }
218:
219: /**
220: * Adds a filename to the list of mapping file names.
221: * @param mappingFileName name of the mapping file to add.
222: */
223: public void addMappingFileName(final String mappingFileName) {
224: this .mappingFileNames.add(mappingFileName);
225: }
226:
227: /**
228: * Sets the persistence provider implementation class name.
229: * @param persistenceProviderClassName name of the class.
230: */
231: public void setPersistenceProviderClassName(
232: final String persistenceProviderClassName) {
233: this .persistenceProviderClassName = persistenceProviderClassName;
234: }
235:
236: /**
237: * Sets the persistence provider object.
238: * @param persistenceProvider the persistence provider object used.
239: */
240: public void setPersistenceProvider(
241: final PersistenceProvider persistenceProvider) {
242: this .persistenceProvider = persistenceProvider;
243: }
244:
245: /**
246: * Sets the URL object that points to the persistence.xml file.
247: * @param persistenceXmlFileUrl URL pointing to persistence.xml file
248: */
249: public void setPersistenceXmlFileUrl(final URL persistenceXmlFileUrl) {
250: this .persistenceXmlFileUrl = persistenceXmlFileUrl;
251: }
252:
253: /**
254: * Sets the properties use by this unit.
255: * @param properties object with key/value.
256: */
257: public void setProperties(final Properties properties) {
258: this .properties = properties;
259: }
260:
261: /**
262: * @return The name of the persistence provider implementation class.
263: * Corresponds to the <provider> element in persistence.xml
264: */
265: public String getPersistenceProviderClassName() {
266: return persistenceProviderClassName;
267: }
268:
269: /**
270: * @return The persistence provider implementation object.
271: */
272: public PersistenceProvider getPersistenceProvider() {
273: return persistenceProvider;
274: }
275:
276: /**
277: * @return the JTA-enabled data source to be used by the persistence
278: * provider. The data source corresponds to the named
279: * <jta-data-source> element in persistence.xml
280: */
281: public DataSource getJtaDataSource() {
282: return jtaDataSource;
283: }
284:
285: /**
286: * @return The non-JTA-enabled data source to be used by the persistence
287: * provider when outside the container, or inside the container when
288: * accessing data outside the global transaction. The data source
289: * corresponds to the named <non-jta-data-source> element in
290: * persistence.xml
291: */
292: public DataSource getNonJtaDataSource() {
293: return nonJtaDataSource;
294: }
295:
296: /**
297: * @return The list of mapping file names that the persistence provider must
298: * load to determine the mappings for the entity classes. The
299: * mapping files must be in the standard XML mapping format, be
300: * uniquely named and be resource-loadable from the application
301: * classpath. This list will not include the entity-mappings.xml
302: * file if one was specified. Each mapping file name corresponds to
303: * a <mapping-file> element in persistence.xml
304: */
305: public List<String> getMappingFileNames() {
306: return mappingFileNames;
307: }
308:
309: /**
310: * @return The list of JAR file URLs that the persistence provider must look
311: * in to find the entity classes that must be managed by
312: * EntityManagers of this name. The persistence archive jar itself
313: * will always be the last entry in the list. Each jar file URL
314: * corresponds to a named <jar-file> element in
315: * persistence.xml
316: */
317: public List<URL> getJarFiles() {
318: return jarFiles;
319: }
320:
321: /**
322: * @return Properties object that may contain vendor-specific properties
323: * contained in the persistence.xml file. Each property corresponds
324: * to a <property> element in persistence.xml
325: */
326: public Properties getProperties() {
327: return properties;
328: }
329:
330: /**
331: * @return ClassLoader that the provider may use to load any classes,
332: * resources, or open URLs.
333: */
334: public ClassLoader getClassLoader() {
335: return classLoader;
336: }
337:
338: /**
339: * @return URL object that points to the persistence.xml file; useful for
340: * providers that may need to re-read the persistence.xml file. If
341: * no persistence.xml file is present in the persistence archive,
342: * null is returned.
343: */
344: public URL getPersistenceXmlFileUrl() {
345: return persistenceXmlFileUrl;
346: }
347:
348: /**
349: * Gets the jta datasource name.
350: * @return jta datasource name.
351: */
352: public String getJtaDataSourceName() {
353: return jtaDataSourceName;
354: }
355:
356: /**
357: * Gets the non jta datasource name.
358: * @return non jta datasource name
359: */
360: public String getNonJtaDataSourceName() {
361: return nonJtaDataSourceName;
362: }
363:
364: /**
365: * @return The transaction type of the entity managers created by the
366: * EntityManagerFactory. The transaction type corresponds to the
367: * transaction-type attribute in the persistence.xml file.
368: */
369: public PersistenceUnitTransactionType getTransactionType() {
370: return transactionType;
371: }
372:
373: /**
374: * Sets the transaction type of the entity managers created by the
375: * EntityManagerFactory.
376: * @param transactionType The transaction type corresponds to the
377: * transaction-type attribute in the persistence.xml file.
378: */
379: public void setTransactionType(
380: final PersistenceUnitTransactionType transactionType) {
381: this .transactionType = transactionType;
382: }
383:
384: /**
385: * @return The list of JAR file URLs that the persistence provider must
386: * examine for managed classes of the persistence unit. Each jar
387: * file URL corresponds to a named <jar-file> element in the
388: * persistence.xml file.
389: */
390: public List<URL> getJarFileUrls() {
391: return jarFiles;
392: }
393:
394: /**
395: * Sets the URL for the jar file or directory that is the root of the
396: * persistence unit.
397: * @param persistenceUnitRootUrl root url of persistence unit
398: */
399: public void setPersistenceUnitRootUrl(
400: final URL persistenceUnitRootUrl) {
401: this .persistenceUnitRootUrl = persistenceUnitRootUrl;
402: }
403:
404: /**
405: * @return The URL for the jar file or directory that is the root of the
406: * persistence unit. (If the persistence unit is rooted in the
407: * WEB-INF/classes directory, this will be the URL of that
408: * directory.)
409: */
410: public URL getPersistenceUnitRootUrl() {
411: return persistenceUnitRootUrl;
412: }
413:
414: /**
415: * Adds a class that the persistence provider must add it to its set of
416: * managed classes.
417: * @param className name of the class
418: */
419: public void addClass(final String className) {
420: managedClassNames.add(className);
421: }
422:
423: /**
424: * @return The list of the names of the classes that the persistence
425: * provider must add it to its set of managed classes. Each name
426: * corresponds to a named <class> element in the
427: * persistence.xml file.
428: */
429: public List<String> getManagedClassNames() {
430: return managedClassNames;
431: }
432:
433: /**
434: * Sets the boolean defining if the persistence unit that have not been
435: * explicitly listed are to be included in the set of managed classes.
436: * @param excludeUnlistedClasses true/false
437: */
438: public void setExcludeUnlistedClasses(
439: final boolean excludeUnlistedClasses) {
440: this .excludeUnlistedClasses = excludeUnlistedClasses;
441: }
442:
443: /**
444: * @return Whether classes in the root of the persistence unit that have not
445: * been explicitly listed are to be included in the set of managed
446: * classes. This value corresponds to the
447: * <exclude-unlisted-classes> element in the persistence.xml
448: * file.
449: */
450: public boolean excludeUnlistedClasses() {
451: return excludeUnlistedClasses;
452: }
453:
454: /**
455: * Add a transformer supplied by the provider that will be called for every
456: * new class definition or class redefinition that gets loaded by the loader
457: * returned by the PersistenceInfo.getClassLoader method. The transformer
458: * has no effect on the result returned by the
459: * PersistenceInfo.getTempClassLoader method. Classes are only transformed
460: * once within the same classloading scope, regardless of how many
461: * persistence units they may be a part of.
462: * @param transformer A provider-supplied transformer that the Container
463: * invokes at class-(re)definition time
464: */
465: public void addTransformer(final ClassTransformer transformer) {
466: if (classLoader instanceof EasyBeansClassLoader) {
467: EasyBeansClassLoader currentCL = (EasyBeansClassLoader) classLoader;
468: currentCL.addTransformer(transformer);
469: return;
470: }
471: throw new IllegalStateException(
472: "Cannot add the given transformer as ClassLoader is not an EasyBeans classloader");
473: }
474:
475: /**
476: * Return a new instance of a ClassLoader that the provider may use to
477: * temporarily load any classes, resources, or open URLs. The scope and
478: * classpath of this loader is exactly the same as that of the loader
479: * returned by PersistenceInfo.getClassLoader. None of the classes loaded by
480: * this class loader will be visible to application components. The
481: * container does not use or maintain references to this class loader after
482: * returning it to the provider.
483: * @return Temporary ClassLoader with same visibility as current loader
484: */
485: public ClassLoader getNewTempClassLoader() {
486: if (classLoader instanceof EasyBeansClassLoader) {
487: EasyBeansClassLoader currentCL = (EasyBeansClassLoader) classLoader;
488: return (EasyBeansClassLoader) currentCL.clone();
489: }
490: throw new IllegalStateException(
491: "Cannot build a new temporary classloader");
492: }
493:
494: }
|