use of javax.persistence.spi.PersistenceProvider in project wildfly by wildfly.
the class PersistenceProviderHandler method deploy.
public static void deploy(final DeploymentPhaseContext phaseContext, final Platform platform) throws DeploymentUnitProcessingException {
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();
providerList.add(provider);
JpaLogger.ROOT_LOGGER.tracef("deployment %s is using its own copy of %s", deploymentUnit.getName(), providerName);
} catch (Exception e) {
throw JpaLogger.ROOT_LOGGER.cannotDeployApp(e, providerName);
}
}
if (!providerList.isEmpty()) {
final String adapterClass = deploymentUnit.getAttachment(JpaAttachments.ADAPTOR_CLASS_NAME);
PersistenceProviderAdaptor adaptor;
if (adapterClass != null) {
try {
adaptor = (PersistenceProviderAdaptor) deploymentModuleClassLoader.loadClass(adapterClass).newInstance();
adaptor.injectJtaManager(new JtaManagerImpl(deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_SYNCHRONIZATION_REGISTRY)));
adaptor.injectPlatform(platform);
ArrayList<PersistenceProviderAdaptor> adaptorList = new ArrayList<>();
adaptorList.add(adaptor);
PersistenceProviderDeploymentHolder.savePersistenceProviderInDeploymentUnit(deploymentUnit, providerList, adaptorList);
} catch (InstantiationException e) {
throw JpaLogger.ROOT_LOGGER.cannotCreateAdapter(e, adapterClass);
} catch (IllegalAccessException e) {
throw JpaLogger.ROOT_LOGGER.cannotCreateAdapter(e, adapterClass);
} catch (ClassNotFoundException e) {
throw JpaLogger.ROOT_LOGGER.cannotCreateAdapter(e, adapterClass);
}
} else {
// register the provider (no adapter specified)
PersistenceProviderDeploymentHolder.savePersistenceProviderInDeploymentUnit(deploymentUnit, providerList, null);
}
}
}
}
use of javax.persistence.spi.PersistenceProvider in project wildfly by wildfly.
the class PersistenceProviderLoader method loadProviderModuleByName.
/**
* Loads the specified Jakarta Persistence persistence provider module
*
* @param moduleName is the static module to be loaded
* @throws ModuleLoadException
* @return list of persistence providers in specified module
*
* Note: side effect of saving loaded persistence providers to static api in javax.persistence.spi.PersistenceProvider.
*/
public static List<PersistenceProvider> loadProviderModuleByName(String moduleName) throws ModuleLoadException {
final ModuleLoader moduleLoader = Module.getBootModuleLoader();
Module module = moduleLoader.loadModule(ModuleIdentifier.fromString(moduleName));
final ServiceLoader<PersistenceProvider> serviceLoader = module.loadService(PersistenceProvider.class);
List<PersistenceProvider> result = new ArrayList<>();
if (serviceLoader != null) {
for (PersistenceProvider provider1 : serviceLoader) {
// persistence provider jar may contain multiple provider service implementations
// save each provider
PersistenceProviderResolverImpl.getInstance().addPersistenceProvider(provider1);
result.add(provider1);
}
}
return result;
}
use of javax.persistence.spi.PersistenceProvider in project tomee by apache.
the class JpaTest method createEntityManagerFactory.
private EntityManagerFactory createEntityManagerFactory() throws Exception {
final PersistenceClassLoaderHandler persistenceClassLoaderHandler = new PersistenceClassLoaderHandler() {
public void addTransformer(final String unitId, final ClassLoader classLoader, final ClassFileTransformer classFileTransformer) {
/*
Instrumentation instrumentation = Agent.getInstrumentation();
instrumentation.addTransformer(classFileTransformer);
*/
}
public void destroy(final String unitId) {
}
public ClassLoader getNewTempClassLoader(final ClassLoader classLoader) {
return new TempClassLoader(classLoader);
}
};
/*
Agent.getInstrumentation().addTransformer(new ClassFileTransformer() {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
if (!className.equals("org/apache/openejb/core/cmp/jpa/Employee")) {
return null;
}
byte[] newBytes = addNewField(classfileBuffer);
return newBytes;
}
});
*/
final PersistenceUnitInfoImpl unitInfo = new PersistenceUnitInfoImpl(persistenceClassLoaderHandler);
unitInfo.setPersistenceUnitName("CMP");
unitInfo.setPersistenceProviderClassName(PERSISTENCE_PROVIDER);
unitInfo.setClassLoader(getClass().getClassLoader());
unitInfo.setExcludeUnlistedClasses(false);
unitInfo.setJtaDataSource(jtaDs);
unitInfo.setNonJtaDataSource(nonJtaDs);
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.Employee");
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.Bill");
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.EmbeddedBill");
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.Person");
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.EmbeddedBillPk");
unitInfo.getMappingFileNames().add("META-INF/jpa-test-mappings.xml");
// Handle Properties
final Properties properties = new Properties();
properties.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
properties.setProperty("openjpa.Log", "DefaultLevel=WARN");
properties.setProperty("openjpa.RuntimeUnenhancedClasses", "supported");
unitInfo.setProperties(properties);
unitInfo.setTransactionType(transactionType);
unitInfo.getManagedClassNames().add("org.apache.openejb.core.cmp.jpa.Employee");
final PersistenceProvider persistenceProvider = (PersistenceProvider) getClass().getClassLoader().loadClass(PERSISTENCE_PROVIDER).newInstance();
final EntityManagerFactory emf = persistenceProvider.createContainerEntityManagerFactory(unitInfo, new HashMap());
return emf;
}
use of javax.persistence.spi.PersistenceProvider in project tomee by apache.
the class UnenhancedTest method runTest.
private void runTest(final String methodName, final PersistenceUnitTransactionType transactionType, final boolean enhance) throws Exception {
this.enhance = enhance;
final ClassLoader loader = new FilteredChildFirstClassLoader(getClass().getClassLoader(), "org.apache.openejb.core.cmp.jpa");
final PersistenceClassLoaderHandler persistenceClassLoaderHandler = new PersistenceClassLoaderHandler() {
public void addTransformer(final String unitId, final ClassLoader classLoader, final ClassFileTransformer classFileTransformer) {
final Instrumentation instrumentation = Agent.getInstrumentation();
if (instrumentation != null) {
instrumentation.addTransformer(new ControllableTransformer(classFileTransformer));
}
}
public void destroy(final String unitId) {
}
public ClassLoader getNewTempClassLoader(final ClassLoader classLoader) {
return new TempClassLoader(classLoader);
}
};
final PersistenceUnitInfoImpl unitInfo = new PersistenceUnitInfoImpl(persistenceClassLoaderHandler);
unitInfo.setPersistenceUnitName("CMP");
unitInfo.setPersistenceProviderClassName(PERSISTENCE_PROVIDER);
unitInfo.setClassLoader(loader);
unitInfo.setExcludeUnlistedClasses(false);
unitInfo.setJtaDataSource(jtaDs);
unitInfo.setNonJtaDataSource(nonJtaDs);
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.ComplexSuperclass");
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.ComplexSubclass");
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.ComplexStandalone");
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.GeneratedStandalone");
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.GeneratedSuperclass");
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.GeneratedSubclass");
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.OneStandalone");
unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.ManyStandalone");
// Handle Properties
final Properties properties = new Properties();
properties.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(SchemaAction='add,deleteTableContents',ForeignKeys=true)");
properties.setProperty("openjpa.RuntimeUnenhancedClasses", "supported");
properties.setProperty("openjpa.Log", "DefaultLevel=INFO");
unitInfo.setProperties(properties);
unitInfo.setTransactionType(transactionType);
unitInfo.getManagedClassNames().add("org.apache.openejb.core.cmp.jpa.Employee");
final PersistenceProvider persistenceProvider = (PersistenceProvider) getClass().getClassLoader().loadClass(PERSISTENCE_PROVIDER).newInstance();
entityManagerFactory = persistenceProvider.createContainerEntityManagerFactory(unitInfo, new HashMap());
// create the test object (via reflection)
final Object testObject = loader.loadClass("org.apache.openejb.core.cmp.jpa.UnenhancedUnits").newInstance();
set(testObject, "TransactionManager", TransactionManager.class, transactionManager);
set(testObject, "EntityManagerFactory", EntityManagerFactory.class, entityManagerFactory);
// invoke the test (via reflection)
Thread.currentThread().setContextClassLoader(loader);
invoke(testObject, "setUp");
try {
invoke(testObject, methodName);
} finally {
invoke(testObject, "tearDown");
}
}
use of javax.persistence.spi.PersistenceProvider in project meecrowave by apache.
the class EntityManagerBean method init.
void init(final PersistenceUnitInfo info, final BeanManager bm) {
final PersistenceProvider provider;
try {
provider = PersistenceProvider.class.cast(Thread.currentThread().getContextClassLoader().loadClass(info.getPersistenceProviderClassName()).newInstance());
} catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new IllegalArgumentException("Bad provider: " + info.getPersistenceProviderClassName());
}
final EntityManagerFactory factory = provider.createContainerEntityManagerFactory(info, new HashMap() {
{
put("javax.persistence.bean.manager", bm);
if (ValidationMode.NONE != info.getValidationMode()) {
ofNullable(findValidatorFactory(bm)).ifPresent(factory -> put("javax.persistence.validation.factory", factory));
}
}
});
instanceFactory = synchronization == SynchronizationType.SYNCHRONIZED ? factory::createEntityManager : () -> factory.createEntityManager(synchronization);
}
Aggregations