Search in sources :

Example 1 with PersistenceProvider

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();
    }
}
Also used : Bundle(org.osgi.framework.Bundle) PersistenceProvider(javax.persistence.spi.PersistenceProvider) BundleWiring(org.osgi.framework.wiring.BundleWiring) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 2 with PersistenceProvider

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;
}
Also used : Bundle(org.osgi.framework.Bundle) PersistenceProvider(javax.persistence.spi.PersistenceProvider) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with PersistenceProvider

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;
}
Also used : ValidationException(org.eclipse.persistence.exceptions.ValidationException) InstrumentableClassLoader(org.glassfish.api.deployment.InstrumentableClassLoader) PersistenceProvider(javax.persistence.spi.PersistenceProvider) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) AVKPersistenceUnitInfoImpl(com.sun.enterprise.tools.verifier.persistence.AVKPersistenceUnitInfoImpl) Properties(java.util.Properties) PersistenceUnitProperties(org.eclipse.persistence.config.PersistenceUnitProperties) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) PersistenceException(javax.persistence.PersistenceException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) Result(com.sun.enterprise.tools.verifier.Result) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) PersistenceUnitDescriptor(com.sun.enterprise.deployment.PersistenceUnitDescriptor)

Example 4 with PersistenceProvider

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;
}
Also used : Bundle(org.osgi.framework.Bundle) PersistenceProvider(javax.persistence.spi.PersistenceProvider) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with PersistenceProvider

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);
}
Also used : PersistenceProvider(javax.persistence.spi.PersistenceProvider) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PersistenceProvider (javax.persistence.spi.PersistenceProvider)25 EntityManagerFactory (javax.persistence.EntityManagerFactory)9 HashMap (java.util.HashMap)8 ModuleClassLoader (org.jboss.modules.ModuleClassLoader)6 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 Properties (java.util.Properties)4 PersistenceUnitInfo (javax.persistence.spi.PersistenceUnitInfo)4 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)4 ServiceReference (org.osgi.framework.ServiceReference)4 IOException (java.io.IOException)3 EntityManager (javax.persistence.EntityManager)3 PersistenceProviderDeploymentHolder (org.jboss.as.jpa.config.PersistenceProviderDeploymentHolder)3 Module (org.jboss.modules.Module)3 PersistenceProviderAdaptor (org.jipijapa.plugin.spi.PersistenceProviderAdaptor)3 Bundle (org.osgi.framework.Bundle)3 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)2 ValidationMode (javax.persistence.ValidationMode)2 TempClassLoader (org.apache.openejb.core.TempClassLoader)2 PersistenceClassLoaderHandler (org.apache.openejb.persistence.PersistenceClassLoaderHandler)2