Search in sources :

Example 6 with ModuleClassLoader

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

the class FieldInjectionTarget method getField.

private Field getField(final DeploymentUnit deploymentUnit) throws DeploymentUnitProcessingException {
    final String name = getName();
    final String className = getClassName();
    final Module module = deploymentUnit.getAttachment(MODULE);
    final ModuleClassLoader classLoader = module.getClassLoader();
    final DeploymentReflectionIndex reflectionIndex = deploymentUnit.getAttachment(REFLECTION_INDEX);
    final ClassReflectionIndex classIndex;
    try {
        classIndex = reflectionIndex.getClassIndex(Class.forName(className, false, classLoader));
    } catch (ClassNotFoundException e) {
        throw new DeploymentUnitProcessingException(e);
    }
    final Field field = classIndex.getField(name);
    if (field == null) {
        throw EeLogger.ROOT_LOGGER.fieldNotFound(name);
    }
    return field;
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) Field(java.lang.reflect.Field) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) ClassReflectionIndex(org.jboss.as.server.deployment.reflect.ClassReflectionIndex) Module(org.jboss.modules.Module) DeploymentReflectionIndex(org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex)

Example 7 with ModuleClassLoader

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

the class WarResourceListingTestCase method testDirectResourceRetrieval.

@Test()
public void testDirectResourceRetrieval() {
    log.trace("Test accessing resources using getResource method");
    ModuleClassLoader classLoader = (ModuleClassLoader) getClass().getClassLoader();
    // checking that resource under META-INF is accessible
    URL manifestResource = classLoader.getResource("META-INF/example.txt");
    assertNotNull("Resource in META-INF should be accessible", manifestResource);
    // checking that resource under META-INF is accessible
    URL nestedManifestResource = classLoader.getResource("META-INF/properties/nested.properties");
    assertNotNull("Nested resource should be also accessible", nestedManifestResource);
    // checking that resource which is not under META-INF is not accessible
    URL nonManifestResource = classLoader.getResource("example2.txt");
    assertNull("Resource in the root of WAR shouldn't be accessible", nonManifestResource);
}
Also used : ModuleClassLoader(org.jboss.modules.ModuleClassLoader) URL(java.net.URL) Test(org.junit.Test)

Example 8 with ModuleClassLoader

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

the class WeldComponentIntegrationProcessor method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (!WeldDeploymentMarker.isWeldDeployment(deploymentUnit)) {
        return;
    }
    final DeploymentUnit topLevelDeployment = getRootDeploymentUnit(deploymentUnit);
    final EEModuleDescription eeModuleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
    final ServiceName weldBootstrapService = topLevelDeployment.getServiceName().append(WeldBootstrapService.SERVICE_NAME);
    final ServiceName weldStartService = topLevelDeployment.getServiceName().append(WeldStartService.SERVICE_NAME);
    final ServiceName beanManagerService = ServiceNames.beanManagerServiceName(deploymentUnit);
    final Iterable<ComponentIntegrator> componentIntegrators = ServiceLoader.load(ComponentIntegrator.class, WildFlySecurityManager.getClassLoaderPrivileged(WeldComponentIntegrationProcessor.class));
    final ComponentInterceptorSupport componentInterceptorSupport = ServiceLoaders.loadSingle(ComponentInterceptorSupport.class, WeldComponentIntegrationProcessor.class).orElse(null);
    WeldClassIntrospector.install(deploymentUnit, phaseContext.getServiceTarget());
    eeModuleDescription.setDefaultClassIntrospectorServiceName(WeldClassIntrospector.serviceName(deploymentUnit));
    for (ComponentDescription component : eeModuleDescription.getComponentDescriptions()) {
        final String beanName;
        if (isBeanNameRequired(component, componentIntegrators)) {
            beanName = component.getComponentName();
        } else {
            beanName = null;
        }
        component.getConfigurators().add((context, description, configuration) -> {
            //add interceptor to activate the request scope if required
            final EjbRequestScopeActivationInterceptor.Factory requestFactory = new EjbRequestScopeActivationInterceptor.Factory(beanManagerService);
            for (ViewConfiguration view : configuration.getViews()) {
                view.addViewInterceptor(requestFactory, InterceptorOrder.View.CDI_REQUEST_SCOPE);
            }
            configuration.addTimeoutViewInterceptor(requestFactory, InterceptorOrder.View.CDI_REQUEST_SCOPE);
        });
        component.getConfigurators().addFirst(new ComponentConfigurator() {

            @Override
            public void configure(final DeploymentPhaseContext context, final ComponentDescription description, final ComponentConfiguration configuration) throws DeploymentUnitProcessingException {
                final Class<?> componentClass = configuration.getComponentClass();
                final DeploymentUnit deploymentUnit = context.getDeploymentUnit();
                final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
                final ModuleClassLoader classLoader = module.getClassLoader();
                //get the interceptors so they can be injected as well
                final Set<Class<?>> interceptorClasses = new HashSet<Class<?>>();
                for (InterceptorDescription interceptorDescription : description.getAllInterceptors()) {
                    try {
                        interceptorClasses.add(ClassLoadingUtils.loadClass(interceptorDescription.getInterceptorClassName(), module));
                    } catch (ClassNotFoundException e) {
                        throw WeldLogger.ROOT_LOGGER.couldNotLoadInterceptorClass(interceptorDescription.getInterceptorClassName(), e);
                    }
                }
                addWeldIntegration(componentIntegrators, componentInterceptorSupport, context.getServiceTarget(), configuration, description, componentClass, beanName, weldBootstrapService, weldStartService, beanManagerService, interceptorClasses, classLoader, description.getBeanDeploymentArchiveId());
            }
        });
    }
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) ComponentDescription(org.jboss.as.ee.component.ComponentDescription) Set(java.util.Set) HashSet(java.util.HashSet) ComponentConfigurator(org.jboss.as.ee.component.ComponentConfigurator) UserInterceptorFactory(org.jboss.as.ee.component.interceptors.UserInterceptorFactory) WeldManagedReferenceFactory(org.jboss.as.weld.injection.WeldManagedReferenceFactory) ImmediateInterceptorFactory(org.jboss.invocation.ImmediateInterceptorFactory) DeploymentPhaseContext(org.jboss.as.server.deployment.DeploymentPhaseContext) ViewConfiguration(org.jboss.as.ee.component.ViewConfiguration) InterceptorDescription(org.jboss.as.ee.component.InterceptorDescription) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) ComponentIntegrator(org.jboss.as.weld.spi.ComponentIntegrator) ComponentInterceptorSupport(org.jboss.as.weld.spi.ComponentInterceptorSupport) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) ComponentConfiguration(org.jboss.as.ee.component.ComponentConfiguration) ServiceName(org.jboss.msc.service.ServiceName) EjbRequestScopeActivationInterceptor(org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) Utils.getRootDeploymentUnit(org.jboss.as.weld.util.Utils.getRootDeploymentUnit)

