jtaDataSourceEnv = SystemInstance.get().getProperty(JTADATASOURCE_PROP);
nonJtaDataSourceEnv = SystemInstance.get().getProperty(NON_JTADATASOURCE_PROP);
}
public EntityManagerFactory createEntityManagerFactory(PersistenceUnitInfo info, ClassLoader classLoader) throws Exception {
PersistenceUnitInfoImpl unitInfo = new PersistenceUnitInfoImpl(persistenceClassLoaderHandler);
// Persistence Unit Id
unitInfo.setId(info.id);
// Persistence Unit Name
unitInfo.setPersistenceUnitName(info.name);
// Persistence Provider Class Name
if (providerEnv != null) {
unitInfo.setPersistenceProviderClassName(providerEnv);
} else {
unitInfo.setPersistenceProviderClassName(info.provider);
}
// ClassLoader
unitInfo.setClassLoader(classLoader);
// Exclude Unlisted Classes
unitInfo.setExcludeUnlistedClasses(info.excludeUnlistedClasses);
Context context = SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
// JTA Datasource
String jtaDataSourceId = info.jtaDataSource;
if (jtaDataSourceEnv != null) jtaDataSourceId = jtaDataSourceEnv;
if (jtaDataSourceId != null) {
if (!SystemInstance.get().hasProperty("openejb.geronimo")) {
try {
if (!jtaDataSourceId.startsWith("java:openejb/Resource/")
&& !jtaDataSourceId.startsWith("openejb/Resource/")) jtaDataSourceId = "openejb/Resource/"+jtaDataSourceId;
DataSource jtaDataSource = (DataSource) context.lookup(jtaDataSourceId);
unitInfo.setJtaDataSource(jtaDataSource);
} catch (NamingException e) {
throw new OpenEJBException("Could not lookup <jta-data-source> '" + jtaDataSourceId + "' for unit '" + unitInfo.getPersistenceUnitName() + "'", e);
}
}
}
// Managed Class Names
unitInfo.setManagedClassNames(info.classes);
// Mapping File Names
unitInfo.setMappingFileNames(info.mappingFiles);
// Handle Properties
unitInfo.setProperties(info.properties);
// Schema version of the persistence.xml file
unitInfo.setPersistenceXMLSchemaVersion(info.persistenceXMLSchemaVersion);
// Second-level cache mode for the persistence unit
SharedCacheMode sharedCacheMode = Enum.valueOf(SharedCacheMode.class, info.sharedCacheMode);
unitInfo.setSharedCacheMode(sharedCacheMode);
// The validation mode to be used for the persistence unit
ValidationMode validationMode = Enum.valueOf(ValidationMode.class, info.validationMode);
unitInfo.setValidationMode(validationMode);
// Persistence Unit Transaction Type
if (transactionTypeEnv != null) {
try {
// Override with sys vars
PersistenceUnitTransactionType type = Enum.valueOf(PersistenceUnitTransactionType.class, transactionTypeEnv.toUpperCase());
unitInfo.setTransactionType(type);
} catch (IllegalArgumentException e) {
throw (IllegalArgumentException)(new IllegalArgumentException("Unknown " + TRANSACTIONTYPE_PROP + ", valid options are " + PersistenceUnitTransactionType.JTA + " or " + PersistenceUnitTransactionType.RESOURCE_LOCAL).initCause(e));
}
} else {
PersistenceUnitTransactionType type = Enum.valueOf(PersistenceUnitTransactionType.class, info.transactionType);
unitInfo.setTransactionType(type);
}
// Non JTA Datasource
String nonJtaDataSourceId = info.nonJtaDataSource;
if (nonJtaDataSourceEnv != null) nonJtaDataSourceId = nonJtaDataSourceEnv;
if (nonJtaDataSourceId != null) {
if (!SystemInstance.get().hasProperty("openejb.geronimo")) {
try {
if (!nonJtaDataSourceId.startsWith("java:openejb/Resource/")) nonJtaDataSourceId = "java:openejb/Resource/"+nonJtaDataSourceId;
DataSource nonJtaDataSource = (DataSource) context.lookup(nonJtaDataSourceId);
unitInfo.setNonJtaDataSource(nonJtaDataSource);
} catch (NamingException e) {
throw new OpenEJBException("Could not lookup <non-jta-data-source> '" + nonJtaDataSourceId + "' for unit '" + unitInfo.getPersistenceUnitName() + "'", e);
}
}
}
// Persistence Unit Root Url
unitInfo.setRootUrlAndJarUrls(info.persistenceUnitRootUrl, info.jarFiles);
// create the persistence provider
String persistenceProviderClassName = unitInfo.getPersistenceProviderClassName();
if (persistenceProviderClassName == null) {
persistenceProviderClassName = DEFAULT_PERSISTENCE_PROVIDER;
}
unitInfo.setPersistenceProviderClassName(persistenceProviderClassName);
final long start = System.nanoTime();
try {
final ExecutorService executor = Executors.newSingleThreadExecutor(new EntityManagerFactoryThreadFactory(classLoader));
final Future<EntityManagerFactory> future = executor.submit(
new EntityManagerFactoryCallable(persistenceProviderClassName, unitInfo)
);
return future.get(10, TimeUnit.MINUTES);
} finally {
final long time = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
logger.info("assembler.buildingPersistenceUnit", unitInfo.getPersistenceUnitName(), unitInfo.getPersistenceProviderClassName(), time+"");
if (logger.isDebugEnabled()) {
for (Map.Entry<Object, Object> entry : unitInfo.getProperties().entrySet()) {
logger.debug(entry.getKey() + "=" + entry.getValue());
}
}
}
}