Search in sources :

Example 1 with ExplicitBeanArchiveMetadataContainer

use of org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadataContainer in project wildfly by wildfly.

the class WeldImplicitDeploymentProcessor method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (WeldDeploymentMarker.isWeldDeployment(deploymentUnit)) {
        return;
    }
    if (Utils.getRootDeploymentUnit(deploymentUnit).getAttachment(WeldConfiguration.ATTACHMENT_KEY).isRequireBeanDescriptor()) {
        // if running in the require-bean-descriptor mode then bean archives are found by BeansXmlProcessor
        return;
    }
    /*
         * look for classes with bean defining annotations
         */
    final Set<AnnotationType> beanDefiningAnnotations = new HashSet<>(getRootDeploymentUnit(deploymentUnit).getAttachment(WeldAttachments.BEAN_DEFINING_ANNOTATIONS));
    final Map<ResourceRoot, Index> indexes = AnnotationIndexUtils.getAnnotationIndexes(deploymentUnit);
    final ExplicitBeanArchiveMetadataContainer explicitBeanArchiveMetadata = deploymentUnit.getAttachment(ExplicitBeanArchiveMetadataContainer.ATTACHMENT_KEY);
    final ResourceRoot classesRoot = deploymentUnit.getAttachment(WeldAttachments.CLASSES_RESOURCE_ROOT);
    final ResourceRoot deploymentRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
    for (Entry<ResourceRoot, Index> entry : indexes.entrySet()) {
        ResourceRoot resourceRoot = entry.getKey();
        if (resourceRoot == classesRoot) {
            // BDA for WEB-INF/classes is keyed under deploymentRoot in explicitBeanArchiveMetadata
            resourceRoot = deploymentRoot;
        }
        /*
             * Make sure bean defining annotations used in archives with bean-discovery-mode="none" are not considered here
             * WFLY-4388
             */
        if (explicitBeanArchiveMetadata != null && explicitBeanArchiveMetadata.getBeanArchiveMetadata().containsKey(resourceRoot)) {
            continue;
        }
        for (final AnnotationType annotation : beanDefiningAnnotations) {
            if (!entry.getValue().getAnnotations(annotation.getName()).isEmpty()) {
                WeldDeploymentMarker.mark(deploymentUnit);
                return;
            }
        }
    }
    /*
         * look for session beans and managed beans
         */
    final EEModuleDescription eeModuleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
    final Iterable<ImplicitBeanArchiveDetector> detectors = ServiceLoader.load(ImplicitBeanArchiveDetector.class, WildFlySecurityManager.getClassLoaderPrivileged(WeldImplicitDeploymentProcessor.class));
    for (ComponentDescription component : eeModuleDescription.getComponentDescriptions()) {
        for (ImplicitBeanArchiveDetector detector : detectors) {
            if (detector.isImplicitBeanArchiveRequired(component)) {
                WeldDeploymentMarker.mark(deploymentUnit);
                return;
            }
        }
    }
}
Also used : ComponentDescription(org.jboss.as.ee.component.ComponentDescription) Index(org.jboss.jandex.Index) AnnotationType(org.jboss.as.weld.discovery.AnnotationType) ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) ImplicitBeanArchiveDetector(org.jboss.as.weld.spi.ImplicitBeanArchiveDetector) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) Utils.getRootDeploymentUnit(org.jboss.as.weld.util.Utils.getRootDeploymentUnit) ExplicitBeanArchiveMetadataContainer(org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadataContainer) HashSet(java.util.HashSet)

Example 2 with ExplicitBeanArchiveMetadataContainer

use of org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadataContainer in project wildfly by wildfly.

