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