Search in sources :

Example 1 with ModuleClassLoader

use of org.jboss.modules.ModuleClassLoader 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
         */
    List<PersistenceProvider> providerList = persistenceProviderDeploymentHolder != null ? persistenceProviderDeploymentHolder.getProviders() : null;
    if (providerList != null) {
        for (PersistenceProvider persistenceProvider : providerList) {
            if (persistenceProvider.getClass().getName().equals(pu.getPersistenceProviderClassName())) {
                ROOT_LOGGER.tracef("deployment %s is using %s", deploymentUnit.getName(), pu.getPersistenceProviderClassName());
                return persistenceProvider;
            }
        }
    }
    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)

Example 2 with ModuleClassLoader

use of org.jboss.modules.ModuleClassLoader in project wildfly by wildfly.

the class JSFSharedTldsProcessor method init.

private void init() {
    JSFModuleIdFactory moduleFactory = JSFModuleIdFactory.getInstance();
    List<String> jsfSlotNames = moduleFactory.getActiveJSFVersions();
    for (String slot : jsfSlotNames) {
        final List<TldMetaData> jsfTlds = new ArrayList<TldMetaData>();
        try {
            ModuleClassLoader jsf = Module.getModuleFromCallerModuleLoader(moduleFactory.getImplModId(slot)).getClassLoader();
            for (String tld : JSF_TAGLIBS) {
                InputStream is = jsf.getResourceAsStream("META-INF/" + tld);
                if (is != null) {
                    TldMetaData tldMetaData = parseTLD(is);
                    jsfTlds.add(tldMetaData);
                }
            }
        } catch (ModuleLoadException e) {
        // Ignore
        } catch (Exception e) {
        // Ignore
        }
        jsfTldMap.put(slot, jsfTlds);
    }
}
Also used : TldMetaData(org.jboss.metadata.web.spec.TldMetaData) ModuleLoadException(org.jboss.modules.ModuleLoadException) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ModuleLoadException(org.jboss.modules.ModuleLoadException) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException)

Example 3 with ModuleClassLoader

use of org.jboss.modules.ModuleClassLoader in project wildfly by wildfly.

the class JarResourceListingTestCase method doTestJARResourceRetrieval.

private void doTestJARResourceRetrieval(boolean recursive, String rootDir) {
    ModuleClassLoader classLoader = (ModuleClassLoader) getClass().getClassLoader();
    List<String> foundResources = ResourceListingUtils.listResources(classLoader, rootDir, recursive);
    // only resources in JAR library should be listed
    List<String> resourcesInDeployment = new ArrayList<>();
    resourcesInDeployment.add(ResourceListingUtils.classToPath(JarResourceListingTestCase.class));
    resourcesInDeployment.add(ResourceListingUtils.classToPath(ResourceListingUtils.class));
    resourcesInDeployment.add("META-INF/example.txt");
    resourcesInDeployment.add("META-INF/properties/nested.properties");
    resourcesInDeployment.add("META-INF/MANIFEST.MF");
    resourcesInDeployment.add("example2.txt");
    resourcesInDeployment.add("TextFile1.txt");
    ResourceListingUtils.filterResources(resourcesInDeployment, rootDir, !recursive);
    Collections.sort(foundResources);
    Collections.sort(resourcesInDeployment);
    log.trace("List of expected resources:");
    for (String expectedResource : resourcesInDeployment) {
        log.trace(expectedResource);
    }
    log.trace("List of found resources: ");
    for (String foundResource : foundResources) {
        log.trace(foundResource);
    }
    Assert.assertArrayEquals("Not all resources from JAR archive are correctly listed", resourcesInDeployment.toArray(), foundResources.toArray());
}
Also used : ResourceListingUtils(org.jboss.as.test.shared.ResourceListingUtils) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) ArrayList(java.util.ArrayList)

Example 4 with ModuleClassLoader

use of org.jboss.modules.ModuleClassLoader in project wildfly by wildfly.

the class WarResourceListingTestCase method doTestResourceRetrieval.

/**
     * Based on provided parameters it filters which resources should be available and which not and tests if the retrieved resources matches this list
     * @param recursive also a nested/recursive resources are counted
     * @param rootDir represents the filtering by root directory (only resources in the specified root dir are taken into account
     */