the class BeansXmlProcessor method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    Map<ResourceRoot, ExplicitBeanArchiveMetadata> beanArchiveMetadata = new HashMap<ResourceRoot, ExplicitBeanArchiveMetadata>();
    ResourceRoot deploymentRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
    if (deploymentRoot == null) {
        return;
    }
    BeansXmlParser parser = BeansXmlParserFactory.getPropertyReplacingParser(deploymentUnit, Utils.getRootDeploymentUnit(deploymentUnit).getAttachment(WeldConfiguration.ATTACHMENT_KEY).isLegacyEmptyBeansXmlTreatment());
    ResourceRoot classesRoot = null;
    List<ResourceRoot> structure = deploymentUnit.getAttachmentList(Attachments.RESOURCE_ROOTS);
    for (ResourceRoot resourceRoot : structure) {
        if (ModuleRootMarker.isModuleRoot(resourceRoot) && !SubDeploymentMarker.isSubDeployment(resourceRoot)) {
            if (resourceRoot.getRootName().equals("classes")) {
                // hack for dealing with war modules
                classesRoot = resourceRoot;
                deploymentUnit.putAttachment(WeldAttachments.CLASSES_RESOURCE_ROOT, resourceRoot);
            } else {
                VirtualFile beansXml = resourceRoot.getRoot().getChild(META_INF_BEANS_XML);
                if (beansXml.exists() && beansXml.isFile()) {
                    WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", beansXml.toString());
                    beanArchiveMetadata.put(resourceRoot, new ExplicitBeanArchiveMetadata(beansXml, resourceRoot, parseBeansXml(beansXml, parser), false));
                }
            }
        }
    }
    if (DeploymentTypeMarker.isType(DeploymentType.WAR, deploymentUnit)) {
        final VirtualFile rootBeansXml = deploymentRoot.getRoot().getChild(WEB_INF_BEANS_XML);
        final boolean rootBeansXmlPresent = rootBeansXml.exists() && rootBeansXml.isFile();
        VirtualFile beansXml = null;
        if (classesRoot != null) {
            beansXml = classesRoot.getRoot().getChild(META_INF_BEANS_XML);
        }
        final boolean beansXmlPresent = beansXml != null && beansXml.exists() && beansXml.isFile();
        if (rootBeansXmlPresent) {
            if (beansXmlPresent) {
                // warn that it is not portable to use both locations at the same time
                WeldLogger.DEPLOYMENT_LOGGER.duplicateBeansXml();
                beanArchiveMetadata.put(deploymentRoot, new ExplicitBeanArchiveMetadata(rootBeansXml, beansXml, classesRoot, parseBeansXml(rootBeansXml, parser), true));
            } else {
                WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", rootBeansXml);
                beanArchiveMetadata.put(deploymentRoot, new ExplicitBeanArchiveMetadata(rootBeansXml, classesRoot, parseBeansXml(rootBeansXml, parser), true));
            }
        } else if (beansXmlPresent) {
            WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", beansXml);
            beanArchiveMetadata.put(deploymentRoot, new ExplicitBeanArchiveMetadata(beansXml, classesRoot, parseBeansXml(beansXml, parser), true));
        }
    } else if (!DeploymentTypeMarker.isType(DeploymentType.EAR, deploymentUnit)) {
        final VirtualFile rootBeansXml = deploymentRoot.getRoot().getChild(META_INF_BEANS_XML);
        if (rootBeansXml.exists() && rootBeansXml.isFile()) {
            WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", rootBeansXml.toString());
            beanArchiveMetadata.put(deploymentRoot, new ExplicitBeanArchiveMetadata(rootBeansXml, deploymentRoot, parseBeansXml(rootBeansXml, parser), true));
        }
    }
    if (!beanArchiveMetadata.isEmpty()) {
        ExplicitBeanArchiveMetadataContainer deploymentMetadata = new ExplicitBeanArchiveMetadataContainer(beanArchiveMetadata);
        deploymentUnit.putAttachment(ExplicitBeanArchiveMetadataContainer.ATTACHMENT_KEY, deploymentMetadata);
        for (Iterator<Entry<ResourceRoot, ExplicitBeanArchiveMetadata>> iterator = beanArchiveMetadata.entrySet().iterator(); iterator.hasNext(); ) {
            if (BeanDiscoveryMode.NONE != iterator.next().getValue().getBeansXml().getBeanDiscoveryMode()) {
                // mark the deployment as requiring CDI integration as long as it contains at least one bean archive with bean-discovery-mode other than "none"
                WeldDeploymentMarker.mark(deploymentUnit);
                break;
            }
        }
    }
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) Entry(java.util.Map.Entry) ExplicitBeanArchiveMetadata(org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadata) HashMap(java.util.HashMap) BeansXmlParser(org.jboss.weld.xml.BeansXmlParser) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) ExplicitBeanArchiveMetadataContainer(org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadataContainer)

Example 3 with ExplicitBeanArchiveMetadataContainer

use of org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadataContainer in project wildfly by wildfly.

