for (PersistenceUnitMetadataHolder holder : puList) {
for (PersistenceUnitMetadata pu : holder.getPersistenceUnits()) {
pu.setClassLoader(classLoader);
pu.setTempClassloader(new TempClassLoader(classLoader));
try {
final PersistenceUnitService service = new PersistenceUnitService(pu);
// TODO: move this to a standalone service
final Injector<TransactionManager> transactionManagerInjector =
new Injector<TransactionManager>() {
public void inject(final TransactionManager value) throws InjectionException {
TransactionUtil.setTransactionManager(value);
}
public void uninject() {
// injector.uninject();
}
};
final Injector<TransactionSynchronizationRegistry> transactionRegistryInjector =
new Injector<TransactionSynchronizationRegistry>() {
public void inject(final TransactionSynchronizationRegistry value) throws
InjectionException {
TransactionUtil.setTransactionSynchronizationRegistry(value);
}
public void uninject() {
// injector.uninject();
}
};
final HashMap properties = new HashMap();
if (!ValidationMode.NONE.equals(pu.getValidationMode())) {
ValidatorFactory validatorFactory = SerializableValidatorFactory.getINSTANCE();
properties.put("javax.persistence.validation.factory", validatorFactory);
}
addProviderProperties(pu, properties);
final ServiceName puServiceName = PersistenceUnitService.getPUServiceName(pu);
// add the PU service as a dependency to all EE components in this scope
this.addPUServiceDependencyToComponents(components, puServiceName);
deploymentUnit.addToAttachmentList(Attachments.WEB_DEPENDENCIES, puServiceName);
ServiceBuilder builder = serviceTarget.addService(puServiceName, service);
boolean useDefaultDataSource = true;
final String jtaDataSource = adjustJndi(pu.getJtaDataSourceName());
final String nonJtaDataSource = adjustJndi(pu.getNonJtaDataSourceName());
if (jtaDataSource != null) {
if (jtaDataSource.startsWith("java:")) {
builder.addDependency(ContextNames.serviceNameOfContext(eeModuleDescription.getApplicationName(), eeModuleDescription.getModuleName(), eeModuleDescription.getModuleName(), jtaDataSource), ManagedReferenceFactory.class, new ManagedReferenceFactoryInjector(service.getJtaDataSourceInjector()));
useDefaultDataSource = false;
} else {
builder.addDependency(AbstractDataSourceService.SERVICE_NAME_BASE.append(jtaDataSource), new CastingInjector<DataSource>(service.getJtaDataSourceInjector(), DataSource.class));
useDefaultDataSource = false;
}
}
if (nonJtaDataSource != null) {
if (nonJtaDataSource.startsWith("java:")) {
builder.addDependency(ContextNames.serviceNameOfContext(eeModuleDescription.getApplicationName(), eeModuleDescription.getModuleName(), eeModuleDescription.getModuleName(), nonJtaDataSource), ManagedReferenceFactory.class, new ManagedReferenceFactoryInjector(service.getNonJtaDataSourceInjector()));
useDefaultDataSource = false;
} else {
builder.addDependency(AbstractDataSourceService.SERVICE_NAME_BASE.append(nonJtaDataSource), new CastingInjector<DataSource>(service.getNonJtaDataSourceInjector(), DataSource.class));
useDefaultDataSource = false;
}
}
// JPA 2.0 8.2.1.5, container provides default JTA datasource
if (useDefaultDataSource) {
final String defaultJtaDataSource = adjustJndi(JPAService.getDefaultDataSourceName());
if (defaultJtaDataSource != null &&
defaultJtaDataSource.length() > 0) {
builder.addDependency(AbstractDataSourceService.SERVICE_NAME_BASE.append(defaultJtaDataSource), new CastingInjector<DataSource>(service.getJtaDataSourceInjector(), DataSource.class));
log.trace(puServiceName + " is using the default data source '" + defaultJtaDataSource + "'");
}
}
Iterable<ServiceName> providerDependencies = getProviderDependencies(pu);
if (providerDependencies != null) {
builder.addDependencies(providerDependencies);
}
if (pu.getProperties().containsKey(JNDI_PROPERTY)) {
String jndiName = pu.getProperties().get(JNDI_PROPERTY).toString();
final ServiceName bindingServiceName = ContextNames.serviceNameOfContext(eeModuleDescription.getApplicationName(), eeModuleDescription.getModuleName(), eeModuleDescription.getModuleName(), jndiName);
final BinderService binderService = new BinderService(jndiName);
serviceTarget.addService(bindingServiceName, binderService)
.addDependency(ContextNames.serviceNameOfNamingStore(eeModuleDescription.getApplicationName(), eeModuleDescription.getModuleName(), eeModuleDescription.getModuleName(), jndiName), NamingStore.class, binderService.getNamingStoreInjector())
.addDependency(puServiceName, PersistenceUnitService.class, new Injector<PersistenceUnitService>() {
@Override
public void inject(final PersistenceUnitService value) throws InjectionException {
binderService.getManagedObjectInjector().inject(new ValueManagedReferenceFactory(new ImmediateValue<Object>(value.getEntityManagerFactory())));
}
@Override
public void uninject() {
binderService.getNamingStoreInjector().uninject();
}
}).install();
}
builder.addDependency(TransactionManagerService.SERVICE_NAME, new CastingInjector<TransactionManager>(transactionManagerInjector, TransactionManager.class))
.addDependency(TransactionSynchronizationRegistryService.SERVICE_NAME, new CastingInjector<TransactionSynchronizationRegistry>(transactionRegistryInjector, TransactionSynchronizationRegistry.class))
.setInitialMode(ServiceController.Mode.ACTIVE)
.addInjection(service.getPropertiesInjector(), properties)
.install();
log.trace("added PersistenceUnitService for '" + puServiceName + "'. PU is ready for injector action. ");
} catch (ServiceRegistryException e) {