Search in sources :

Example 6 with PersistenceProvider

use of javax.persistence.spi.PersistenceProvider in project opencast by opencast.

the class PersistenceUtil method mkEntityManagerFactory.

/**
 * 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 mkEntityManagerFactory(ComponentContext cc, String emName) {
    final PersistenceProvider persistenceProvider = (PersistenceProvider) cc.locateService("persistence");
    final Map pp = (Map) cc.locateService("persistenceProps");
    final Map persistenceProps = pp != null ? pp : NO_PERSISTENCE_PROPS;
    return persistenceProvider.createEntityManagerFactory(emName, persistenceProps);
}
Also used : PersistenceProvider(javax.persistence.spi.PersistenceProvider) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with PersistenceProvider

use of javax.persistence.spi.PersistenceProvider in project wildfly by wildfly.

the class PersistenceProviderResolverImpl method addDeploymentSpecificPersistenceProvider.

/**
 * Set at application deployment time to the persistence providers packaged in the application
 *
 * @param persistenceProvider
 * @param deploymentClassLoaders
 */
public void addDeploymentSpecificPersistenceProvider(PersistenceProvider persistenceProvider, Set<ClassLoader> deploymentClassLoaders) {
    synchronized (persistenceProviderPerClassLoader) {
        for (ClassLoader deploymentClassLoader : deploymentClassLoaders) {
            List<Class<? extends PersistenceProvider>> list = persistenceProviderPerClassLoader.get(deploymentClassLoader);
            ROOT_LOGGER.tracef("getting persistence provider list (%s) for deployment (%s)", list, deploymentClassLoader);
            if (list == null) {
                list = new ArrayList<>();
                persistenceProviderPerClassLoader.put(deploymentClassLoader, list);
                ROOT_LOGGER.tracef("saving new persistence provider list (%s) for deployment (%s)", list, deploymentClassLoader);
            }
            list.add(persistenceProvider.getClass());
            ROOT_LOGGER.tracef("added new persistence provider (%s) to provider list (%s)", persistenceProvider.getClass().getName(), list);
        }
    }
}
Also used : PersistenceProvider(javax.persistence.spi.PersistenceProvider) ModuleClassLoader(org.jboss.modules.ModuleClassLoader)

Example 8 with PersistenceProvider

use of javax.persistence.spi.PersistenceProvider in project wildfly by wildfly.

the class PersistenceProviderResolverImpl method getPersistenceProviders.

/**
 * Return a new instance of each persistence provider class
 *
 * @return
 */
@Override
public List<PersistenceProvider> getPersistenceProviders() {
    List<PersistenceProvider> providersCopy = new ArrayList<>(providers.size());
    /**
     * Add the application specified providers first so they are found before the global providers
     */
    synchronized (persistenceProviderPerClassLoader) {
        if (persistenceProviderPerClassLoader.size() > 0) {
            // get the deployment or subdeployment classloader
            ClassLoader deploymentClassLoader = findParentModuleCl(WildFlySecurityManager.getCurrentContextClassLoaderPrivileged());
            ROOT_LOGGER.tracef("get application level Persistence Provider for classloader %s", deploymentClassLoader);
            // collect persistence providers associated with deployment/each sub-deployment
            List<Class<? extends PersistenceProvider>> deploymentSpecificPersistenceProviders = persistenceProviderPerClassLoader.get(deploymentClassLoader);
            ROOT_LOGGER.tracef("got application level Persistence Provider list %s", deploymentSpecificPersistenceProviders);
            if (deploymentSpecificPersistenceProviders != null) {
                for (Class<? extends PersistenceProvider> providerClass : deploymentSpecificPersistenceProviders) {
                    try {
                        ROOT_LOGGER.tracef("application has its own Persistence Provider %s", providerClass.getName());
                        providersCopy.add(providerClass.newInstance());
                    } catch (InstantiationException e) {
                        throw JpaLogger.ROOT_LOGGER.couldNotCreateInstanceProvider(e, providerClass.getName());
                    } catch (IllegalAccessException e) {
                        throw JpaLogger.ROOT_LOGGER.couldNotCreateInstanceProvider(e, providerClass.getName());
                    }
                }
            }
        }
    }
    // add global persistence providers last (so application packaged providers have priority)
    for (Class<?> providerClass : providers) {
        try {
            providersCopy.add((PersistenceProvider) providerClass.newInstance());
            ROOT_LOGGER.tracef("returning global (module) Persistence Provider %s", providerClass.getName());
        } catch (InstantiationException e) {
            throw JpaLogger.ROOT_LOGGER.couldNotCreateInstanceProvider(e, providerClass.getName());
        } catch (IllegalAccessException e) {
            throw JpaLogger.ROOT_LOGGER.couldNotCreateInstanceProvider(e, providerClass.getName());
        }
    }
    return providersCopy;
}
Also used : PersistenceProvider(javax.persistence.spi.PersistenceProvider) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ModuleClassLoader(org.jboss.modules.ModuleClassLoader)

Example 9 with PersistenceProvider

use of javax.persistence.spi.PersistenceProvider in project wildfly by wildfly.

the class PersistenceProviderDeploymentHolder method savePersistenceProviderInDeploymentUnit.