Example 9 with ModuleClassLoader

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

the class PersistenceUnitServiceHandler method addPuService.

/**
     * Add one PU service per top level deployment that represents
     *
     *
     * @param phaseContext
     * @param puList
     * @param startEarly
     * @param platform
     * @throws DeploymentUnitProcessingException
     *
     */
private static void addPuService(final DeploymentPhaseContext phaseContext, final ArrayList<PersistenceUnitMetadataHolder> puList, final boolean startEarly, final Platform platform) throws DeploymentUnitProcessingException {
    if (puList.size() > 0) {
        final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
        final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
        final EEModuleDescription eeModuleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
        final Collection<ComponentDescription> components = eeModuleDescription.getComponentDescriptions();
        if (module == null) {
            // Unresolved OSGi bundles would not have a module attached
            ROOT_LOGGER.failedToGetModuleAttachment(deploymentUnit);
            return;
        }
        final ServiceTarget serviceTarget = phaseContext.getServiceTarget();
        final ModuleClassLoader classLoader = module.getClassLoader();
        for (PersistenceUnitMetadataHolder holder : puList) {
            setAnnotationIndexes(holder, deploymentUnit);
            for (PersistenceUnitMetadata pu : holder.getPersistenceUnits()) {
                // only start the persistence unit if JPA_CONTAINER_MANAGED is true
                String jpaContainerManaged = pu.getProperties().getProperty(Configuration.JPA_CONTAINER_MANAGED);
                boolean deployPU = (jpaContainerManaged == null ? true : Boolean.parseBoolean(jpaContainerManaged));
                if (deployPU) {
                    final PersistenceProviderDeploymentHolder persistenceProviderDeploymentHolder = getPersistenceProviderDeploymentHolder(deploymentUnit);
                    final PersistenceProvider provider = lookupProvider(pu, persistenceProviderDeploymentHolder, deploymentUnit);
                    final PersistenceProviderAdaptor adaptor = getPersistenceProviderAdaptor(pu, persistenceProviderDeploymentHolder, deploymentUnit, provider, platform);
                    final boolean twoPhaseBootStrapCapable = (adaptor instanceof TwoPhaseBootstrapCapable) && Configuration.allowTwoPhaseBootstrap(pu);
                    if (startEarly) {
                        if (twoPhaseBootStrapCapable) {
                            deployPersistenceUnitPhaseOne(phaseContext, deploymentUnit, eeModuleDescription, components, serviceTarget, classLoader, pu, adaptor);
                        } else if (false == Configuration.needClassFileTransformer(pu)) {
                            // will start later when startEarly == false
                            ROOT_LOGGER.tracef("persistence unit %s in deployment %s is configured to not need class transformer to be set, no class rewriting will be allowed", pu.getPersistenceUnitName(), deploymentUnit.getName());
                        } else {
                            // we need class file transformer to work, don't allow cdi bean manager to be access since that
                            // could cause application classes to be loaded (workaround by setting jboss.as.jpa.classtransformer to false).  WFLY-1463
                            final boolean allowCdiBeanManagerAccess = false;
                            deployPersistenceUnit(phaseContext, deploymentUnit, eeModuleDescription, components, serviceTarget, classLoader, pu, startEarly, provider, adaptor, allowCdiBeanManagerAccess);
                        }
                    } else {
                        // !startEarly
                        if (twoPhaseBootStrapCapable) {
                            deployPersistenceUnitPhaseTwo(phaseContext, deploymentUnit, eeModuleDescription, components, serviceTarget, classLoader, pu, provider, adaptor);
                        } else if (false == Configuration.needClassFileTransformer(pu)) {
                            final boolean allowCdiBeanManagerAccess = true;
                            // PUs that have Configuration.JPA_CONTAINER_CLASS_TRANSFORMER = false will start during INSTALL phase
                            deployPersistenceUnit(phaseContext, deploymentUnit, eeModuleDescription, components, serviceTarget, classLoader, pu, startEarly, provider, adaptor, allowCdiBeanManagerAccess);
                        }
                    }
                } else {
                    ROOT_LOGGER.tracef("persistence unit %s in deployment %s is not container managed (%s is set to false)", pu.getPersistenceUnitName(), deploymentUnit.getName(), Configuration.JPA_CONTAINER_MANAGED);
                }
            }
        }
    }
}
Also used : ComponentDescription(org.jboss.as.ee.component.ComponentDescription) PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) ServiceTarget(org.jboss.msc.service.ServiceTarget) PersistenceProvider(javax.persistence.spi.PersistenceProvider) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) PersistenceUnitMetadata(org.jipijapa.plugin.spi.PersistenceUnitMetadata) Module(org.jboss.modules.Module) PersistenceProviderDeploymentHolder(org.jboss.as.jpa.config.PersistenceProviderDeploymentHolder) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) TwoPhaseBootstrapCapable(org.jipijapa.plugin.spi.TwoPhaseBootstrapCapable) PersistenceProviderAdaptor(org.jipijapa.plugin.spi.PersistenceProviderAdaptor)

