jtaDataSourceEnv = System.getProperty(JTADATASOURCE_PROP);
nonJtaDataSourceEnv = System.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);
// JTA Datasource
String jtaDataSourceId = info.jtaDataSource;
if (jtaDataSourceEnv != null) jtaDataSourceId = jtaDataSourceEnv;
if (jtaDataSourceId != null) {
if (System.getProperty("duct tape") == null){
try {
if (!jtaDataSourceId.startsWith("java:openejb/Resource/")
&& !jtaDataSourceId.startsWith("openejb/Resource/")) jtaDataSourceId = "openejb/Resource/"+jtaDataSourceId;
Context context = SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
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);
// 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 (System.getProperty("duct tape") == null){
try {
if (!nonJtaDataSourceId.startsWith("java:openejb/Resource/")) nonJtaDataSourceId = "java:openejb/Resource/"+nonJtaDataSourceId;
Context context = SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
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);
Class clazz = classLoader.loadClass(persistenceProviderClassName);
PersistenceProvider persistenceProvider = (PersistenceProvider) clazz.newInstance();
logger.info("assembler.buildingPersistenceUnit", unitInfo.getPersistenceUnitName(), unitInfo.getPersistenceProviderClassName(), unitInfo.getPersistenceUnitRootUrl(), unitInfo.getTransactionType());
// Create entity manager factory
EntityManagerFactory emf = persistenceProvider.createContainerEntityManagerFactory(unitInfo, new HashMap());
return emf;
}