public static void savePersistenceProviderInDeploymentUnit(DeploymentUnit deploymentUnit, final List<PersistenceProvider> providerList, final List<PersistenceProviderAdaptor> adaptorList) {
    deploymentUnit = DeploymentUtils.getTopDeploymentUnit(deploymentUnit);
    PersistenceProviderDeploymentHolder persistenceProviderDeploymentHolder = getPersistenceProviderDeploymentHolder(deploymentUnit);
    if (persistenceProviderDeploymentHolder == null) {
        persistenceProviderDeploymentHolder = new PersistenceProviderDeploymentHolder(providerList, adaptorList);
        deploymentUnit.putAttachment(JpaAttachments.DEPLOYED_PERSISTENCE_PROVIDER, persistenceProviderDeploymentHolder);
    } else {
        synchronized (persistenceProviderDeploymentHolder.providerMap) {
            for (PersistenceProvider persistenceProvider : providerList) {
                persistenceProviderDeploymentHolder.providerMap.put(persistenceProvider.getClass().getName(), persistenceProvider);
            }
        }
        if (adaptorList != null) {
            persistenceProviderDeploymentHolder.adapterList.addAll(adaptorList);
        }
    }
}
Also used : PersistenceProvider(javax.persistence.spi.PersistenceProvider)

Example 10 with PersistenceProvider

use of javax.persistence.spi.PersistenceProvider in project wildfly by wildfly.

the class PersistenceUnitServiceHandler method lookupProvider.

/**
 * Look up the persistence provider
 *
 * @param pu
 * @param deploymentUnit
 * @return
 */
private static PersistenceProvider lookupProvider(PersistenceUnitMetadata pu, PersistenceProviderDeploymentHolder persistenceProviderDeploymentHolder, DeploymentUnit deploymentUnit) throws DeploymentUnitProcessingException {
    /**
     * check if the deployment is already associated with the specified persistence provider
     */
    Map<String, PersistenceProvider> providerMap = persistenceProviderDeploymentHolder != null ? persistenceProviderDeploymentHolder.getProviders() : null;
    if (providerMap != null) {
        synchronized (providerMap) {
            if (providerMap.containsKey(pu.getPersistenceProviderClassName())) {
                ROOT_LOGGER.tracef("deployment %s is using %s", deploymentUnit.getName(), pu.getPersistenceProviderClassName());
                return providerMap.get(pu.getPersistenceProviderClassName());
            }
        }
    }
    String configuredPersistenceProviderModule = pu.getProperties().getProperty(Configuration.PROVIDER_MODULE);
    String persistenceProviderClassName = pu.getPersistenceProviderClassName();
    if (persistenceProviderClassName == null) {
        persistenceProviderClassName = Configuration.PROVIDER_CLASS_DEFAULT;
    }
    /**
     * locate persistence provider in specified static module
     */
    if (configuredPersistenceProviderModule != null) {
        List<PersistenceProvider> providers;
        if (Configuration.PROVIDER_MODULE_APPLICATION_SUPPLIED.equals(configuredPersistenceProviderModule)) {
            try {
                // load the persistence provider from the application deployment
                final ModuleClassLoader classLoader = deploymentUnit.getAttachment(Attachments.MODULE).getClassLoader();
                PersistenceProvider provider = PersistenceProviderLoader.loadProviderFromDeployment(classLoader, persistenceProviderClassName);
                providers = new ArrayList<>();
                providers.add(provider);
                PersistenceProviderDeploymentHolder.savePersistenceProviderInDeploymentUnit(deploymentUnit, providers, null);
                return provider;
            } catch (ClassNotFoundException e) {
                throw JpaLogger.ROOT_LOGGER.cannotDeployApp(e, persistenceProviderClassName);
            } catch (InstantiationException e) {
                throw JpaLogger.ROOT_LOGGER.cannotDeployApp(e, persistenceProviderClassName);
            } catch (IllegalAccessException e) {
                throw JpaLogger.ROOT_LOGGER.cannotDeployApp(e, persistenceProviderClassName);
            }
        } else {
            try {
                providers = PersistenceProviderLoader.loadProviderModuleByName(configuredPersistenceProviderModule);
                PersistenceProviderDeploymentHolder.savePersistenceProviderInDeploymentUnit(deploymentUnit, providers, null);
                PersistenceProvider provider = getProviderByName(pu, providers);
                if (provider != null) {
                    return provider;
                }
            } catch (ModuleLoadException e) {
                throw JpaLogger.ROOT_LOGGER.cannotLoadPersistenceProviderModule(e, configuredPersistenceProviderModule, persistenceProviderClassName);
            }
        }
    }
    // try to determine the static module name based on the persistence provider class name
    String providerNameDerivedFromClassName = Configuration.getProviderModuleNameFromProviderClassName(persistenceProviderClassName);
    // see if the providerNameDerivedFromClassName has been loaded yet
    PersistenceProvider provider = getProviderByName(pu);
    // if we haven't loaded the provider yet, try loading now
    if (provider == null && providerNameDerivedFromClassName != null) {
        try {
            List<PersistenceProvider> providers = PersistenceProviderLoader.loadProviderModuleByName(providerNameDerivedFromClassName);
            PersistenceProviderDeploymentHolder.savePersistenceProviderInDeploymentUnit(deploymentUnit, providers, null);
            provider = getProviderByName(pu, providers);
        } catch (ModuleLoadException e) {
            throw JpaLogger.ROOT_LOGGER.cannotLoadPersistenceProviderModule(e, providerNameDerivedFromClassName, persistenceProviderClassName);
        }
    }
    if (provider == null)
        throw JpaLogger.ROOT_LOGGER.persistenceProviderNotFound(persistenceProviderClassName);
    return provider;
}
Also used : ModuleLoadException(org.jboss.modules.ModuleLoadException) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) PersistenceProvider(javax.persistence.spi.PersistenceProvider)

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