Search in sources :

Example 16 with BeansXml

use of org.jboss.weld.bootstrap.spi.BeansXml in project core by weld.

the class WebAppBeanArchiveScanner method scan.

@Override
public List<ScanResult> scan() {
    // We use context path as a base for bean archive ids
    String contextPath = servletContext.getContextPath();
    List<ScanResult> results = new ArrayList<>(super.scan());
    // All previous results for WEB-INF/classes must be ignored
    for (Iterator<ScanResult> iterator = results.iterator(); iterator.hasNext(); ) {
        ScanResult result = iterator.next();
        String path = result.getBeanArchiveRef().toString();
        if (path.contains(WEB_INF_CLASSES_FILE_PATH) || path.contains(WEB_INF_CLASSES)) {
            iterator.remove();
        } else {
            result.extractBeanArchiveId(contextPath, WEB_INF);
        }
    }
    try {
        // WEB-INF/classes
        URL beansXmlUrl = null;
        for (String resource : RESOURCES) {
            URL resourceUrl;
            resourceUrl = servletContext.getResource(resource);
            if (resourceUrl != null) {
                if (beansXmlUrl != null) {
                    WeldServletLogger.LOG.foundBothConfiguration(beansXmlUrl);
                } else {
                    beansXmlUrl = resourceUrl;
                }
            }
        }
        if (beansXmlUrl != null) {
            BeansXml beansXml = parseBeansXml(beansXmlUrl);
            if (accept(beansXml)) {
                File webInfClasses = Servlets.getRealFile(servletContext, WEB_INF_CLASSES);
                if (webInfClasses != null) {
                    results.add(new ScanResult(beansXml, webInfClasses.getPath()).extractBeanArchiveId(contextPath, WEB_INF));
                } else {
                    // The WAR is not extracted to the file system - make use of ServletContext.getResourcePaths()
                    results.add(new ScanResult(beansXml, WEB_INF_CLASSES));
                }
            }
        }
    } catch (MalformedURLException e) {
        throw WeldServletLogger.LOG.errorLoadingResources(e);
    }
    return ImmutableList.copyOf(results);
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) File(java.io.File) URL(java.net.URL)

Example 17 with BeansXml

use of org.jboss.weld.bootstrap.spi.BeansXml in project core by weld.

the class ClassPathBeanArchiveScanner method scanDirectory.

private void scanDirectory(File entryDirectory, ImmutableList.Builder<ScanResult> results) throws IOException {
    // First try to find beans.xml
    File beansXmlFile = new File(entryDirectory, AbstractWeldDeployment.BEANS_XML);
    if (beansXmlFile.canRead()) {
        logger.debugv(BEANS_XML_FOUND_MESSAGE, entryDirectory);
        final BeansXml beansXml = parseBeansXml(beansXmlFile.toURI().toURL());
        if (accept(beansXml)) {
            results.add(new ScanResult(beansXml, entryDirectory.getPath()));
        }
    } else {
        // No beans.xml found - check whether the bean archive contains an extension
        logger.debugv(BEANS_XML_NOT_FOUND_MESSAGE, entryDirectory);
        File extensionFile = new File(entryDirectory, EXTENSION_FILE);
        if (!extensionFile.canRead()) {
            results.add(new ScanResult(null, entryDirectory.getPath()));
        }
    }
}
Also used : BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 18 with BeansXml

use of org.jboss.weld.bootstrap.spi.BeansXml in project core by weld.

the class AbstractDiscoveryStrategy method performDiscovery.

