use of javax.persistence.spi.PersistenceProvider in project wildfly by wildfly.
the class PersistenceProviderHandler method finishDeploy.
public static void finishDeploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
PersistenceProviderDeploymentHolder persistenceProviderDeploymentHolder = PersistenceProviderDeploymentHolder.getPersistenceProviderDeploymentHolder(deploymentUnit);
Map<String, PersistenceProvider> providerMap = persistenceProviderDeploymentHolder != null ? persistenceProviderDeploymentHolder.getProviders() : null;
if (providerMap != null) {
Set<ClassLoader> deploymentClassLoaders = allDeploymentModuleClassLoaders(deploymentUnit);
synchronized (providerMap) {
for (Map.Entry<String, PersistenceProvider> kv : providerMap.entrySet()) {
PersistenceProviderResolverImpl.getInstance().addDeploymentSpecificPersistenceProvider(kv.getValue(), deploymentClassLoaders);
}
}
}
}
use of javax.persistence.spi.PersistenceProvider in project tomee by apache.
the class EntityManagerFactoryCallable method call.
@Override
public EntityManagerFactory call() throws Exception {
final ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(appClassLoader);
try {
final Class<?> clazz = appClassLoader.loadClass(persistenceProviderClassName);
final PersistenceProvider persistenceProvider = (PersistenceProvider) clazz.newInstance();
// Create entity manager factories with the validator factory
final Map<String, Object> properties = new HashMap<>();
if (!ValidationMode.NONE.equals(unitInfo.getValidationMode())) {
properties.put("javax.persistence.validation.factory", // optim to avoid lookups
potentialValidators != null && potentialValidators.size() == 1 ? ensureSerializable(potentialValidators.values().iterator().next()) : new ValidatorFactoryWrapper(potentialValidators));
}
if (cdi && "true".equalsIgnoreCase(unitInfo.getProperties().getProperty("tomee.jpa.cdi", "true")) && "true".equalsIgnoreCase(SystemInstance.get().getProperty("tomee.jpa.cdi", "true"))) {
properties.put("javax.persistence.bean.manager", Proxy.newProxyInstance(appClassLoader, new Class<?>[] { BeanManager.class }, new BmHandler()));
}
customizeProperties(properties);
// ensure no tx is there cause a managed connection would fail if the provider setAutocCommit(true) and some hib* have this good idea
final Transaction transaction;
if (unitInfo.isLazilyInitialized()) {
transaction = OpenEJB.getTransactionManager().suspend();
} else {
transaction = null;
}
final EntityManagerFactory emf;
try {
emf = persistenceProvider.createContainerEntityManagerFactory(unitInfo, properties);
} finally {
if (unitInfo.isLazilyInitialized() && transaction != null) {
OpenEJB.getTransactionManager().resume(transaction);
}
}
if (unitInfo.getProperties() != null && "true".equalsIgnoreCase(unitInfo.getProperties().getProperty(OPENEJB_JPA_INIT_ENTITYMANAGER)) || SystemInstance.get().getOptions().get(OPENEJB_JPA_INIT_ENTITYMANAGER, false)) {
emf.createEntityManager().close();
}
if (unitInfo.getNonJtaDataSource() != null) {
final ImportSql importer = new ImportSql(appClassLoader, unitInfo.getPersistenceUnitName(), unitInfo.getNonJtaDataSource());
if (importer.hasSomethingToImport()) {
// to let OpenJPA create the database if configured this way
emf.createEntityManager().close();
importer.doImport();
}
}
return emf;
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
use of javax.persistence.spi.PersistenceProvider in project tomee by apache.
the class PersistenceBootstrap method bootstrap.
public static void bootstrap(final ClassLoader classLoader) {
if (classLoader == PersistenceBootstrap.class.getClassLoader()) /*virtual system loader*/
{
// no need to sync it otherwise far worse things should get sync
if (systemDone) {
return;
}
systemDone = true;
}
final Properties args = getAgentArgs(classLoader);
debug = args.getProperty("debug", "false").equalsIgnoreCase("true");
final boolean enabled = args.getProperty("enabled", "true").equalsIgnoreCase("true");
if (!enabled) {
debug("disabled");
return;
}
try {
debug("searching for persistence.xml files");
// create persistence.xml names respecting altdd
final Collection<String> pXmlNames = new ArrayList<>();
// altdd logic duplicated to avoid classloading issue in tomee-webapp mode
final String altDD = getAltDD();
if (altDD != null) {
for (final String p : altDD.split(",")) {
pXmlNames.add(p + ".persistence.xml");
pXmlNames.add(p + "-persistence.xml");
}
} else {
pXmlNames.add("persistence.xml");
}
final List<URL> urls = new LinkedList<>();
for (final String pXmlName : pXmlNames) {
// find persistence.xml in the classloader and in WEB-INF
urls.addAll(Collections.list(classLoader.getResources("META-INF/" + pXmlName)));
if ("true".equals(args.getProperty("web-scan", "false"))) {
// findUrls is slow for small tests and rarely needed
try {
final Collection<URL> loaderUrls = findUrls(classLoader, args);
for (final URL url : loaderUrls) {
final File file = toFile(url);
if ("classes".equals(file.getName()) && "WEB-INF".equals(file.getParentFile().getName())) {
final File pXml = new File(file.getParentFile(), pXmlName);
if (pXml.exists()) {
urls.add(pXml.toURI().toURL());
}
break;
}
if (file.getName().endsWith(".jar") && file.getParentFile().getName().equals("lib") && "WEB-INF".equals(file.getParentFile().getParentFile().getName())) {
final File pXml = new File(file.getParentFile().getParentFile(), pXmlName);
if (pXml.exists()) {
urls.add(pXml.toURI().toURL());
}
break;
}
}
} catch (final Throwable th) {
// no-op
}
}
}
if (urls.size() == 0) {
debug("no persistence.xml files found");
return;
}
final Map<String, Unit> units = new HashMap<>();
for (final URL url : urls) {
final String urlPath = url.toExternalForm();
debug("found " + urlPath);
try {
final InputStream in = IO.read(url);
try {
collectUnits(in, units, args);
} catch (final Throwable e) {
debug("failed to read " + urlPath, e);
in.close();
}
} catch (final Throwable e) {
debug("failed to read " + urlPath, e);
}
}
for (final Unit unit : units.values()) {
final String provider = unit.provider;
final String extraClassesKey = provider + "@classes";
final String unitNameKey = provider + "@unitName";
final String unitName = args.getProperty(unitNameKey, "classpath-bootstrap");
final String classes = args.getProperty(extraClassesKey);
if (classes != null) {
debug("parsing value of " + extraClassesKey);
try {
final List<String> list = Arrays.asList(classes.split("[ \n\r\t,]"));
unit.classes.addAll(list);
} catch (final Exception e) {
debug("cannot parse: " + classes, e);
}
}
try {
// Hibernate doesn't use byte code modification
if (provider.startsWith("org.hibernate")) {
debug("skipping: " + provider);
continue;
} else {
debug("starting: " + provider);
}
final PersistenceUnitInfoImpl info = new PersistenceUnitInfoImpl(new Handler());
info.setManagedClassNames(new ArrayList(unit.classes));
info.setPersistenceProviderClassName(unit.provider);
info.setProperties(new Properties());
info.setId(unitName);
info.setPersistenceUnitName(unitName);
info.setRootUrlAndJarUrls("", Collections.EMPTY_LIST);
info.setJtaDataSource(new NullDataSource());
info.setNonJtaDataSource(new NullDataSource());
info.setExcludeUnlistedClasses(true);
info.setClassLoader(classLoader);
for (final String name : unit.classes) {
debug("class " + name);
}
final Class clazz = classLoader.loadClass(unit.provider);
final PersistenceProvider persistenceProvider = (PersistenceProvider) clazz.newInstance();
// Create entity manager factory
final EntityManagerFactory emf = persistenceProvider.createContainerEntityManagerFactory(info, new HashMap());
emf.close();
debug("success: " + provider);
} catch (final Throwable e) {
debug("failed: " + provider, e);
}
}
} catch (final Throwable t) {
debug("error: ", t);
}
}
use of javax.persistence.spi.PersistenceProvider in project hibernate-orm by hibernate.
the class OsgiIntegrationTest method testJpaEnvers.
@Test
public void testJpaEnvers() throws Exception {
final ServiceReference serviceReference = bundleContext.getServiceReference(PersistenceProvider.class.getName());
final PersistenceProvider persistenceProvider = (PersistenceProvider) bundleContext.getService(serviceReference);
final EntityManagerFactory emf = persistenceProvider.createEntityManagerFactory("hibernate-osgi-test", null);
final Integer adpId;
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
AuditedDataPoint adp = new AuditedDataPoint("Chris");
em.persist(adp);
em.getTransaction().commit();
adpId = adp.getId();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
adp = em.find(AuditedDataPoint.class, adpId);
adp.setName("Chris2");
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
AuditReader ar = AuditReaderFactory.get(em);
assertEquals(2, ar.getRevisions(AuditedDataPoint.class, adpId).size());
AuditedDataPoint rev1 = ar.find(AuditedDataPoint.class, adpId, 1);
AuditedDataPoint rev2 = ar.find(AuditedDataPoint.class, adpId, 2);
assertEquals(new AuditedDataPoint(adpId, "Chris"), rev1);
assertEquals(new AuditedDataPoint(adpId, "Chris2"), rev2);
em.close();
}
use of javax.persistence.spi.PersistenceProvider in project hibernate-orm by hibernate.
the class OsgiIntegrationTest method testJpa.
@Test
public void testJpa() throws Exception {
final ServiceReference serviceReference = bundleContext.getServiceReference(PersistenceProvider.class.getName());
final PersistenceProvider persistenceProvider = (PersistenceProvider) bundleContext.getService(serviceReference);
final EntityManagerFactory emf = persistenceProvider.createEntityManagerFactory("hibernate-osgi-test", null);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(new DataPoint("Brett"));
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
DataPoint dp = em.find(DataPoint.class, 1);
assertNotNull(dp);
assertEquals("Brett", dp.getName());
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
dp = em.find(DataPoint.class, 1);
dp.setName("Brett2");
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from DataPoint").executeUpdate();
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
dp = em.find(DataPoint.class, 1);
assertNull(dp);
em.getTransaction().commit();
em.close();
}
Aggregations