use of javax.persistence.spi.PersistenceProvider in project aries by apache.
the class XAJPAEMFLocator method setupTransactionManager.
private void setupTransactionManager(BundleContext context, Map<String, Object> props, Map<String, Object> providerProps, ThreadLocal<TransactionControl> t, ServiceReference<EntityManagerFactoryBuilder> reference) {
String provider = (String) reference.getProperty(JPA_UNIT_PROVIDER);
ServiceReference<PersistenceProvider> providerRef = getPersistenceProvider(provider, context);
if (providerRef == null) {
// TODO log a warning and give up
return;
}
Bundle providerBundle = providerRef.getBundle();
Bundle txControlProviderBundle = context.getBundle();
try {
if ("org.hibernate.jpa.HibernatePersistenceProvider".equals(provider)) {
try {
providerBundle.loadClass("org.hibernate.resource.transaction.TransactionCoordinatorBuilder");
} catch (Exception e) {
BundleWiring wiring = providerBundle.adapt(BundleWiring.class);
providerBundle = wiring.getRequiredWires("osgi.wiring.package").stream().filter(bw -> "org.hibernate".equals(bw.getCapability().getAttributes().get("osgi.wiring.package"))).map(BundleWire::getProviderWiring).map(BundleWiring::getBundle).findFirst().get();
}
ClassLoader pluginLoader = getPluginLoader(providerBundle, txControlProviderBundle);
Class<?> pluginClazz = pluginLoader.loadClass("org.apache.aries.tx.control.jpa.xa.hibernate.impl.HibernateTxControlPlatform");
Object plugin = pluginClazz.getConstructor(ThreadLocal.class).newInstance(t);
props.put("hibernate.transaction.coordinator_class", plugin);
} else if ("org.apache.openjpa.persistence.PersistenceProviderImpl".equals(provider)) {
ClassLoader pluginLoader = getPluginLoader(providerBundle, txControlProviderBundle);
Class<?> pluginClazz = pluginLoader.loadClass("org.apache.aries.tx.control.jpa.xa.openjpa.impl.OpenJPATxControlPlatform");
Object plugin = pluginClazz.getConstructor(ThreadLocal.class).newInstance(t);
props.put("openjpa.ManagedRuntime", plugin);
} else if ("org.eclipse.persistence.jpa.PersistenceProvider".equals(provider)) {
ClassLoader pluginLoader = getPluginLoader(providerBundle, txControlProviderBundle);
Class<?> pluginClazz = pluginLoader.loadClass("org.apache.aries.tx.control.jpa.xa.eclipse.impl.EclipseTxControlPlatform");
pluginClazz.getMethod("setTransactionControl", ThreadLocal.class).invoke(null, t);
props.put("eclipselink.target-server", pluginClazz.getName());
props.put("org.apache.aries.jpa.eclipselink.plugin.types", pluginClazz);
// transactions without blowing up.
if (!props.containsKey("eclipselink.jdbc.sequence-connection-pool")) {
props.put("eclipselink.jdbc.sequence-connection-pool", "true");
}
} else {
// TODO log a warning and give up
return;
}
} catch (Exception e) {
//TODO log a warning and give up
e.printStackTrace();
}
}
use of javax.persistence.spi.PersistenceProvider in project hibernate-orm by hibernate.
the class HibernateUtil method getEntityManagerFactory.
private EntityManagerFactory getEntityManagerFactory() {
if (emf == null) {
Bundle thisBundle = FrameworkUtil.getBundle(HibernateUtil.class);
// Could get this by wiring up OsgiTestBundleActivator as well.
BundleContext context = thisBundle.getBundleContext();
ServiceReference serviceReference = context.getServiceReference(PersistenceProvider.class.getName());
PersistenceProvider persistenceProvider = (PersistenceProvider) context.getService(serviceReference);
emf = persistenceProvider.createEntityManagerFactory("unmanaged-jpa", null);
}
return emf;
}
use of javax.persistence.spi.PersistenceProvider in project Payara by payara.
the class DefaultProviderVerification method check.
public Result check(Descriptor descriptor) {
PersistenceUnitDescriptor pu = PersistenceUnitDescriptor.class.cast(descriptor);
Result result = getInitializedResult();
result.setStatus(Result.PASSED);
PersistenceProvider provider;
final String appLocation = getVerifierContext().getAbstractArchive().getURI().getPath();
final InstrumentableClassLoader cl = InstrumentableClassLoader.class.cast(pu.getParent().getClassLoader());
PersistenceUnitInfo pi = new AVKPersistenceUnitInfoImpl(pu, appLocation, cl);
logger.fine("PersistenceInfo for PU is :\n" + pi);
Properties props = new Properties();
// This property is set to indicate that TopLink should only
// validate the descriptors etc. and not try to login to database.
props.put(PersistenceUnitProperties.VALIDATION_ONLY_PROPERTY, // NOI18N
"TRUE");
// This property is used so that TopLink throws validation exceptions
// as opposed to printing CONFIG level messages to console.
// e.g. if mapping file does not exist, we will get an exception.
props.put(PersistenceUnitProperties.THROW_EXCEPTIONS, // NOI18N
"TRUE");
// the following property is needed as it initializes the logger in TL
props.put(PersistenceUnitProperties.TARGET_SERVER, // NOI18N
"SunAS9");
// Turn off enhancement during verification. For details,
// refer to http://glassfish.dev.java.net/issues/show_bug.cgi?id=3295
props.put(PersistenceUnitProperties.WEAVING, "FALSE");
provider = new org.eclipse.persistence.jpa.PersistenceProvider();
EntityManagerFactory emf = null;
try {
emf = provider.createContainerEntityManagerFactory(pi, props);
logger.logp(Level.FINE, "DefaultProviderVerification", "check", "emf = {0}", emf);
} catch (IntegrityException ie) {
result.setStatus(Result.FAILED);
addErrorDetails(result, getVerifierContext().getComponentNameConstructor());
for (Object o : ie.getIntegrityChecker().getCaughtExceptions()) {
Exception e = Exception.class.cast(o);
result.addErrorDetails(e.getMessage());
}
} catch (ValidationException ve) {
addErrorDetails(result, getVerifierContext().getComponentNameConstructor());
result.failed(ve.getMessage());
logger.logp(Level.FINE, "DefaultProviderVerification", "check", "Following exception occurred", ve);
} catch (DatabaseException de) {
addErrorDetails(result, getVerifierContext().getComponentNameConstructor());
result.failed(de.getMessage());
logger.logp(Level.FINE, "DefaultProviderVerification", "check", "Following exception occurred", de);
} catch (PersistenceException pe) {
addErrorDetails(result, getVerifierContext().getComponentNameConstructor());
result.failed(pe.getMessage());
logger.logp(Level.FINE, "DefaultProviderVerification", "check", "Following exception occurred", pe);
} finally {
if (emf != null) {
emf.close();
}
}
return result;
}
use of javax.persistence.spi.PersistenceProvider in project hibernate-orm by hibernate.
the class HibernateUtil method getEntityManagerFactory.
private EntityManagerFactory getEntityManagerFactory() {
if (emf == null) {
Bundle thisBundle = FrameworkUtil.getBundle(HibernateUtil.class);
BundleContext context = thisBundle.getBundleContext();
ServiceReference serviceReference = context.getServiceReference(PersistenceProvider.class.getName());
PersistenceProvider persistenceProvider = (PersistenceProvider) context.getService(serviceReference);
emf = persistenceProvider.createEntityManagerFactory("YourPersistenceUnitName", null);
}
return emf;
}
use of javax.persistence.spi.PersistenceProvider in project opencast by opencast.
the class PersistenceUtil method newEntityManagerFactory.
/**
* Create a new entity manager factory with the persistence unit name <code>emName</code>. A
* {@link javax.persistence.spi.PersistenceProvider} named <code>persistence</code> has to be registered as an OSGi
* service. If you want to configure the factory please also register a map containing all properties under the name
* <code>persistenceProps</code>. See
* {@link javax.persistence.spi.PersistenceProvider#createEntityManagerFactory(String, java.util.Map)} for more
* information about config maps.
*
* @param emName
* name of the persistence unit
*/
public static EntityManagerFactory newEntityManagerFactory(ComponentContext cc, String emName) {
PersistenceProvider persistenceProvider = (PersistenceProvider) cc.locateService("persistence");
final Map persistenceProps;
Map pp = (Map) cc.locateService("persistenceProps");
persistenceProps = pp != null ? pp : Collections.emptyMap();
return persistenceProvider.createEntityManagerFactory(emName, persistenceProps);
}
Aggregations