private void doTestResourceRetrieval(boolean recursive, String rootDir) {
    ModuleClassLoader classLoader = (ModuleClassLoader) getClass().getClassLoader();
    List<String> resourcesInDeployment = getActualResourcesInWar(recursive, rootDir);
    List<String> foundResources = ResourceListingUtils.listResources(classLoader, rootDir, recursive);
    Collections.sort(foundResources);
    log.trace("List of expected resources:");
    for (String expectedResource : resourcesInDeployment) {
        log.trace(expectedResource);
    }
    log.trace("List of found resources: ");
    for (String foundResource : foundResources) {
        log.trace(foundResource);
    }
    Assert.assertArrayEquals("Not all resources from WAR archive are correctly listed", resourcesInDeployment.toArray(), foundResources.toArray());
}
Also used : ModuleClassLoader(org.jboss.modules.ModuleClassLoader)

Example 5 with ModuleClassLoader

use of org.jboss.modules.ModuleClassLoader in project wildfly by wildfly.

the class DriverProcessor method deploy.

/** {@inheritDoc} */
@Override
public void deploy(final DeploymentPhaseContext phaseContext) 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 classLoader = module.getClassLoader();
        final List<String> driverNames = servicesAttachment.getServiceImplementations(Driver.class.getName());
        for (String driverClassName : driverNames) {
            try {
                final Class<? extends Driver> driverClass = classLoader.loadClass(driverClassName).asSubclass(Driver.class);
                final Constructor<? extends Driver> constructor = driverClass.getConstructor();
                final Driver driver = constructor.newInstance();
                final int majorVersion = driver.getMajorVersion();
                final int minorVersion = driver.getMinorVersion();
                final boolean compliant = driver.jdbcCompliant();
                if (compliant) {
                    DEPLOYER_JDBC_LOGGER.deployingCompliantJdbcDriver(driverClass, Integer.valueOf(majorVersion), Integer.valueOf(minorVersion));
                } else {
                    DEPLOYER_JDBC_LOGGER.deployingNonCompliantJdbcDriver(driverClass, Integer.valueOf(majorVersion), Integer.valueOf(minorVersion));
                }
                String driverName = deploymentUnit.getName();
                if ((driverName.contains(".") && !driverName.endsWith(".jar")) || driverNames.size() != 1) {
                    driverName += "_" + driverClassName + "_" + majorVersion + "_" + minorVersion;
                }
                InstalledDriver driverMetadata = new InstalledDriver(driverName, driverClass.getName(), null, null, majorVersion, minorVersion, compliant);
                DriverService driverService = new DriverService(driverMetadata, driver);
                phaseContext.getServiceTarget().addService(ServiceName.JBOSS.append("jdbc-driver", driverName.replaceAll("\\.", "_")), driverService).addDependency(ConnectorServices.JDBC_DRIVER_REGISTRY_SERVICE, DriverRegistry.class, driverService.getDriverRegistryServiceInjector()).setInitialMode(Mode.ACTIVE).install();
            } catch (Throwable e) {
                DEPLOYER_JDBC_LOGGER.cannotInstantiateDriverClass(driverClassName, e);
            }
        }
    }
}
Also used : InstalledDriver(org.jboss.as.connector.services.driver.InstalledDriver) ServicesAttachment(org.jboss.as.server.deployment.ServicesAttachment) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) InstalledDriver(org.jboss.as.connector.services.driver.InstalledDriver) Driver(java.sql.Driver) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) DriverService(org.jboss.as.connector.services.driver.DriverService)

Aggregations

ModuleClassLoader (org.jboss.modules.ModuleClassLoader)12 Module (org.jboss.modules.Module)5 ArrayList (java.util.ArrayList)4 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)4 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)4 PersistenceProvider (javax.persistence.spi.PersistenceProvider)3 ModuleLoadException (org.jboss.modules.ModuleLoadException)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ComponentDescription (org.jboss.as.ee.component.ComponentDescription)2 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)2 ServicesAttachment (org.jboss.as.server.deployment.ServicesAttachment)2 ResourceListingUtils (org.jboss.as.test.shared.ResourceListingUtils)2 TldMetaData (org.jboss.metadata.web.spec.TldMetaData)2 PersistenceProviderAdaptor (org.jipijapa.plugin.spi.PersistenceProviderAdaptor)2 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 Driver (java.sql.Driver)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1