001 // Copyright 2011 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.internal.jpa;
016
017 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
018 import org.apache.tapestry5.jpa.TapestryPersistenceUnitInfo;
019
020 import javax.naming.Context;
021 import javax.naming.InitialContext;
022 import javax.naming.NamingException;
023 import javax.persistence.SharedCacheMode;
024 import javax.persistence.ValidationMode;
025 import javax.persistence.spi.ClassTransformer;
026 import javax.persistence.spi.PersistenceUnitTransactionType;
027 import javax.sql.DataSource;
028 import java.net.MalformedURLException;
029 import java.net.URL;
030 import java.util.Collections;
031 import java.util.List;
032 import java.util.Properties;
033 import java.util.Set;
034
035 public class PersistenceUnitInfoImpl implements TapestryPersistenceUnitInfo
036 {
037 private String persistenceUnitName;
038
039 private String persistenceProviderClassName;
040
041 private String persistenceXMLSchemaVersion;
042
043 private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
044
045 private DataSource nonJtaDataSource;
046
047 private DataSource jtaDataSource;
048
049 private ValidationMode validationMode;
050
051 private SharedCacheMode sharedCacheMode;
052
053 private boolean excludeUnlistedClasses = true;
054
055 private final Set<String> managedClassNames = CollectionFactory.newSet();
056
057 private final Set<String> mappingFilesNames = CollectionFactory.newSet();
058
059 private final List<URL> jarFileUrls = CollectionFactory.newList();
060
061 private final Properties properties = new Properties();
062
063
064 public PersistenceUnitInfoImpl(String persistenceUnitName)
065 {
066 this.persistenceUnitName = persistenceUnitName;
067 }
068
069 /**
070 * {@inheritDoc}
071 */
072 public String getPersistenceUnitName()
073 {
074 return persistenceUnitName;
075 }
076
077 /**
078 * {@inheritDoc}
079 */
080 public String getPersistenceProviderClassName()
081 {
082 return persistenceProviderClassName;
083 }
084
085 /**
086 * {@inheritDoc}
087 */
088 public TapestryPersistenceUnitInfo persistenceProviderClassName(final String persistenceProviderClassName)
089 {
090 this.persistenceProviderClassName = persistenceProviderClassName;
091
092 return this;
093 }
094
095 /**
096 * {@inheritDoc}
097 */
098 public PersistenceUnitTransactionType getTransactionType()
099 {
100 return transactionType;
101 }
102
103 /**
104 * {@inheritDoc}
105 */
106 public TapestryPersistenceUnitInfo transactionType(final PersistenceUnitTransactionType transactionType)
107 {
108 this.transactionType = transactionType;
109
110 return this;
111 }
112
113 /**
114 * {@inheritDoc}
115 */
116 public DataSource getJtaDataSource()
117 {
118 return jtaDataSource;
119 }
120
121 /**
122 * {@inheritDoc}
123 */
124 public DataSource getNonJtaDataSource()
125 {
126 return nonJtaDataSource;
127 }
128
129 /**
130 * {@inheritDoc}
131 */
132 public TapestryPersistenceUnitInfo nonJtaDataSource(final String nonJtaDataSource)
133 {
134 this.nonJtaDataSource = lookupDataSource(nonJtaDataSource);
135
136 return this;
137 }
138
139 /**
140 * {@inheritDoc}
141 */
142 public TapestryPersistenceUnitInfo jtaDataSource(final String jtaDataSource)
143 {
144 this.jtaDataSource = lookupDataSource(jtaDataSource);
145
146 return this;
147 }
148
149 /**
150 * {@inheritDoc}
151 */
152 public List<String> getMappingFileNames()
153 {
154 List<String> tmp = CollectionFactory.newList(mappingFilesNames);
155 return Collections.unmodifiableList(tmp);
156 }
157
158 /**
159 * {@inheritDoc}
160 */
161 public TapestryPersistenceUnitInfo addMappingFileName(final String fileName)
162 {
163 mappingFilesNames.add(fileName);
164
165 return this;
166
167 }
168
169 /**
170 * {@inheritDoc}
171 */
172 public TapestryPersistenceUnitInfo addJarFileUrl(URL url)
173 {
174 jarFileUrls.add(url);
175
176 return this;
177 }
178
179 /**
180 * {@inheritDoc}
181 */
182 public TapestryPersistenceUnitInfo addJarFileUrl(String url)
183 {
184 try
185 {
186 return addJarFileUrl(new URL(getPersistenceUnitRootUrl(), url));
187 } catch (MalformedURLException e)
188 {
189 throw new RuntimeException(e);
190 }
191 }
192
193
194 /**
195 * {@inheritDoc}
196 */
197 public TapestryPersistenceUnitInfo addProperty(String name, String value)
198 {
199 getProperties().put(name, value);
200
201 return this;
202 }
203
204
205 /**
206 * {@inheritDoc}
207 */
208 public TapestryPersistenceUnitInfo excludeUnlistedClasses(boolean exclude)
209 {
210 this.excludeUnlistedClasses = exclude;
211
212 return this;
213 }
214
215 /**
216 * {@inheritDoc}
217 */
218 public List<URL> getJarFileUrls()
219 {
220 return Collections.unmodifiableList(jarFileUrls);
221 }
222
223 /**
224 * {@inheritDoc}
225 */
226 public URL getPersistenceUnitRootUrl()
227 {
228 return getClass().getResource("/");
229 }
230
231 /**
232 * {@inheritDoc}
233 */
234 public List<String> getManagedClassNames()
235 {
236 List<String> tmp = CollectionFactory.newList(managedClassNames);
237 return Collections.<String>unmodifiableList(tmp);
238 }
239
240 /**
241 * {@inheritDoc}
242 */
243 public TapestryPersistenceUnitInfo addManagedClassName(final String className)
244 {
245 managedClassNames.add(className);
246
247 return this;
248 }
249
250 /**
251 * {@inheritDoc}
252 */
253 public TapestryPersistenceUnitInfo addManagedClass(final Class<?> clazz)
254 {
255 addManagedClassName(clazz.getName());
256
257 return this;
258 }
259
260 /**
261 * {@inheritDoc}
262 */
263 public boolean excludeUnlistedClasses()
264 {
265 return excludeUnlistedClasses;
266 }
267
268 /**
269 * {@inheritDoc}
270 */
271 public SharedCacheMode getSharedCacheMode()
272 {
273 return sharedCacheMode;
274 }
275
276 /**
277 * {@inheritDoc}
278 */
279 public TapestryPersistenceUnitInfo sharedCacheMode(final SharedCacheMode cacheMode)
280 {
281 sharedCacheMode = cacheMode;
282
283 return this;
284 }
285
286 /**
287 * {@inheritDoc}
288 */
289 public ValidationMode getValidationMode()
290 {
291 return validationMode;
292 }
293
294 /**
295 * {@inheritDoc}
296 */
297 public TapestryPersistenceUnitInfo validationMode(final ValidationMode validationMode)
298 {
299 this.validationMode = validationMode;
300
301 return this;
302 }
303
304 /**
305 * {@inheritDoc}
306 */
307 public Properties getProperties()
308 {
309 return properties;
310 }
311
312 /**
313 * {@inheritDoc}
314 */
315 public String getPersistenceXMLSchemaVersion()
316 {
317 return persistenceXMLSchemaVersion;
318 }
319
320 public void setPersistenceXMLSchemaVersion(final String version)
321 {
322 persistenceXMLSchemaVersion = version;
323 }
324
325 /**
326 * {@inheritDoc}
327 */
328 public ClassLoader getClassLoader()
329 {
330 return Thread.currentThread().getContextClassLoader();
331 }
332
333 /**
334 * {@inheritDoc}
335 */
336 public void addTransformer(final ClassTransformer transformer)
337 {
338
339 }
340
341 /**
342 * {@inheritDoc}
343 */
344 public ClassLoader getNewTempClassLoader()
345 {
346 return getClassLoader();
347 }
348
349
350 private DataSource lookupDataSource(final String name)
351 {
352 try
353 {
354 // TODO: Create InitialContext with environment properties?
355 final Context initContext = new InitialContext();
356
357 final Context envContext = (Context) initContext.lookup("java:comp/env");
358
359 return (DataSource) envContext.lookup(name);
360 } catch (final NamingException e)
361 {
362 throw new RuntimeException(e);
363 }
364
365 }
366
367 }