Search in sources :

Example 1 with PropertyReplacingBeansXmlParser

use of org.jboss.as.weld.deployment.PropertyReplacingBeansXmlParser 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;
    }
    PropertyReplacingBeansXmlParser parser = new PropertyReplacingBeansXmlParser(deploymentUnit);
    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, deploymentUnit), 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, deploymentUnit), true));
            } else {
                WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", rootBeansXml);
                beanArchiveMetadata.put(deploymentRoot, new ExplicitBeanArchiveMetadata(rootBeansXml, classesRoot, parseBeansXml(rootBeansXml, parser, deploymentUnit), true));
            }
        } else if (beansXmlPresent) {
            WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", beansXml);
            beanArchiveMetadata.put(deploymentRoot, new ExplicitBeanArchiveMetadata(beansXml, classesRoot, parseBeansXml(beansXml, parser, deploymentUnit), 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, deploymentUnit), 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) HashMap(java.util.HashMap) ExplicitBeanArchiveMetadata(org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadata) PropertyReplacingBeansXmlParser(org.jboss.as.weld.deployment.PropertyReplacingBeansXmlParser) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) ExplicitBeanArchiveMetadataContainer(org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadataContainer)

Example 2 with PropertyReplacingBeansXmlParser

use of org.jboss.as.weld.deployment.PropertyReplacingBeansXmlParser 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.";
    final List<DeploymentUnit> deploymentUnits = new ArrayList<DeploymentUnit>();
    deploymentUnits.add(deploymentUnit);
    deploymentUnits.addAll(deploymentUnit.getAttachmentList(Attachments.SUB_DEPLOYMENTS));
    PropertyReplacingBeansXmlParser parser = new PropertyReplacingBeansXmlParser(deploymentUnit);
    final HashSet<URL> existing = new HashSet<URL>();
    for (DeploymentUnit deployment : deploymentUnits) {
        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));
    for (DeploymentUnit deployment : deploymentUnits) {
        final Module module = deployment.getAttachment(Attachments.MODULE);
        if (module == null) {
            return;
        }
        for (DependencySpec dep : module.getDependencies()) {
            final Module dependency = loadModuleDependency(dep);
            if (dependency == null) {
                continue;
            }
            Set<URL> urls = findExportedLocalBeansXml(dependency);
            if (urls != null) {
                List<BeanDeploymentArchiveImpl> moduleBdas = new ArrayList<>();
                for (URL url : urls) {
                    if (existing.contains(url)) {
                        continue;
                    }
                    /*
                         * Workaround for http://java.net/jira/browse/JAVASERVERFACES-2837
                         */
                    if (url.toString().contains("jsf-impl-2.2")) {
                        continue;
                    }
                    /*
                         * Workaround for resteasy-cdi bundling beans.xml
                         */
                    if (url.toString().contains("resteasy-cdi")) {
                        continue;
                    }
                    WeldLogger.DEPLOYMENT_LOGGER.debugf("Found external beans.xml: %s", url.toString());
                    final BeansXml beansXml = parseBeansXml(url, parser, deploymentUnit);
                    final UrlScanner urlScanner = new UrlScanner();
                    final List<String> discoveredClasses = new ArrayList<String>();
                    if (!urlScanner.handleBeansXml(url, discoveredClasses)) {
                        continue;
                    }
                    discoveredClasses.removeAll(componentClassNames);
                    final BeanDeploymentArchiveImpl bda = new BeanDeploymentArchiveImpl(new HashSet<String>(discoveredClasses), beansXml, dependency, beanArchiveIdPrefix + url.toExternalForm(), BeanArchiveType.EXTERNAL);
                    WeldLogger.DEPLOYMENT_LOGGER.beanArchiveDiscovered(bda);
                    // Add module services to external bean deployment archive
                    for (Entry<Class<? extends Service>, Service> entry : ServiceLoaders.loadModuleServices(moduleServicesProviders, deploymentUnit, deployment, module, null).entrySet()) {
                        bda.getServices().add(entry.getKey(), Reflections.cast(entry.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(url);
                }
                //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) ArrayList(java.util.ArrayList) URL(java.net.URL) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) UrlScanner(org.jboss.as.weld.deployment.UrlScanner) BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) DependencySpec(org.jboss.modules.DependencySpec) ModuleDependencySpec(org.jboss.modules.ModuleDependencySpec) 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) PropertyReplacingBeansXmlParser(org.jboss.as.weld.deployment.PropertyReplacingBeansXmlParser) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit)

Aggregations

DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)2 ExplicitBeanArchiveMetadata (org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadata)2 ExplicitBeanArchiveMetadataContainer (org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadataContainer)2 PropertyReplacingBeansXmlParser (org.jboss.as.weld.deployment.PropertyReplacingBeansXmlParser)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1 ComponentDescription (org.jboss.as.ee.component.ComponentDescription)1 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)1 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)1 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)1 BeanDeploymentArchiveImpl (org.jboss.as.weld.deployment.BeanDeploymentArchiveImpl)1 UrlScanner (org.jboss.as.weld.deployment.UrlScanner)1 ComponentSupport (org.jboss.as.weld.spi.ComponentSupport)1 ModuleServicesProvider (org.jboss.as.weld.spi.ModuleServicesProvider)1 DependencySpec (org.jboss.modules.DependencySpec)1 Module (org.jboss.modules.Module)1