@Override
public Set<WeldBeanDeploymentArchive> performDiscovery() {
    if (scanner == null) {
        scanner = new DefaultBeanArchiveScanner(resourceLoader, bootstrap);
    }
    final List<BeanArchiveBuilder> beanArchiveBuilders = new ArrayList<BeanArchiveBuilder>();
    final Set<String> processedRefs = new HashSet<String>();
    List<BeanArchiveHandler> beanArchiveHandlers = initBeanArchiveHandlers();
    for (ScanResult scanResult : scanner.scan()) {
        final String ref = scanResult.getBeanArchiveRef();
        if (processedRefs.contains(ref)) {
            throw CommonLogger.LOG.invalidScanningResult(ref);
        }
        CommonLogger.LOG.processingBeanArchiveReference(ref);
        processedRefs.add(ref);
        BeanArchiveBuilder builder = null;
        for (BeanArchiveHandler handler : beanArchiveHandlers) {
            builder = handler.handle(ref);
            if (builder != null) {
                CommonLogger.LOG.beanArchiveReferenceHandled(ref, handler);
                builder.setId(scanResult.getBeanArchiveId());
                builder.setBeansXml(scanResult.getBeansXml());
                beanArchiveBuilders.add(builder);
                break;
            }
        }
        if (builder == null) {
            CommonLogger.LOG.beanArchiveReferenceCannotBeHandled(ref, beanArchiveHandlers);
        }
    }
    beforeDiscovery(beanArchiveBuilders);
    Set<WeldBeanDeploymentArchive> archives = new HashSet<WeldBeanDeploymentArchive>();
    for (Iterator<BeanArchiveBuilder> iterator = beanArchiveBuilders.iterator(); iterator.hasNext(); ) {
        BeanArchiveBuilder builder = iterator.next();
        BeansXml beansXml = builder.getBeansXml();
        if (beansXml != null) {
            switch(beansXml.getBeanDiscoveryMode()) {
                case ALL:
                    addToArchives(archives, processAllDiscovery(builder));
                    break;
                case ANNOTATED:
                    addToArchives(archives, processAnnotatedDiscovery(builder));
                    break;
                case NONE:
                    addToArchives(archives, processNoneDiscovery(builder));
                    break;
                default:
                    throw CommonLogger.LOG.undefinedBeanDiscoveryValue(beansXml.getBeanDiscoveryMode());
            }
        } else {
            // A candidate for an implicit bean archive with no beans.xml
            addToArchives(archives, processAnnotatedDiscovery(builder));
        }
    }
    for (WeldBeanDeploymentArchive archive : archives) {
        archive.getServices().add(ResourceLoader.class, resourceLoader);
    }
    afterDiscovery(archives);
    return archives;
}
Also used : ScanResult(org.jboss.weld.environment.deployment.discovery.BeanArchiveScanner.ScanResult) WeldBeanDeploymentArchive(org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive) ArrayList(java.util.ArrayList) BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) HashSet(java.util.HashSet)

Example 19 with BeansXml

use of org.jboss.weld.bootstrap.spi.BeansXml in project core by weld.

the class BeansXmlParser method merge.

// Merging utils
public static <T> BeansXml merge(Iterable<? extends T> items, Function<T, BeansXml> function, boolean removeDuplicates) {
    List<Metadata<String>> alternatives = new ArrayList<>();
    List<Metadata<String>> alternativeStereotypes = new ArrayList<>();
    List<Metadata<String>> decorators = new ArrayList<>();
    List<Metadata<String>> interceptors = new ArrayList<>();
    List<Metadata<Filter>> includes = new ArrayList<>();
    List<Metadata<Filter>> excludes = new ArrayList<>();
    boolean isTrimmed = false;
    URL beansXmlUrl = null;
    for (T item : items) {
        BeansXml beansXml = function.apply(item);
        addTo(alternatives, beansXml.getEnabledAlternativeClasses(), removeDuplicates);
        addTo(alternativeStereotypes, beansXml.getEnabledAlternativeStereotypes(), removeDuplicates);
        addTo(decorators, beansXml.getEnabledDecorators(), removeDuplicates);
        addTo(interceptors, beansXml.getEnabledInterceptors(), removeDuplicates);
        includes.addAll(beansXml.getScanning().getIncludes());
        excludes.addAll(beansXml.getScanning().getExcludes());
        isTrimmed = beansXml.isTrimmed();
        /*
             * provided we are merging the content of multiple XML files, getBeansXml() returns an InputStream representing the last one
             */
        beansXmlUrl = beansXml.getUrl();
    }
    return new BeansXmlImpl(alternatives, alternativeStereotypes, decorators, interceptors, new ScanningImpl(includes, excludes), beansXmlUrl, BeanDiscoveryMode.ALL, null, isTrimmed);
}
Also used : BeansXmlImpl(org.jboss.weld.metadata.BeansXmlImpl) Metadata(org.jboss.weld.bootstrap.spi.Metadata) ArrayList(java.util.ArrayList) BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) ScanningImpl(org.jboss.weld.metadata.ScanningImpl) URL(java.net.URL)

