final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
final ServicesAttachment servicesAttachment = deploymentUnit.getAttachment(Attachments.SERVICES);
if (module != null && servicesAttachment != null) {
final ModuleClassLoader deploymentModuleClassLoader = module.getClassLoader();
PersistenceProvider provider;
// collect list of persistence providers packaged with the application
final List<String> providerNames = servicesAttachment.getServiceImplementations(PERSISTENCE_PROVIDER_CLASSNAME);
List<PersistenceProvider> providerList = new ArrayList<PersistenceProvider>();
for (String providerName : providerNames) {
try {
final Class<? extends PersistenceProvider> providerClass = deploymentModuleClassLoader.loadClass(providerName).asSubclass(PersistenceProvider.class);
final Constructor<? extends PersistenceProvider> constructor = providerClass.getConstructor();
provider = constructor.newInstance();
Set<ClassLoader> deploymentClassLoaders = allDeploymentModuleClassLoaders(deploymentUnit);
ROOT_LOGGER.debugf("Deployment has its own persistence provider %s associated with classloaders %s", providerClass, deploymentClassLoaders.toString());
// register persistence provider so javax.persistence.Persistence.createEntityManagerFactory can find it
PersistenceProviderResolverImpl.getInstance().addDeploymentSpecificPersistenceProvider(provider, deploymentClassLoaders);
// TODO: instead of passing provider in via PersistenceProviderDeploymentHolder,
// it should instead be looked up via PersistenceProviderResolverImpl (still need to pass adapter in
// or add mechanism to deal with it on a per deployment basis).
providerList.add(provider);
} catch (Exception e) {
throw MESSAGES.cannotDeployApp(e, providerName);
}
}
if (providerList.size() > 0) {
final String adapterClass = deploymentUnit.getAttachment(JpaAttachments.ADAPTOR_CLASS_NAME);
PersistenceProviderAdaptor adaptor;
if (adapterClass != null) {
try {
adaptor = (PersistenceProviderAdaptor) deploymentModuleClassLoader.loadClass(adapterClass).newInstance();
adaptor.injectJtaManager(JtaManagerImpl.getInstance());
deploymentUnit.putAttachment(JpaAttachments.DEPLOYED_PERSISTENCE_PROVIDER, new PersistenceProviderDeploymentHolder(providerList, adaptor));
} catch (InstantiationException e) {
throw MESSAGES.cannotCreateAdapter(e, adapterClass);
} catch (IllegalAccessException e) {