Search in sources :

Example 1 with WeldBeanDeploymentArchive

use of org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive in project core by weld.

the class WeldServletLifecycle method createDeployment.

/**
 * Create servlet deployment.
 *
 * Can be overridden with custom servlet deployment. e.g. exact resources listing in restricted env like GAE
 *
 * @param context the servlet context
 * @param bootstrap the bootstrap
 * @return new servlet deployment
 */
protected CDI11Deployment createDeployment(ServletContext context, CDI11Bootstrap bootstrap) {
    ImmutableSet.Builder<Metadata<Extension>> extensionsBuilder = ImmutableSet.builder();
    extensionsBuilder.addAll(bootstrap.loadExtensions(WeldResourceLoader.getClassLoader()));
    if (isDevModeEnabled) {
        extensionsBuilder.add(new MetadataImpl<Extension>(DevelopmentMode.getProbeExtension(resourceLoader), "N/A"));
    }
    final Iterable<Metadata<Extension>> extensions = extensionsBuilder.build();
    final TypeDiscoveryConfiguration typeDiscoveryConfiguration = bootstrap.startExtensions(extensions);
    final EEModuleDescriptor eeModule = new EEModuleDescriptorImpl(context.getContextPath(), ModuleType.WEB);
    final DiscoveryStrategy strategy = DiscoveryStrategyFactory.create(resourceLoader, bootstrap, typeDiscoveryConfiguration.getKnownBeanDefiningAnnotations(), Boolean.parseBoolean(context.getInitParameter(Jandex.DISABLE_JANDEX_DISCOVERY_STRATEGY)));
    if (Jandex.isJandexAvailable(resourceLoader)) {
        try {
            Class<? extends BeanArchiveHandler> handlerClass = Reflections.loadClass(resourceLoader, JANDEX_SERVLET_CONTEXT_BEAN_ARCHIVE_HANDLER);
            strategy.registerHandler((SecurityActions.newConstructorInstance(handlerClass, new Class<?>[] { ServletContext.class }, context)));
        } catch (Exception e) {
            throw CommonLogger.LOG.unableToInstantiate(JANDEX_SERVLET_CONTEXT_BEAN_ARCHIVE_HANDLER, Arrays.toString(new Object[] { context }), e);
        }
    } else {
        strategy.registerHandler(new ServletContextBeanArchiveHandler(context));
    }
    strategy.setScanner(new WebAppBeanArchiveScanner(resourceLoader, bootstrap, context));
    Set<WeldBeanDeploymentArchive> beanDeploymentArchives = strategy.performDiscovery();
    String isolation = context.getInitParameter(CONTEXT_PARAM_ARCHIVE_ISOLATION);
    if (isolation == null || Boolean.valueOf(isolation)) {
        CommonLogger.LOG.archiveIsolationEnabled();
    } else {
        CommonLogger.LOG.archiveIsolationDisabled();
        Set<WeldBeanDeploymentArchive> flatDeployment = new HashSet<WeldBeanDeploymentArchive>();
        flatDeployment.add(WeldBeanDeploymentArchive.merge(bootstrap, beanDeploymentArchives));
        beanDeploymentArchives = flatDeployment;
    }
    for (BeanDeploymentArchive archive : beanDeploymentArchives) {
        archive.getServices().add(EEModuleDescriptor.class, eeModule);
    }
    CDI11Deployment deployment = new WeldDeployment(resourceLoader, bootstrap, beanDeploymentArchives, extensions) {

        @Override
        protected WeldBeanDeploymentArchive createAdditionalBeanDeploymentArchive() {
            WeldBeanDeploymentArchive archive = super.createAdditionalBeanDeploymentArchive();
            archive.getServices().add(EEModuleDescriptor.class, eeModule);
            return archive;
        }
    };
    if (strategy.getClassFileServices() != null) {
        deployment.getServices().add(ClassFileServices.class, strategy.getClassFileServices());
    }
    return deployment;
}
Also used : WeldBeanDeploymentArchive(org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive) Metadata(org.jboss.weld.bootstrap.spi.Metadata) CDI11Deployment(org.jboss.weld.bootstrap.spi.CDI11Deployment) EEModuleDescriptorImpl(org.jboss.weld.bootstrap.spi.helpers.EEModuleDescriptorImpl) WeldDeployment(org.jboss.weld.environment.deployment.WeldDeployment) DiscoveryStrategy(org.jboss.weld.environment.deployment.discovery.DiscoveryStrategy) Extension(javax.enterprise.inject.spi.Extension) ServletContextBeanArchiveHandler(org.jboss.weld.environment.servlet.deployment.ServletContextBeanArchiveHandler) TypeDiscoveryConfiguration(org.jboss.weld.bootstrap.api.TypeDiscoveryConfiguration) ImmutableSet(org.jboss.weld.util.collections.ImmutableSet) WebAppBeanArchiveScanner(org.jboss.weld.environment.servlet.deployment.WebAppBeanArchiveScanner) WeldBeanDeploymentArchive(org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) EEModuleDescriptor(org.jboss.weld.bootstrap.spi.EEModuleDescriptor) HashSet(java.util.HashSet)