the class ExternalBeanArchiveProcessor method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (!WeldDeploymentMarker.isPartOfWeldDeployment(deploymentUnit)) {
        return;
    }
    if (deploymentUnit.getParent() != null) {
        return;
    }
    final Set<String> componentClassNames = new HashSet<>();
    final ServiceLoader<ComponentSupport> supportServices = ServiceLoader.load(ComponentSupport.class, WildFlySecurityManager.getClassLoaderPrivileged(ExternalBeanArchiveProcessor.class));
    final String beanArchiveIdPrefix = deploymentUnit.getName() + ".external.";
    // This set is used for external bean archives with annotated discovery mode
    final Set<AnnotationType> beanDefiningAnnotations = new HashSet<>(deploymentUnit.getAttachment(WeldAttachments.BEAN_DEFINING_ANNOTATIONS));
    final List<DeploymentUnit> deploymentUnits = new ArrayList<DeploymentUnit>();
    deploymentUnits.add(deploymentUnit);
    deploymentUnits.addAll(deploymentUnit.getAttachmentList(Attachments.SUB_DEPLOYMENTS));
    BeansXmlParser parser = BeansXmlParserFactory.getPropertyReplacingParser(deploymentUnit, Utils.getRootDeploymentUnit(deploymentUnit).getAttachment(WeldConfiguration.ATTACHMENT_KEY).isLegacyEmptyBeansXmlTreatment());
    final HashSet<URL> existing = new HashSet<URL>();
    // build a set of all deployment unit names, used later on to skip processing of some dependencies
    final Set<String> depUnitNames = new HashSet<>();
    final String prefix = "deployment.";
    for (DeploymentUnit deployment : deploymentUnits) {
        depUnitNames.add(prefix + deployment.getName());
        try {
            final ExplicitBeanArchiveMetadataContainer weldDeploymentMetadata = deployment.getAttachment(ExplicitBeanArchiveMetadataContainer.ATTACHMENT_KEY);
            if (weldDeploymentMetadata != null) {
                for (ExplicitBeanArchiveMetadata md : weldDeploymentMetadata.getBeanArchiveMetadata().values()) {
                    existing.add(md.getBeansXmlFile().toURL());
                    if (md.getAdditionalBeansXmlFile() != null) {
                        existing.add(md.getAdditionalBeansXmlFile().toURL());
                    }
                }
            }
        } catch (MalformedURLException e) {
            throw new DeploymentUnitProcessingException(e);
        }
        EEModuleDescription moduleDesc = deployment.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
        if (moduleDesc != null) {
            for (ComponentDescription component : moduleDesc.getComponentDescriptions()) {
                for (ComponentSupport support : supportServices) {
                    if (!support.isDiscoveredExternalType(component)) {
                        componentClassNames.add(component.getComponentClassName());
                        break;
                    }
                }
            }
        }
    }
    final ServiceLoader<ModuleServicesProvider> moduleServicesProviders = ServiceLoader.load(ModuleServicesProvider.class, WildFlySecurityManager.getClassLoaderPrivileged(WeldDeploymentProcessor.class));
    Set<String> skipPrecalculatedJandexModules = getSkipPrecalculatedJandexModules(deploymentUnit);
    // This map is a cache that allows us to avoid repeated introspection of Module's exported resources
    // it is of little importance for small deployment, but makes a difference in massive ones, see WFLY-14055
    Map<String, Map<URL, URL>> exportedResourcesCache = new HashMap<>();
    for (DeploymentUnit deployment : deploymentUnits) {
        final Module module = deployment.getAttachment(Attachments.MODULE);
        if (module == null) {
            return;
        }
        for (DependencySpec dep : module.getDependencies()) {
            if (!(dep instanceof ModuleDependencySpec)) {
                continue;
            }
            if (depUnitNames.contains(((ModuleDependencySpec) dep).getName())) {
                // we want to skip processing this as it will be (or already was) processed as another dep. unit
                continue;
            }
            final Module dependency = loadModuleDependency(dep);
            if (dependency == null) {
                continue;
            }
            Map<URL, URL> resourcesMap = findExportedResources(dependency, exportedResourcesCache);
            if (!resourcesMap.isEmpty()) {
                List<BeanDeploymentArchiveImpl> moduleBdas = new ArrayList<>();
                for (Entry<URL, URL> entry : resourcesMap.entrySet()) {
                    URL beansXmlUrl = entry.getKey();
                    if (existing.contains(beansXmlUrl)) {
                        continue;
                    }
                    /*
                         * Workaround for http://java.net/jira/browse/JAVASERVERFACES-2837
                         */
                    if (beansXmlUrl.toString().contains("jsf-impl-2.2")) {
                        continue;
                    }
                    /*
                         * Workaround for resteasy-cdi bundling beans.xml
                         */
                    if (beansXmlUrl.toString().contains("resteasy-cdi")) {
                        continue;
                    }
                    /*
                         * check if the dependency processes META-INF, if it doesn't we don't want to pick up beans
                         * See https://docs.wildfly.org/17/Developer_Guide.html#CDI_Reference
                         */
                    if (!dep.getImportFilter().accept("META-INF")) {
                        continue;
                    }
                    WeldLogger.DEPLOYMENT_LOGGER.debugf("Found external beans.xml: %s", beansXmlUrl.toString());
                    final BeansXml beansXml = parseBeansXml(beansXmlUrl, parser, deploymentUnit);
                    if (BeanDiscoveryMode.NONE.equals(beansXml.getBeanDiscoveryMode())) {
                        // Scanning suppressed per spec
                        continue;
                    }
                    final boolean skipPrecalculatedJandex = skipPrecalculatedJandexModules.contains(dependency.getName());
                    Map<String, List<String>> allAndBeanClasses = discover(beansXml.getBeanDiscoveryMode(), beansXmlUrl, entry.getValue(), beanDefiningAnnotations, skipPrecalculatedJandex);
                    Collection<String> discoveredBeanClasses = allAndBeanClasses.get(BEAN_CLASSES);
                    Collection<String> allKnownClasses = allAndBeanClasses.get(ALL_KNOWN_CLASSES);
                    if (discoveredBeanClasses == null) {
                        // URL scanner probably does not understand the protocol
                        continue;
                    }
                    discoveredBeanClasses.removeAll(componentClassNames);
                    final BeanDeploymentArchiveImpl bda = new BeanDeploymentArchiveImpl(new HashSet<String>(discoveredBeanClasses), new HashSet<String>(allKnownClasses), beansXml, dependency, beanArchiveIdPrefix + beansXmlUrl.toExternalForm(), BeanArchiveType.EXTERNAL);
                    WeldLogger.DEPLOYMENT_LOGGER.beanArchiveDiscovered(bda);
                    // Add module services to external bean deployment archive
                    for (Entry<Class<? extends Service>, Service> moduleService : ServiceLoaders.loadModuleServices(moduleServicesProviders, deploymentUnit, deployment, module, null).entrySet()) {
                        bda.getServices().add(moduleService.getKey(), Reflections.cast(moduleService.getValue()));
                    }
                    deploymentUnit.addToAttachmentList(WeldAttachments.ADDITIONAL_BEAN_DEPLOYMENT_MODULES, bda);
                    moduleBdas.add(bda);
                    // make sure that if this beans.xml is seen by some other module, it is not processed twice
                    existing.add(beansXmlUrl);
                }
                // BDA's from inside the same module have visibility on each other
                for (BeanDeploymentArchiveImpl i : moduleBdas) {
                    for (BeanDeploymentArchiveImpl j : moduleBdas) {
                        if (i != j) {
                            i.addBeanDeploymentArchive(j);
                        }
                    }
                }
            }
        }
    }
}
Also used : ComponentSupport(org.jboss.as.weld.spi.ComponentSupport) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) MalformedURLException(java.net.MalformedURLException) ComponentDescription(org.jboss.as.ee.component.ComponentDescription) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AnnotationType(org.jboss.as.weld.discovery.AnnotationType) URL(java.net.URL) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) DependencySpec(org.jboss.modules.DependencySpec) ModuleDependencySpec(org.jboss.modules.ModuleDependencySpec) List(java.util.List) ArrayList(java.util.ArrayList) ExplicitBeanArchiveMetadataContainer(org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadataContainer) HashSet(java.util.HashSet) ModuleServicesProvider(org.jboss.as.weld.spi.ModuleServicesProvider) ExplicitBeanArchiveMetadata(org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadata) BeanDeploymentArchiveImpl(org.jboss.as.weld.deployment.BeanDeploymentArchiveImpl) Service(org.jboss.weld.bootstrap.api.Service) ModuleDependencySpec(org.jboss.modules.ModuleDependencySpec) BeansXmlParser(org.jboss.weld.xml.BeansXmlParser) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) Utils.getRootDeploymentUnit(org.jboss.as.weld.util.Utils.getRootDeploymentUnit) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)3 ExplicitBeanArchiveMetadataContainer (org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadataContainer)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ComponentDescription (org.jboss.as.ee.component.ComponentDescription)2 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)2 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)2 ExplicitBeanArchiveMetadata (org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadata)2 AnnotationType (org.jboss.as.weld.discovery.AnnotationType)2 Utils.getRootDeploymentUnit (org.jboss.as.weld.util.Utils.getRootDeploymentUnit)2 BeansXmlParser (org.jboss.weld.xml.BeansXmlParser)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)1 BeanDeploymentArchiveImpl (org.jboss.as.weld.deployment.BeanDeploymentArchiveImpl)1 ComponentSupport (org.jboss.as.weld.spi.ComponentSupport)1