KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > JpaUnit


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

19
20 package org.apache.cayenne.jpa;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import javax.persistence.spi.ClassTransformer;
30 import javax.persistence.spi.PersistenceUnitInfo;
31 import javax.persistence.spi.PersistenceUnitTransactionType;
32 import javax.sql.DataSource JavaDoc;
33
34 import org.apache.cayenne.jpa.conf.JpaDataSourceFactory;
35
36 /**
37  * A <code>javax.persistence.spi.PersistenceUnitInfo</code> implementor used by Cayenne
38  * JPA provider.
39  *
40  * @author Andrus Adamchik
41  */

42 public abstract class JpaUnit implements PersistenceUnitInfo {
43
44     protected String JavaDoc persistenceUnitName;
45     protected List JavaDoc<String JavaDoc> mappingFileNames;
46     protected List JavaDoc<URL JavaDoc> jarFileUrls;
47     protected List JavaDoc<String JavaDoc> managedClassNames;
48     protected URL JavaDoc persistenceUnitRootUrl;
49     protected boolean excludeUnlistedClasses;
50     protected Properties JavaDoc properties;
51     protected String JavaDoc description;
52
53     // properties not exposed directly
54
protected ClassLoader JavaDoc classLoader;
55
56     public JpaUnit() {
57
58         this.mappingFileNames = new ArrayList JavaDoc<String JavaDoc>(2);
59         this.jarFileUrls = new ArrayList JavaDoc<URL JavaDoc>(2);
60         this.managedClassNames = new ArrayList JavaDoc<String JavaDoc>(30);
61         this.properties = new Properties JavaDoc();
62
63         setDefaultClassLoader();
64     }
65
66     public String JavaDoc getPersistenceUnitName() {
67         return persistenceUnitName;
68     }
69
70     public String JavaDoc getPersistenceProviderClassName() {
71         return getProperty(Provider.PROVIDER_PROPERTY);
72     }
73
74     /**
75      * Adds a {@link ClassTransformer} to the persistence unit.
76      * <h3>JPA Specification, 7.1.4:</h3>
77      * Add a transformer supplied by the provider that will be called for every new class
78      * definition or class redefinition that gets loaded by the loader returned by the
79      * PersistenceInfo.getClassLoader method. The transformer has no effect on the result
80      * returned by the PersistenceInfo.getTempClassLoader method. Classes are only
81      * transformed once within the same classloading scope, regardless of how many
82      * persistence units they may be a part of.
83      *
84      * @param transformer A provider-supplied transformer that the Container invokes at
85      * class-(re)definition time
86      */

87     public abstract void addTransformer(ClassTransformer transformer);
88
89     public PersistenceUnitTransactionType getTransactionType() {
90         String JavaDoc type = getProperty(Provider.TRANSACTION_TYPE_PROPERTY);
91
92         // default JTA type is somewhat arbitrary as application-managed EntityManagers
93
// will use resource-local, while container-managed will use JTA. Normally whoever
94
// created this unit will set the right value.
95
return type != null
96                 ? PersistenceUnitTransactionType.valueOf(type)
97                 : PersistenceUnitTransactionType.JTA;
98     }
99
100     String JavaDoc getProperty(String JavaDoc key) {
101         return properties.getProperty(key);
102     }
103
104     JpaDataSourceFactory getJpaDataSourceFactory() {
105         String JavaDoc factory = getProperty(Provider.DATA_SOURCE_FACTORY_PROPERTY);
106
107         if (factory == null) {
108             throw new JpaProviderException("No value for '"
109                     + Provider.DATA_SOURCE_FACTORY_PROPERTY
110                     + "' property - can't build DataSource factory.");
111         }
112
113         try {
114             // use app class loader - this is not the class to enhance...
115
return (JpaDataSourceFactory) Class.forName(
116                     factory,
117                     true,
118                     Thread.currentThread().getContextClassLoader()).newInstance();
119         }
120         catch (Throwable JavaDoc th) {
121             throw new JpaProviderException("Error instantiating a JPADataSourceFactory: "
122                     + factory, th);
123         }
124     }
125
126     public DataSource JavaDoc getJtaDataSource() {
127         String JavaDoc name = getProperty(Provider.JTA_DATA_SOURCE_PROPERTY);
128         return getJpaDataSourceFactory().getJtaDataSource(name, this);
129     }
130
131     public DataSource JavaDoc getNonJtaDataSource() {
132         String JavaDoc name = getProperty(Provider.NON_JTA_DATA_SOURCE_PROPERTY);
133         return getJpaDataSourceFactory().getNonJtaDataSource(name, this);
134     }
135
136     public List JavaDoc<String JavaDoc> getMappingFileNames() {
137         return mappingFileNames;
138     }
139
140     public List JavaDoc<URL JavaDoc> getJarFileUrls() {
141         return jarFileUrls;
142     }
143
144     public URL JavaDoc getPersistenceUnitRootUrl() {
145         return persistenceUnitRootUrl;
146     }
147
148     public List JavaDoc<String JavaDoc> getManagedClassNames() {
149         return managedClassNames;
150     }
151
152     /**
153      * Returns whether classes not listed in the persistence.xml descriptor file should be
154      * excluded from persistence unit. Should be ignored in J2SE environment.
155      */

156     public boolean excludeUnlistedClasses() {
157         return excludeUnlistedClasses;
158     }
159
160     public Properties JavaDoc getProperties() {
161         return properties;
162     }
163
164     public ClassLoader JavaDoc getClassLoader() {
165         return classLoader;
166     }
167
168     /**
169      * Creates and returns a child of the main unit ClassLoader.
170      */

171     public ClassLoader JavaDoc getNewTempClassLoader() {
172         return new JpaUnitClassLoader(Thread.currentThread().getContextClassLoader());
173     }
174
175     public void setExcludeUnlistedClasses(boolean excludeUnlistedClasses) {
176         this.excludeUnlistedClasses = excludeUnlistedClasses;
177     }
178
179     public void addJarFileUrl(String JavaDoc jarName) {
180         // resolve URLs relative to the unit root
181

182         if (persistenceUnitRootUrl == null) {
183             throw new IllegalStateException JavaDoc("Persistence Unit Root URL is not set");
184         }
185
186         try {
187             this.jarFileUrls.add(new URL JavaDoc(persistenceUnitRootUrl, jarName));
188         }
189         catch (MalformedURLException JavaDoc e) {
190             throw new IllegalArgumentException JavaDoc("Invalid Jar file name:" + jarName, e);
191         }
192     }
193
194     public void setPersistenceUnitName(String JavaDoc persistenceUnitName) {
195         this.persistenceUnitName = persistenceUnitName;
196     }
197
198     /**
199      * Sets new "main" ClassLoader of this unit.
200      */

201     public void setClassLoader(ClassLoader JavaDoc classLoader) {
202         if (classLoader == null) {
203             setDefaultClassLoader();
204         }
205         else {
206             this.classLoader = classLoader;
207         }
208     }
209
210     protected void setDefaultClassLoader() {
211         this.classLoader = Thread.currentThread().getContextClassLoader();
212     }
213
214     public void addManagedClassName(String JavaDoc managedClassName) {
215         this.managedClassNames.add(managedClassName);
216     }
217
218     public void addMappingFileName(String JavaDoc mappingFileName) {
219         this.mappingFileNames.add(mappingFileName);
220     }
221
222     public void setPersistenceUnitRootUrl(URL JavaDoc persistenceUnitRootUrl) {
223         this.persistenceUnitRootUrl = persistenceUnitRootUrl;
224     }
225
226     public void addProperties(Map JavaDoc properties) {
227         this.properties.putAll(properties);
228     }
229
230     public void putProperty(String JavaDoc key, String JavaDoc value) {
231         this.properties.put(key, value);
232     }
233
234     public String JavaDoc getDescription() {
235         return description;
236     }
237
238     public void setDescription(String JavaDoc description) {
239         this.description = description;
240     }
241
242 }
243
Popular Tags