if (logger.isInfoEnabled() && map != null) {
logger.info("Extra container PersistenceUnitInfo properties: " + map);
}
String name = unit.getPersistenceUnitName();
DataDomain domain = configuration.getDomain(name);
// TODO: andrus, 2/3/2007 - considering property overrides, it may be a bad idea
// to cache domains. Essentially we are caching a PersistenceUnitInfo with a given
// name, without a possibility to refresh it. But maybe this is ok...?
if (domain == null) {
long t0 = System.currentTimeMillis();
boolean isJTA = isJta(unit, map);
EntityMapLoader loader = new EntityMapLoader(unit);
// add transformer before DataMapConverter starts loading the classes via app
// class loader
ClassFileTransformer enhancer = new Enhancer(new JpaEnhancerVisitorFactory(
loader.getEntityMap()));
Map<String, JpaClassDescriptor> managedClasses = loader
.getEntityMap()
.getManagedClasses();
unit.addTransformer(new UnitClassTransformer(managedClasses, loader
.getContext()
.getTempClassLoader(), enhancer));
DataMapConverter converter = new DataMapConverter();
DataMap cayenneMap = converter.toDataMap(name, loader.getContext());
// configure Cayenne domain
domain = new DataDomain(name);
ClassDescriptorMap descriptors = domain
.getEntityResolver()
.getClassDescriptorMap();
FaultFactory faultFactory = new SingletonFaultFactory();
descriptors.addFactory(new JpaClassDescriptorFactory(
loader.getEntityMap(),
descriptors,
faultFactory));
configuration.addDomain(domain);
// TODO: andrus, 2/3/2007 - clarify this logic.... JTA EM may not always mean
// JTA DS?
DataSource dataSource = isJTA ? unit.getJtaDataSource() : unit
.getNonJtaDataSource();
if (dataSource == null) {
String jta = isJTA ? "JTA" : "non-JTA";
logger.warn("NULL "
+ jta
+ " DataSource returned from PersistenceUnitInfo");
}
DbAdapter adapter = createCustomAdapter(loader.getContext(), unit);
DataNode node = new DataNode(name);
if (adapter == null) {
adapter = new AutoAdapter(new NodeDataSource(node));
}
node.setAdapter(adapter);
node.setDataSource(dataSource);
node.addDataMap(cayenneMap);
domain.addNode(node);
// note that for now we do not apply object layer defaults, as that would
// require extra enhancement for runtime relationships...
domain.getEntityResolver().applyDBLayerDefaults();
domain.setUsingExternalTransactions(isJTA);
if ("true".equalsIgnoreCase(unit.getProperties().getProperty(
DROP_SCHEMA_PROPERTY))) {
dropSchema(dataSource, adapter, cayenneMap);
}
if ("true".equalsIgnoreCase(unit.getProperties().getProperty(
CREATE_SCHEMA_PROPERTY))) {
loadSchema(dataSource, adapter, cayenneMap);
}
long t1 = System.currentTimeMillis();
// report conflicts...
ValidationResult conflicts = loader.getContext().getConflicts();
if (conflicts.hasFailures()) {
for (Object failure : conflicts.getFailures()) {
logger.info("*** mapping conflict: " + failure);
}
}
if (logger.isDebugEnabled()) {
logger.debug("loaded persistence unit '"
+ name
+ "' in "
+ (t1 - t0)
+ " ms.");
}
}
// see TODO above - JTA vs RESOURCE_LOCAL is cached per domain... maybe need to
// change that
return domain.isUsingExternalTransactions() ? new JtaEntityManagerFactory(
this,
domain,
unit) : new ResourceLocalEntityManagerFactory(this, domain, unit);
}