public PersistenceBuilder(PersistenceClassLoaderHandler persistenceClassLoaderHandler) {
this.persistenceClassLoaderHandler = persistenceClassLoaderHandler;
}
public ReloadableEntityManagerFactory 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
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;
unitInfo.setJtaDataSourceName(jtaDataSourceId);
if (jtaDataSourceId != null) {
if (!SystemInstance.get().hasProperty("openejb.geronimo")) {
String initialJndiName = jtaDataSourceId;
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) {
try {
unitInfo.setJtaDataSource((DataSource) new InitialContext().lookup(initialJndiName));
} catch (NamingException ne) {
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
PersistenceUnitTransactionType type = Enum.valueOf(PersistenceUnitTransactionType.class, info.transactionType);
unitInfo.setTransactionType(type);
// Non JTA Datasource
String nonJtaDataSourceId = info.nonJtaDataSource;
unitInfo.setNonJtaDataSourceName(nonJtaDataSourceId);
if (nonJtaDataSourceId != null) {
if (!SystemInstance.get().hasProperty("openejb.geronimo")) {
String initialJndiName = nonJtaDataSourceId;
try {
if (!nonJtaDataSourceId.startsWith("java:openejb/Resource/")) nonJtaDataSourceId = "java:openejb/Resource/"+nonJtaDataSourceId;
DataSource nonJtaDataSource = (DataSource) context.lookup(nonJtaDataSourceId);
unitInfo.setNonJtaDataSource(nonJtaDataSource);
} catch (NamingException e) {
try {
unitInfo.setNonJtaDataSource((DataSource) new InitialContext().lookup(initialJndiName));
} catch (NamingException ne) {
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();
unitInfo.setPersistenceProviderClassName(persistenceProviderClassName);
final long start = System.nanoTime();
try {
final EntityManagerFactoryCallable callable = new EntityManagerFactoryCallable(persistenceProviderClassName, unitInfo, classLoader);
return new ReloadableEntityManagerFactory(classLoader, callable, unitInfo.getProperties());
} 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());
}
}
}
}