Example 2 with WeldBeanDeploymentArchive

use of org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive in project core by weld.

the class Weld method createDeployment.

/**
 * <p>
 * Extensions to Weld SE can subclass and override this method to customize the deployment before weld boots up. For example, to add a custom
 * ResourceLoader, you would subclass Weld like so:
 * </p>
 *
 * <pre>
 * public class MyWeld extends Weld {
 *     protected Deployment createDeployment(ResourceLoader resourceLoader, CDI11Bootstrap bootstrap) {
 *         return super.createDeployment(new MyResourceLoader(), bootstrap);
 *     }
 * }
 * </pre>
 *
 * <p>
 * This could then be used as normal:
 * </p>
 *
 * <pre>
 * WeldContainer container = new MyWeld().initialize();
 * </pre>
 *
 * @param resourceLoader
 * @param bootstrap
 */
protected Deployment createDeployment(ResourceLoader resourceLoader, CDI11Bootstrap bootstrap) {
    final Iterable<Metadata<Extension>> extensions = getExtensions();
    final TypeDiscoveryConfiguration typeDiscoveryConfiguration = bootstrap.startExtensions(extensions);
    final Deployment deployment;
    final Set<WeldBeanDeploymentArchive> beanDeploymentArchives = new HashSet<WeldBeanDeploymentArchive>();
    final Map<Class<? extends Service>, Service> additionalServices = new HashMap<>(this.additionalServices);
    final Set<Class<? extends Annotation>> beanDefiningAnnotations = ImmutableSet.<Class<? extends Annotation>>builder().addAll(typeDiscoveryConfiguration.getKnownBeanDefiningAnnotations()).add(ThreadScoped.class).build();
    if (discoveryEnabled) {
        DiscoveryStrategy strategy = DiscoveryStrategyFactory.create(resourceLoader, bootstrap, beanDefiningAnnotations, isEnabled(Jandex.DISABLE_JANDEX_DISCOVERY_STRATEGY, false));
        if (isImplicitScanEnabled()) {
            strategy.setScanner(new ClassPathBeanArchiveScanner(bootstrap));
        }
        beanDeploymentArchives.addAll(strategy.performDiscovery());
        ClassFileServices classFileServices = strategy.getClassFileServices();
        if (classFileServices != null) {
            additionalServices.put(ClassFileServices.class, classFileServices);
        }
    }
    if (isSyntheticBeanArchiveRequired()) {
        ImmutableSet.Builder<String> beanClassesBuilder = ImmutableSet.builder();
        beanClassesBuilder.addAll(scanPackages());
        Set<String> setOfAllBeanClasses = beanClassesBuilder.build();
        // the creation process differs based on bean discovery mode
        if (BeanDiscoveryMode.ANNOTATED.equals(beanDiscoveryMode)) {
            // Annotated bean discovery mode, filter classes
            ImmutableSet.Builder<String> filteredSetbuilder = ImmutableSet.builder();
            for (String className : setOfAllBeanClasses) {
                Class<?> clazz = Reflections.loadClass(resourceLoader, className);
                if (clazz != null && Reflections.hasBeanDefiningAnnotation(clazz, beanDefiningAnnotations)) {
                    filteredSetbuilder.add(className);
                }
            }
            setOfAllBeanClasses = filteredSetbuilder.build();
        }
        WeldBeanDeploymentArchive syntheticBeanArchive = new WeldBeanDeploymentArchive(WeldDeployment.SYNTHETIC_BDA_ID, setOfAllBeanClasses, null, buildSyntheticBeansXml(), Collections.emptySet(), ImmutableSet.copyOf(beanClasses));
        beanDeploymentArchives.add(syntheticBeanArchive);
    }
    if (beanDeploymentArchives.isEmpty() && this.containerLifecycleObservers.isEmpty() && this.extensions.isEmpty()) {
        throw WeldSELogger.LOG.weldContainerCannotBeInitializedNoBeanArchivesFound();
    }
    Multimap<String, BeanDeploymentArchive> problems = BeanArchives.findBeanClassesDeployedInMultipleBeanArchives(beanDeploymentArchives);
    if (!problems.isEmpty()) {
        // Right now, we only log a warning for each bean class deployed in multiple bean archives
        for (Entry<String, Collection<BeanDeploymentArchive>> entry : problems.entrySet()) {
            WeldSELogger.LOG.beanClassDeployedInMultipleBeanArchives(entry.getKey(), WeldCollections.toMultiRowString(entry.getValue()));
        }
    }
    if (isEnabled(ARCHIVE_ISOLATION_SYSTEM_PROPERTY, true)) {
        deployment = new WeldDeployment(resourceLoader, bootstrap, beanDeploymentArchives, extensions);
        CommonLogger.LOG.archiveIsolationEnabled();
    } else {
        Set<WeldBeanDeploymentArchive> flatDeployment = new HashSet<WeldBeanDeploymentArchive>();
        flatDeployment.add(WeldBeanDeploymentArchive.merge(bootstrap, beanDeploymentArchives));
        deployment = new WeldDeployment(resourceLoader, bootstrap, flatDeployment, extensions);
        CommonLogger.LOG.archiveIsolationDisabled();
    }
    // Register additional services if a service with higher priority not present
    for (Entry<Class<? extends Service>, Service> entry : additionalServices.entrySet()) {
        Services.put(deployment.getServices(), entry.getKey(), entry.getValue());
    }
    return deployment;
}
Also used : WeldBeanDeploymentArchive(org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive) HashMap(java.util.HashMap) Metadata(org.jboss.weld.bootstrap.spi.Metadata) Deployment(org.jboss.weld.bootstrap.spi.Deployment) WeldDeployment(org.jboss.weld.environment.deployment.WeldDeployment) WeldDeployment(org.jboss.weld.environment.deployment.WeldDeployment) DiscoveryStrategy(org.jboss.weld.environment.deployment.discovery.DiscoveryStrategy) TypeDiscoveryConfiguration(org.jboss.weld.bootstrap.api.TypeDiscoveryConfiguration) ImmutableSet(org.jboss.weld.util.collections.ImmutableSet) HashSet(java.util.HashSet) ClassPathBeanArchiveScanner(org.jboss.weld.environment.deployment.discovery.ClassPathBeanArchiveScanner) Service(org.jboss.weld.bootstrap.api.Service) ThreadScoped(org.jboss.weld.environment.se.contexts.ThreadScoped) Annotation(java.lang.annotation.Annotation) ClassFileServices(org.jboss.weld.resources.spi.ClassFileServices) WeldBeanDeploymentArchive(org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) Collection(java.util.Collection)

