use of org.eclipse.persistence.internal.jpa.config.persistenceunit.MetadataSource in project eclipselink by eclipse-ee4j.
the class RuntimeFactory method createEntityManagerFactory.
public EntityManagerFactory createEntityManagerFactory(PersistenceUnit pu) {
EntityManagerSetupImpl emSetupImpl = null;
boolean isNew = false;
// the name that uniquely defines persistence unit
String name = pu.getName();
pu.setProperty(PersistenceUnitProperties.METADATA_SOURCE, new MetadataSource(pu));
SEPersistenceUnitInfo puInfo = (SEPersistenceUnitInfo) pu.getPersistenceUnitInfo();
JPAInitializer initializer = new PersistenceProvider().getInitializer(name, null);
Map<String, Object> props = new HashMap<String, Object>();
String uniqueName = initializer.createUniquePersistenceUnitName(puInfo);
String sessionName = EntityManagerSetupImpl.getOrBuildSessionName(props, puInfo, uniqueName);
try {
synchronized (EntityManagerFactoryProvider.emSetupImpls) {
emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
if (emSetupImpl != null) {
if (puInfo.getClassLoader() != emSetupImpl.getPersistenceUnitInfo().getClassLoader()) {
emSetupImpl.undeploy();
EntityManagerFactoryProvider.emSetupImpls.remove(sessionName);
SessionManager manager = SessionManager.getManager();
if (manager.getSessions().containsKey(sessionName)) {
manager.destroySession(sessionName);
}
manager.destroy();
emSetupImpl = null;
}
}
if (emSetupImpl == null) {
// there may be initial emSetupImpl cached in Initializer - remove it and use.
emSetupImpl = initializer.extractInitialEmSetupImpl(name);
if (emSetupImpl == null) {
// create and predeploy a new emSetupImpl
emSetupImpl = initializer.callPredeploy(puInfo, props, uniqueName, sessionName);
} else {
// change the name
emSetupImpl.changeSessionName(sessionName);
}
// emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), props);
EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName, emSetupImpl);
isNew = true;
}
}
} catch (Exception e) {
throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(initializer.getInitializationClassLoader(), e);
}
if (!isNew) {
if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName())) {
throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName, uniqueName, emSetupImpl.getPersistenceUnitUniqueName());
}
// synchronized to prevent undeploying by other threads.
boolean undeployed = false;
synchronized (emSetupImpl) {
if (emSetupImpl.isUndeployed()) {
undeployed = true;
}
// emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), props);
}
if (undeployed) {
// start all over again.
return createEntityManagerFactory(pu);
}
}
EntityManagerFactoryImpl factory = null;
try {
factory = new EntityManagerFactoryImpl(emSetupImpl, props);
// actually calling createEntityManager
if (emSetupImpl.shouldGetSessionOnCreateFactory(props)) {
factory.getDatabaseSession();
}
} catch (RuntimeException ex) {
if (factory != null) {
factory.close();
} else {
emSetupImpl.undeploy();
}
throw ex;
}
return factory;
}
Aggregations