1 23 24 25 package com.sun.enterprise.server; 26 27 import com.sun.enterprise.connectors.ConnectorRuntime; 28 import com.sun.enterprise.deployment.PersistenceUnitDescriptor; 29 import com.sun.enterprise.deployment.deploy.shared.FileArchive; 30 import com.sun.enterprise.deployment.deploy.shared.OutputJarArchive; 31 import com.sun.enterprise.loader.InstrumentableClassLoader; 32 import com.sun.enterprise.util.io.FileUtils; 33 import com.sun.enterprise.util.shared.ArchivistUtils; 34 import com.sun.logging.LogDomains; 35 36 import javax.naming.NamingException ; 37 import javax.persistence.spi.PersistenceUnitInfo; 38 import javax.persistence.spi.PersistenceUnitTransactionType; 39 import javax.persistence.spi.ClassTransformer; 40 import javax.sql.DataSource ; 41 import java.io.File ; 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.io.OutputStream ; 45 import java.net.MalformedURLException ; 46 import java.net.URL ; 47 import java.util.ArrayList ; 48 import java.util.Enumeration ; 49 import java.util.List ; 50 import java.util.Properties ; 51 import java.util.logging.Level ; 52 import java.util.logging.Logger ; 53 54 59 public class PersistenceUnitInfoImpl implements PersistenceUnitInfo { 60 61 62 private static final String DEFAULT_PROVIDER_NAME = 64 "oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider"; 66 private static final String DEFAULT_DS_NAME = "jdbc/__default"; 68 private static String defaultProvider; 70 71 private static Logger logger = LogDomains.getLogger( 72 LogDomains.LOADER_LOGGER);; 73 74 private PersistenceUnitDescriptor persistenceUnitDescriptor; 75 76 private String applicationLocation; 77 78 private File absolutePuRootFile; 79 80 private DataSource jtaDataSource; 81 82 private DataSource nonJtaDataSource; 83 84 private List <URL > jarFiles; 85 86 private InstrumentableClassLoader classLoader; 87 88 public PersistenceUnitInfoImpl( 89 PersistenceUnitDescriptor persistenceUnitDescriptor, 90 String applicationLocation, 91 InstrumentableClassLoader classLoader) { 92 this.persistenceUnitDescriptor = persistenceUnitDescriptor; 93 this.applicationLocation = applicationLocation; 94 this.classLoader = classLoader; 95 jarFiles = _getJarFiles(); 96 jtaDataSource = _getJtaDataSource(); 97 nonJtaDataSource = _getNonJtaDataSource(); 98 } 99 100 102 105 public String getPersistenceUnitName() { 106 return persistenceUnitDescriptor.getName(); 107 } 108 109 112 public String getPersistenceProviderClassName() { 113 return getPersistenceProviderClassNameForPuDesc(persistenceUnitDescriptor); 114 } 115 116 119 public PersistenceUnitTransactionType getTransactionType() { 120 return PersistenceUnitTransactionType.valueOf( 121 persistenceUnitDescriptor.getTransactionType()); 122 } 123 124 127 public DataSource getJtaDataSource() { 128 return jtaDataSource; 129 } 130 131 134 public DataSource getNonJtaDataSource() { 135 return nonJtaDataSource; 136 } 137 138 public URL getPersistenceUnitRootUrl() { 139 try { 140 return getAbsolutePuRootFile().toURI().toURL(); 141 } catch (MalformedURLException e) { 142 throw new RuntimeException (e); 143 } 144 } 145 146 149 public List <String > getMappingFileNames() { 150 return persistenceUnitDescriptor.getMappingFiles(); } 152 153 156 public List <URL > getJarFileUrls() { 157 return jarFiles; 158 } 159 160 163 public List <String > getManagedClassNames() { 164 return persistenceUnitDescriptor.getClasses(); } 166 167 public boolean excludeUnlistedClasses() { 168 return persistenceUnitDescriptor.isExcludeUnlistedClasses(); 169 } 170 171 174 public Properties getProperties() { 175 return persistenceUnitDescriptor.getProperties(); } 177 178 181 public ClassLoader getClassLoader() { 182 return ClassLoader .class.cast(classLoader); 183 } 184 185 188 public void addTransformer(ClassTransformer transformer) { 189 classLoader.addTransformer(transformer); 190 } 191 192 195 public ClassLoader getNewTempClassLoader() { 196 return classLoader.copy(); 197 } 198 199 @Override public String toString() { 200 203 StringBuilder result = new StringBuilder ("<persistence-unit>"); result.append("\n\t<PURoot>" + getPersistenceUnitRootUrl() + "</PURoot>"); result.append("\n\t<name>" + getPersistenceUnitName() + "</name>"); result.append("\n\t<provider>" + getPersistenceProviderClassName() + "</provider>"); result.append("\n\t<transaction-type>" + getTransactionType() + "</transaction-type>"); result.append("\n\t<jta-data-source>" + getJtaDataSource() + "</jta-data-source>"); result.append("\n\t<non-jta-data-source>" + getNonJtaDataSource() + "</non-jta-data-source>"); for (URL jar : getJarFileUrls()) { 215 result.append("\n\t<jar-file>" + jar + "</jar-file>"); } 217 for (String mappingFile : getMappingFileNames()) { 218 result.append("\n\t<mapping-file>" + mappingFile + "</mapping-file>"); } 221 for (String clsName : getManagedClassNames()) { 222 result.append("\n\t<class-name>" + clsName + "</class-name>"); } 224 result.append("\n\t<exclude-unlisted-classes>" + excludeUnlistedClasses() + "</exclude-unlisted-classes>"); result.append("\n\t<properties>" + getProperties() + "</properties>"); result.append("\n\t<class-loader>" + getClassLoader() + "</class-loader>"); result.append("\n</persistence-unit>\n"); return result.toString(); 231 } 232 233 protected DataSource _getJtaDataSource() { 234 238 if (getTransactionType() != PersistenceUnitTransactionType.JTA) { 239 PersistenceUnitInfoImpl.logger.logp(Level.FINE, 240 "PersistenceUnitInfoImpl", "_getJtaDataSource", "This PU is configured as non-jta, so jta-data-source is null"); return null; } 245 String DSName; 246 String userSuppliedJTADSName = persistenceUnitDescriptor.getJtaDataSource(); 247 if (!isNullOrEmpty(userSuppliedJTADSName)) { 248 DSName = userSuppliedJTADSName; } else { 250 DSName = DEFAULT_DS_NAME; 251 } 252 try { 253 logger.logp(Level.FINE, "PersistenceUnitLoaderImpl", "_getJtaDataSource", "JTADSName = {0}", DSName); 256 return DataSource .class.cast( 257 ConnectorRuntime.getRuntime().lookupPMResource(DSName)); 258 } catch (NamingException e) { 259 PersistenceUnitInfoImpl.logger.log(Level.SEVERE, 260 "Error looking up jta-datasource by name : " + DSName, e); 261 throw new RuntimeException (e); 262 } 263 } 264 265 protected DataSource _getNonJtaDataSource() { 266 278 String DSName; 279 String userSuppliedNonJTADSName = persistenceUnitDescriptor.getNonJtaDataSource(); 280 if (!isNullOrEmpty(userSuppliedNonJTADSName)) { 281 DSName = userSuppliedNonJTADSName; 282 } else { 283 String userSuppliedJTADSName = persistenceUnitDescriptor.getJtaDataSource(); 284 if (!isNullOrEmpty(userSuppliedJTADSName)) { 285 DSName = userSuppliedJTADSName; 286 } else { 287 DSName = DEFAULT_DS_NAME; 288 } 289 } 290 try { 291 logger.logp(Level.FINE, 292 "PersistenceUnitInfoImpl", "_getNonJtaDataSource", "nonJTADSName = {0}", DSName); 295 return DataSource .class.cast( 296 ConnectorRuntime.getRuntime().lookupNonTxResource(DSName)); 297 } catch (NamingException e) { 298 PersistenceUnitInfoImpl.logger.log(Level.SEVERE, 299 "Error looking up non-jta-datasource by name : " + DSName, 300 e); 301 throw new RuntimeException (e); 302 } 303 } 304 305 private List <URL > _getJarFiles() { 306 List <String > jarFileNames = new ArrayList <String >( 307 persistenceUnitDescriptor.getJarFiles()); 308 List <URL > jarFiles = new ArrayList <URL >(jarFileNames.size() + 1); 309 String absolutePuRoot = getAbsolutePuRootFile().getAbsolutePath(); 310 for (String jarFileName : jarFileNames) { 311 String nativeJarFileName = jarFileName.replace('/', 312 File.separatorChar); 313 final File parentFile = new File (absolutePuRoot).getParentFile(); 314 File jarFile = new File (parentFile, nativeJarFileName); 316 if (!jarFile.exists()) { 317 321 String pathComponent = ""; 327 String nameComponent = jarFileName; 328 if(jarFileName.lastIndexOf("../") != -1) { 329 final int separatorIndex = jarFileName.lastIndexOf("../")+3; 330 pathComponent = jarFileName.substring(0,separatorIndex); 331 nameComponent = jarFileName.substring(separatorIndex); 332 } 333 logger.fine("For jar-file="+ jarFileName+ ", " + 334 "pathComponent=" +pathComponent + 335 ", nameComponent=" + nameComponent); 336 jarFile = new File (new File (parentFile, pathComponent), 337 FileUtils.makeFriendlyFileName(nameComponent)); 338 } 339 if (jarFile.exists()) { 340 try { 341 jarFiles.add(jarFile.toURI().toURL()); 342 } catch (MalformedURLException e) { 343 throw new RuntimeException (e); 344 } 345 } else { 346 PersistenceUnitInfoImpl.logger.logp(Level.WARNING, 348 "PersistenceUnitInfoImpl", 349 "initJarFiles", "For pu-root {0}, \n" + 350 "a jar-file specified in persistence.xml is not found.\n" + 351 "Original name = {1}, and looked up name = {2}.", 352 new Object []{absolutePuRoot, jarFileName, jarFile}); 353 } 354 } 355 return jarFiles; 356 } 357 358 private File getAbsolutePuRootFile() { 359 if (absolutePuRootFile == null) { 360 absolutePuRootFile = new File (applicationLocation, 361 persistenceUnitDescriptor.getAbsolutePuRoot().replace('/', 362 File.separatorChar)); 363 if (!absolutePuRootFile.exists()) { 364 throw new RuntimeException ( 365 absolutePuRootFile.getAbsolutePath() + " does not exist!"); 366 } 367 } 368 return absolutePuRootFile; 369 } 370 371 378 private static String getDefaultprovider() { 379 final String DEFAULT_PERSISTENCE_PROVIDER_PROPERTY = 380 "com.sun.persistence.defaultProvider"; if(defaultProvider == null) { 382 defaultProvider = 383 System.getProperty(DEFAULT_PERSISTENCE_PROVIDER_PROPERTY, 384 DEFAULT_PROVIDER_NAME); 385 } 386 387 return defaultProvider; 388 } 389 390 402 private static void createJar(String sourcePath, String destinationPath) 403 throws IOException { 404 FileArchive source = new FileArchive(); 405 OutputJarArchive destination = new OutputJarArchive(); 406 try { 407 source.open(sourcePath); 408 destination.create(destinationPath); 409 for (Enumeration entries = source.entries(); 410 entries.hasMoreElements();) { 411 String entry = String .class.cast(entries.nextElement()); 412 InputStream is = null; 413 OutputStream os = null; 414 try { 415 is = source.getEntry(entry); 416 os = destination.putNextEntry(entry); 417 ArchivistUtils.copyWithoutClose(is, os); 418 } finally { 419 if (is != null) is.close(); 420 if (os != null) destination.closeEntry(); 421 } 422 } 423 } finally { 424 source.close(); 425 destination.close(); 426 } 427 } 428 429 private static boolean isNullOrEmpty(String s) { 430 return s == null || s.length() == 0; 431 } 432 433 public static String getPersistenceProviderClassNameForPuDesc( 434 PersistenceUnitDescriptor persistenceUnitDescriptor) { 435 String provider = persistenceUnitDescriptor.getProvider(); 436 if (isNullOrEmpty(provider)) { 437 provider = getDefaultprovider(); 438 } 439 return provider; 440 } 441 442 } 443 | Popular Tags |