provider = persistenceProviderDeploymentHolder.getProvider();
} else {
provider = lookupProvider(pu);
}
final PersistenceUnitService service = new PersistenceUnitService(pu, adaptor, provider);
// add persistence provider specific properties
adaptor.addProviderProperties(properties, pu);
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 = adaptor.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.setInitialMode(ServiceController.Mode.ACTIVE)
.addInjection(service.getPropertiesInjector(), properties)
.install();
log.trace("added PersistenceUnitService for '" + puServiceName + "'. PU is ready for injector action. ");
} catch (ServiceRegistryException e) {