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.Map;
033 import java.util.Properties;
034 import java.util.Set;
035
036 public class PersistenceUnitInfoImpl implements TapestryPersistenceUnitInfo
037 {
038 private String persistenceUnitName;
039
040 private String persistenceProviderClassName;
041
042 private String persistenceXMLSchemaVersion;
043
044 private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
045
046 private DataSource nonJtaDataSource;
047
048 private DataSource jtaDataSource;
049
050 private ValidationMode validationMode;
051
052 private SharedCacheMode sharedCacheMode;
053
054 private boolean excludeUnlistedClasses = true;
055
056 private final Set<String> managedClassNames = CollectionFactory.newSet();
057
058 private final Set<String> mappingFilesNames = CollectionFactory.newSet();
059
060 private final List<URL> jarFileUrls = CollectionFactory.newList();
061
062 private final Properties properties = new Properties();
063
064 private Map entityManagerProperties;
065
066 public PersistenceUnitInfoImpl(String persistenceUnitName)
067 {
068 this.persistenceUnitName = persistenceUnitName;
069 }
070
071 /**
072 * {@inheritDoc}
073 */
074 public String getPersistenceUnitName()
075 {
076 return persistenceUnitName;
077 }
078
079 /**
080 * {@inheritDoc}
081 */
082 public String getPersistenceProviderClassName()
083 {
084 return persistenceProviderClassName;
085 }
086
087 /**
088 * {@inheritDoc}
089 */
090 public TapestryPersistenceUnitInfo persistenceProviderClassName(final String persistenceProviderClassName)
091 {
092 this.persistenceProviderClassName = persistenceProviderClassName;
093
094 return this;
095 }
096
097 /**
098 * {@inheritDoc}
099 */
100 public PersistenceUnitTransactionType getTransactionType()
101 {
102 return transactionType;
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 public TapestryPersistenceUnitInfo transactionType(final PersistenceUnitTransactionType transactionType)
109 {
110 this.transactionType = transactionType;
111
112 return this;
113 }
114
115 /**
116 * {@inheritDoc}
117 */
118 public DataSource getJtaDataSource()
119 {
120 return jtaDataSource;
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 public DataSource getNonJtaDataSource()
127 {
128 return nonJtaDataSource;
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 public TapestryPersistenceUnitInfo nonJtaDataSource(final String nonJtaDataSource)
135 {
136 this.nonJtaDataSource = lookupDataSource(nonJtaDataSource);
137
138 return this;
139 }
140
141 /**
142 * {@inheritDoc}
143 */
144 public TapestryPersistenceUnitInfo jtaDataSource(final String jtaDataSource)
145 {
146 this.jtaDataSource = lookupDataSource(jtaDataSource);
147
148 return this;
149 }
150
151 /**
152 * {@inheritDoc}
153 */
154 public List<String> getMappingFileNames()
155 {
156 List<String> tmp = CollectionFactory.newList(mappingFilesNames);
157 return Collections.unmodifiableList(tmp);
158 }
159
160 /**
161 * {@inheritDoc}
162 */
163 public TapestryPersistenceUnitInfo addMappingFileName(final String fileName)
164 {
165 mappingFilesNames.add(fileName);
166
167 return this;
168
169 }
170
171 /**
172 * {@inheritDoc}
173 */
174 public TapestryPersistenceUnitInfo addJarFileUrl(URL url)
175 {
176 jarFileUrls.add(url);
177
178 return this;
179 }
180
181 /**
182 * {@inheritDoc}
183 */
184 public TapestryPersistenceUnitInfo addJarFileUrl(String url)
185 {
186 try
187 {
188 return addJarFileUrl(new URL(getPersistenceUnitRootUrl(), url));
189 } catch (MalformedURLException e)
190 {
191 throw new RuntimeException(e);
192 }
193 }
194
195
196 /**
197 * {@inheritDoc}
198 */
199 public TapestryPersistenceUnitInfo addProperty(String name, String value)
200 {
201 getProperties().put(name, value);
202
203 return this;
204 }
205
206
207 /**
208 * {@inheritDoc}
209 */
210 public TapestryPersistenceUnitInfo excludeUnlistedClasses(boolean exclude)
211 {
212 this.excludeUnlistedClasses = exclude;
213
214 return this;
215 }
216
217 /**
218 * {@inheritDoc}
219 */
220 public List<URL> getJarFileUrls()
221 {
222 return Collections.unmodifiableList(jarFileUrls);
223 }
224
225 /**
226 * {@inheritDoc}
227 */
228 public URL getPersistenceUnitRootUrl()
229 {
230 return getClass().getResource("/");
231 }
232
233 /**
234 * {@inheritDoc}
235 */
236 public List<String> getManagedClassNames()
237 {
238 List<String> tmp = CollectionFactory.newList(managedClassNames);
239 return Collections.<String>unmodifiableList(tmp);
240 }
241
242 /**
243 * {@inheritDoc}
244 */
245 public TapestryPersistenceUnitInfo addManagedClassName(final String className)
246 {
247 managedClassNames.add(className);
248
249 return this;
250 }
251
252 /**
253 * {@inheritDoc}
254 */
255 public TapestryPersistenceUnitInfo addManagedClass(final Class<?> clazz)
256 {
257 addManagedClassName(clazz.getName());
258
259 return this;
260 }
261
262 /**
263 * {@inheritDoc}
264 */
265 public boolean excludeUnlistedClasses()
266 {
267 return excludeUnlistedClasses;
268 }
269
270 /**
271 * {@inheritDoc}
272 */
273 public SharedCacheMode getSharedCacheMode()
274 {
275 return sharedCacheMode;
276 }
277
278 /**
279 * {@inheritDoc}
280 */
281 public TapestryPersistenceUnitInfo sharedCacheMode(final SharedCacheMode cacheMode)
282 {
283 sharedCacheMode = cacheMode;
284
285 return this;
286 }
287
288 /**
289 * {@inheritDoc}
290 */
291 public ValidationMode getValidationMode()
292 {
293 return validationMode;
294 }
295
296 /**
297 * {@inheritDoc}
298 */
299 public TapestryPersistenceUnitInfo validationMode(final ValidationMode validationMode)
300 {
301 this.validationMode = validationMode;
302
303 return this;
304 }
305
306 /**
307 * {@inheritDoc}
308 */
309 public Properties getProperties()
310 {
311 return properties;
312 }
313
314 /**
315 * {@inheritDoc}
316 */
317 public String getPersistenceXMLSchemaVersion()
318 {
319 return persistenceXMLSchemaVersion;
320 }
321
322 public void setPersistenceXMLSchemaVersion(final String version)
323 {
324 persistenceXMLSchemaVersion = version;
325 }
326
327 /**
328 * {@inheritDoc}
329 */
330 public ClassLoader getClassLoader()
331 {
332 return Thread.currentThread().getContextClassLoader();
333 }
334
335 /**
336 * {@inheritDoc}
337 */
338 public void addTransformer(final ClassTransformer transformer)
339 {
340
341 }
342
343 /**
344 * {@inheritDoc}
345 */
346 public ClassLoader getNewTempClassLoader()
347 {
348 return getClassLoader();
349 }
350
351
352 private DataSource lookupDataSource(final String name)
353 {
354 try
355 {
356 // TODO: Create InitialContext with environment properties?
357 final Context initContext = new InitialContext();
358
359 final Context envContext = (Context) initContext.lookup("java:comp/env");
360
361 return (DataSource) envContext.lookup(name);
362 } catch (final NamingException e)
363 {
364 throw new RuntimeException(e);
365 }
366
367 }
368
369 public TapestryPersistenceUnitInfo setEntityManagerProperties(Map properties) {
370 entityManagerProperties = properties;
371 return this;
372 }
373
374 public Map getEntityManagerProperties() {
375 return entityManagerProperties;
376 }
377
378
379 }