Example 20 with BeansXml

use of org.jboss.weld.bootstrap.spi.BeansXml in project core by weld.

the class JsonObjects method createDeploymentJson.

/**
 * @param beanManager
 * @return the root resource representation
 */
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "We want to catch all exceptions, runtime included.")
static String createDeploymentJson(BeanManagerImpl beanManager, Probe probe) {
    Map<BeanDeploymentArchive, BeanManagerImpl> beanDeploymentArchivesMap = Container.instance(beanManager).beanDeploymentArchives();
    AnnotationApiAbstraction annotationApi = beanManager.getServices().get(AnnotationApiAbstraction.class);
    JsonObjectBuilder deploymentBuilder = Json.objectBuilder();
    // INIT TS
    deploymentBuilder.add(INIT_TS, probe.getInitTs());
    // CONTEXT ID
    deploymentBuilder.add(CONTEXT_ID, beanManager.getContextId());
    // WELD VERSION
    deploymentBuilder.add(VERSION, Formats.getSimpleVersion());
    // BEAN DEPLOYMENT ARCHIVES
    JsonArrayBuilder bdasBuilder = Json.arrayBuilder();
    List<BeanDeploymentArchive> bdas = new ArrayList<BeanDeploymentArchive>(beanDeploymentArchivesMap.keySet());
    Collections.sort(bdas, probe.getBdaComparator());
    for (BeanDeploymentArchive bda : bdas) {
        JsonObjectBuilder bdaBuilder = createSimpleBdaJson(bda.getId());
        // If beans.xml is not found it's likely an implicit bean archive
        BeansXml beansXml = bda.getBeansXml();
        bdaBuilder.add(BEAN_DISCOVERY_MODE, beansXml != null ? beansXml.getBeanDiscoveryMode().toString() : BeanDiscoveryMode.ANNOTATED.toString());
        // beans.xml
        if (beansXml != null) {
            JsonObjectBuilder beansXmlBuilder = Json.objectBuilder();
            if (beansXml.equals(BeansXml.EMPTY_BEANS_XML)) {
                beansXmlBuilder.add(MARKER, Boolean.TRUE);
            } else {
                beansXmlBuilder.add(Strings.URL, beansXml.getUrl() != null ? beansXml.getUrl().toString() : EMPTY);
                beansXmlBuilder.add(VERSION, beansXml.getVersion() != null ? beansXml.getVersion().toString() : EMPTY);
                beansXmlBuilder.add(TRIMMED, beansXml.isTrimmed());
                if (beansXml.getScanning() != null && (!beansXml.getScanning().getExcludes().isEmpty() || !beansXml.getScanning().getExcludes().isEmpty())) {
                    JsonArrayBuilder scanBuilder = Json.arrayBuilder();
                    createMetadataArrayJson(scanBuilder, beansXml.getScanning().getExcludes(), EXCLUDE);
                    createMetadataArrayJson(scanBuilder, beansXml.getScanning().getIncludes(), INCLUDE);
                    beansXmlBuilder.add(SCAN, scanBuilder);
                }
            }
            bdaBuilder.add(Strings.BEANS_XML, beansXmlBuilder);
        }
        // Enablement - interceptors, decorators, alternatives
        JsonObjectBuilder enablementBuilder = Json.objectBuilder(true);
        ModuleEnablement enablement = beanDeploymentArchivesMap.get(bda).getEnabled();
        JsonArrayBuilder interceptors = Json.arrayBuilder();
        for (Class<?> interceptor : Components.getSortedProbeComponetCandidates(enablement.getInterceptors())) {
            Bean<?> interceptorBean = findEnabledBean(interceptor, BeanKind.INTERCEPTOR, probe);
            if (interceptorBean != null) {
                JsonObjectBuilder builder = decorateProbeComponent(interceptor, createSimpleBeanJson(interceptorBean, probe));
                if (beansXml != null) {
                    for (Metadata<String> meta : beansXml.getEnabledInterceptors()) {
                        if (meta.getValue().equals(interceptorBean.getBeanClass().getName())) {
                            // Locally enabled
                            builder.add(BEANS_XML, true);
                        }
                    }
                }
                Object priority = interceptorBean.getBeanClass().getAnnotation(annotationApi.PRIORITY_ANNOTATION_CLASS);
                if (priority != null) {
                    builder.add(PRIORITY, annotationApi.getPriority(priority));
                }
                if (builder.has(PRIORITY) && builder.has(BEANS_XML)) {
                    builder.add(CONFLICTS, true);
                }
                interceptors.add(builder);
            }
        }
        enablementBuilder.add(INTERCEPTORS, interceptors);
        JsonArrayBuilder decorators = Json.arrayBuilder();
        for (Class<?> decorator : enablement.getDecorators()) {
            Bean<?> decoratorBean = findEnabledBean(decorator, BeanKind.DECORATOR, probe);
            if (decoratorBean != null) {
                JsonObjectBuilder builder = createSimpleBeanJson(decoratorBean, probe);
                if (beansXml != null) {
                    for (Metadata<String> meta : beansXml.getEnabledDecorators()) {
                        if (meta.getValue().equals(decoratorBean.getBeanClass().getName())) {
                            // Locally enabled
                            builder.add(BEANS_XML, true);
                        }
                    }
                }
                Object priority = decoratorBean.getBeanClass().getAnnotation(annotationApi.PRIORITY_ANNOTATION_CLASS);
                if (priority != null) {
                    builder.add(PRIORITY, annotationApi.getPriority(priority));
                }
                if (builder.has(PRIORITY) && builder.has(BEANS_XML)) {
                    builder.add(CONFLICTS, true);
                }
                decorators.add(builder);
            }
        }
        enablementBuilder.add(DECORATORS, decorators);
        JsonArrayBuilder alternatives = Json.arrayBuilder();
        for (Class<?> clazz : Sets.union(enablement.getAlternativeClasses(), enablement.getGlobalAlternatives())) {
            Bean<?> alternativeBean = findAlternativeBean(clazz, probe);
            if (alternativeBean != null) {
                JsonObjectBuilder builder = createSimpleBeanJson(alternativeBean, probe);
                if (enablement.getAlternativeClasses().contains(clazz)) {
                    builder.add(BEANS_XML, true);
                }
                if (enablement.getGlobalAlternatives().contains(clazz)) {
                    Object priority = clazz.getAnnotation(annotationApi.PRIORITY_ANNOTATION_CLASS);
                    if (priority != null) {
                        builder.add(PRIORITY, annotationApi.getPriority(priority));
                    }
                }
                alternatives.add(builder);
            }
        }
        for (Class<? extends Annotation> stereotype : enablement.getAlternativeStereotypes()) {
            Set<Bean<?>> beans = findAlternativeStereotypeBeans(stereotype, probe);
            if (!beans.isEmpty()) {
                for (Bean<?> bean : beans) {
                    JsonObjectBuilder builder = createSimpleBeanJson(bean, probe);
                    builder.add(BEANS_XML, true);
                    alternatives.add(builder);
                }
            }
        }
        enablementBuilder.add(ALTERNATIVES, alternatives);
        bdaBuilder.add(ENABLEMENT, enablementBuilder);
        // Accessible BDAs
        BeanManagerImpl manager = beanDeploymentArchivesMap.get(bda);
        JsonArrayBuilder accesibleBdasBuilder = Json.arrayBuilder();
        for (BeanManagerImpl accesible : manager.getAccessibleManagers()) {
            accesibleBdasBuilder.add(Components.getId(accesible.getId()));
        }
        bdaBuilder.add(ACCESSIBLE_BDAS, accesibleBdasBuilder);
        bdaBuilder.add(BEANS, Components.getNumberOfEnabledBeans(manager));
        bdasBuilder.add(bdaBuilder);
    }
    deploymentBuilder.add(BDAS, bdasBuilder);
    // CONFIGURATION
    JsonArrayBuilder configBuilder = Json.arrayBuilder();
    WeldConfiguration configuration = beanManager.getServices().get(WeldConfiguration.class);
    for (ConfigurationKey key : Reports.getSortedConfigurationKeys()) {
        Object defaultValue = key.getDefaultValue();
        String desc = Reports.getDesc(key);
        if (desc == null) {
            // Don't show config options without description
            continue;
        }
        Object value = Reports.getValue(key, configuration);
        if (value == null) {
            // Unsupported property type
            continue;
        }
        configBuilder.add(Json.objectBuilder().add(NAME, key.get()).add(DEFAULT_VALUE, defaultValue.toString()).add(VALUE, value.toString()).add(DESCRIPTION, desc));
    }
    deploymentBuilder.add(CONFIGURATION, configBuilder);
    // INSPECTABLE CONTEXTS
    deploymentBuilder.add(CONTEXTS, createContextsJson(beanManager, probe));
    // DASHBOARD DATA
    JsonObjectBuilder dashboardBuilder = Json.objectBuilder();
    // Application
    JsonObjectBuilder appBuilder = Json.objectBuilder();
    appBuilder.add(BEANS, probe.getApplicationBeansCount());
    appBuilder.add(OBSERVERS, probe.getApplicationObserversCount());
    dashboardBuilder.add(APPLICATION, appBuilder);
    // Bootstrap
    dashboardBuilder.add(BOOSTRAP_STATS, createBootstrapStatsJson(probe));
    deploymentBuilder.add(DASHBOARD, dashboardBuilder);
    return deploymentBuilder.build();
}
Also used : AnnotationApiAbstraction(org.jboss.weld.util.AnnotationApiAbstraction) ModuleEnablement(org.jboss.weld.bootstrap.enablement.ModuleEnablement) ArrayList(java.util.ArrayList) SessionBean(org.jboss.weld.bean.SessionBean) AbstractClassBean(org.jboss.weld.bean.AbstractClassBean) AbstractBuiltInBean(org.jboss.weld.bean.builtin.AbstractBuiltInBean) AbstractProducerBean(org.jboss.weld.bean.AbstractProducerBean) Bean(javax.enterprise.inject.spi.Bean) ConfigurationKey(org.jboss.weld.config.ConfigurationKey) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) JsonArrayBuilder(org.jboss.weld.probe.Json.JsonArrayBuilder) JsonObjectBuilder(org.jboss.weld.probe.Json.JsonObjectBuilder) WeldConfiguration(org.jboss.weld.config.WeldConfiguration) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

BeansXml (org.jboss.weld.bootstrap.spi.BeansXml)34 Test (org.testng.annotations.Test)12 URL (java.net.URL)9 BeanDeploymentArchive (org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)6 ArrayList (java.util.ArrayList)5 File (java.io.File)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 Metadata (org.jboss.weld.bootstrap.spi.Metadata)3 EjbDescriptor (com.sun.enterprise.deployment.EjbDescriptor)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 ZipFile (java.util.zip.ZipFile)2 Bean (javax.enterprise.inject.spi.Bean)2 BeanManager (javax.enterprise.inject.spi.BeanManager)2 DeploymentContext (org.glassfish.api.deployment.DeploymentContext)2 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)2 BeanDeploymentArchiveImpl (org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl)2