Example 10 with ModuleClassLoader

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

the class PersistenceProviderHandler method deploy.

public static void deploy(final DeploymentPhaseContext phaseContext, final Platform platform) 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 deploymentModuleClassLoader = module.getClassLoader();
        PersistenceProvider provider;
        // collect list of persistence providers packaged with the application
        final List<String> providerNames = servicesAttachment.getServiceImplementations(PERSISTENCE_PROVIDER_CLASSNAME);
        List<PersistenceProvider> providerList = new ArrayList<PersistenceProvider>();
        for (String providerName : providerNames) {
            try {
                final Class<? extends PersistenceProvider> providerClass = deploymentModuleClassLoader.loadClass(providerName).asSubclass(PersistenceProvider.class);
                final Constructor<? extends PersistenceProvider> constructor = providerClass.getConstructor();
                provider = constructor.newInstance();
                providerList.add(provider);
                JpaLogger.ROOT_LOGGER.tracef("deployment %s is using its own copy of %s", deploymentUnit.getName(), providerName);
            } catch (Exception e) {
                throw JpaLogger.ROOT_LOGGER.cannotDeployApp(e, providerName);
            }
        }
        if (providerList.size() > 0) {
            final String adapterClass = deploymentUnit.getAttachment(JpaAttachments.ADAPTOR_CLASS_NAME);
            PersistenceProviderAdaptor adaptor;
            if (adapterClass != null) {
                try {
                    adaptor = (PersistenceProviderAdaptor) deploymentModuleClassLoader.loadClass(adapterClass).newInstance();
                    adaptor.injectJtaManager(new JtaManagerImpl(deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_MANAGER), deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_SYNCHRONIZATION_REGISTRY)));
                    adaptor.injectPlatform(platform);
                    ArrayList<PersistenceProviderAdaptor> adaptorList = new ArrayList<>();
                    adaptorList.add(adaptor);
                    PersistenceProviderDeploymentHolder.savePersistenceProviderInDeploymentUnit(deploymentUnit, providerList, adaptorList);
                } catch (InstantiationException e) {
                    throw JpaLogger.ROOT_LOGGER.cannotCreateAdapter(e, adapterClass);
                } catch (IllegalAccessException e) {
                    throw JpaLogger.ROOT_LOGGER.cannotCreateAdapter(e, adapterClass);
                } catch (ClassNotFoundException e) {
                    throw JpaLogger.ROOT_LOGGER.cannotCreateAdapter(e, adapterClass);
                }
            } else {
                // register the provider (no adapter specified)
                PersistenceProviderDeploymentHolder.savePersistenceProviderInDeploymentUnit(deploymentUnit, providerList, null);
            }
        }
    }
}
Also used : ServicesAttachment(org.jboss.as.server.deployment.ServicesAttachment) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) PersistenceProvider(javax.persistence.spi.PersistenceProvider) ArrayList(java.util.ArrayList) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) JtaManagerImpl(org.jboss.as.jpa.transaction.JtaManagerImpl) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) PersistenceProviderAdaptor(org.jipijapa.plugin.spi.PersistenceProviderAdaptor)

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