Example 3 with WeldBeanDeploymentArchive

use of org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive 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 4 with WeldBeanDeploymentArchive

use of org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive in project core by weld.

the class BeanArchivesTest method testFindBeanClassesDeployedInMultipleBeanArchives.

@Test
public void testFindBeanClassesDeployedInMultipleBeanArchives() {
    String beanClass = "com.foo.Bar";
    BeanDeploymentArchive bda1 = new WeldBeanDeploymentArchive("foo", ImmutableList.of(beanClass), null);
    BeanDeploymentArchive bda2 = new WeldBeanDeploymentArchive("bar", ImmutableList.of(beanClass), null);
    Multimap<String, BeanDeploymentArchive> problems = BeanArchives.findBeanClassesDeployedInMultipleBeanArchives(Collections.singleton(bda1));
    assertTrue(problems.isEmpty());
    problems = BeanArchives.findBeanClassesDeployedInMultipleBeanArchives(ImmutableSet.of(bda1, bda2));
    assertFalse(problems.isEmpty());
    assertEquals(1, problems.size());
    Entry<String, Collection<BeanDeploymentArchive>> entry = problems.entrySet().iterator().next();
    assertEquals(beanClass, entry.getKey());
    assertEquals(2, entry.getValue().size());
    for (BeanDeploymentArchive bda : entry.getValue()) {
        if (!bda.getId().equals("foo") && !bda.getId().equals("bar")) {
            fail();
        }
    }
}
Also used : WeldBeanDeploymentArchive(org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive) WeldBeanDeploymentArchive(org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

WeldBeanDeploymentArchive (org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive)4 HashSet (java.util.HashSet)3 BeanDeploymentArchive (org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)3 Collection (java.util.Collection)2 TypeDiscoveryConfiguration (org.jboss.weld.bootstrap.api.TypeDiscoveryConfiguration)2 Metadata (org.jboss.weld.bootstrap.spi.Metadata)2 WeldDeployment (org.jboss.weld.environment.deployment.WeldDeployment)2 DiscoveryStrategy (org.jboss.weld.environment.deployment.discovery.DiscoveryStrategy)2 ImmutableSet (org.jboss.weld.util.collections.ImmutableSet)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Extension (javax.enterprise.inject.spi.Extension)1 Service (org.jboss.weld.bootstrap.api.Service)1 BeansXml (org.jboss.weld.bootstrap.spi.BeansXml)1 CDI11Deployment (org.jboss.weld.bootstrap.spi.CDI11Deployment)1 Deployment (org.jboss.weld.bootstrap.spi.Deployment)1 EEModuleDescriptor (org.jboss.weld.bootstrap.spi.EEModuleDescriptor)1 EEModuleDescriptorImpl (org.jboss.weld.bootstrap.spi.helpers.EEModuleDescriptorImpl)1 ScanResult (org.jboss.weld.environment.deployment.discovery.BeanArchiveScanner.ScanResult)1