Search in sources :

Example 6 with Deployment

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

the class TransitiveResolutionTest method testBeansXmlMultipleEnabling.

/*
    * description = "WELD-319"
    */
@Test
public void testBeansXmlMultipleEnabling() {
    BeanDeploymentArchiveImpl jar1 = new BeanDeploymentArchiveImpl("first-jar", new BeansXmlImpl(Collections.singletonList(Alt.class.getName()), null, null, null), Alt.class);
    BeanDeploymentArchiveImpl jar2 = new BeanDeploymentArchiveImpl("second-jar", new BeansXmlImpl(Collections.singletonList(Alt2.class.getName()), Collections.<String>emptyList(), null, null), Alt2.class);
    BeanDeploymentArchiveImpl war = new BeanDeploymentArchiveImpl("war");
    war.getBeanDeploymentArchives().add(jar1);
    war.getBeanDeploymentArchives().add(jar2);
    Deployment deployment = new FlatDeployment(war);
    TestContainer container = null;
    try {
        container = new TestContainer(deployment).startContainer().ensureRequestActive();
        BeanManagerImpl warBeanManager = (BeanManagerImpl) container.getBeanManager(war);
        BeanManagerImpl jar1BeanManager = (BeanManagerImpl) container.getBeanManager(jar1);
        BeanManagerImpl jar2BeanManager = (BeanManagerImpl) container.getBeanManager(jar2);
        Assert.assertTrue(warBeanManager.getEnabled().getAlternativeClasses().isEmpty());
        Assert.assertFalse(jar1BeanManager.getEnabled().getAlternativeClasses().isEmpty());
        Assert.assertFalse(jar2BeanManager.getEnabled().getAlternativeClasses().isEmpty());
    } finally {
        if (container != null) {
            container.stopContainer();
        }
    }
}
Also used : TestContainer(org.jboss.arquillian.container.weld.embedded.mock.TestContainer) BeanDeploymentArchiveImpl(org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) Deployment(org.jboss.weld.bootstrap.spi.Deployment) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) Test(org.testng.annotations.Test)

Example 7 with Deployment

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

the class DuplicateBeanArchiveIdTest method test.

@Test(expectedExceptions = { DeploymentException.class })
public void test() {
    // Override equals/hashcode - bda is used in a map
    final BeanDeploymentArchive bda1 = new BeanDeploymentArchiveImpl("1", Foo.class) {

        @Override
        public boolean equals(Object obj) {
            return (this == obj);
        }

        @Override
        public int hashCode() {
            return System.identityHashCode(this);
        }
    };
    final BeanDeploymentArchive bda2 = new BeanDeploymentArchiveImpl("1", Bar.class) {

        @Override
        public boolean equals(Object obj) {
            return (this == obj);
        }

        @Override
        public int hashCode() {
            return System.identityHashCode(this);
        }
    };
    Deployment deployment = new FlatDeployment(new BeanDeploymentArchive[] { bda1, bda2 }) {

        @Override
        public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
            return loadBeanDeploymentArchive(beanClass);
        }
    };
    TestContainer container = new TestContainer(deployment);
    container.startContainer();
    container.stopContainer();
}
Also used : TestContainer(org.jboss.arquillian.container.weld.embedded.mock.TestContainer) BeanDeploymentArchiveImpl(org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) Deployment(org.jboss.weld.bootstrap.spi.Deployment) Test(org.testng.annotations.Test)

Example 8 with Deployment

use of org.jboss.weld.bootstrap.spi.Deployment 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 9 with Deployment

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

the class NonBdaExtensionTest method test.

/*
    * description = "WELD-233"
    */
@Test
public void test() {
    // Create the BDA in which we will deploy Observer1 and Foo. This is equivalent to a war or ejb jar
    final BeanDeploymentArchiveImpl bda1 = new BeanDeploymentArchiveImpl("1", Observer1.class, Foo.class);
    // Create the BDA to return from loadBeanDeploymentArchive for Observer2, this is probably a library, though could be another war or ejb jar
    // bda2 is accessible from bda1, but isn't added to it's accessibility graph by default. This similar to an archive which doesn't contain a beans.xml but does contain an extension
    final BeanDeploymentArchive bda2 = new BeanDeploymentArchiveImpl("2", Observer2.class);
    // Create a deployment, that we can use to mirror the structure of one Extension inside a BDA, and one outside
    Deployment deployment = new FlatDeployment(bda1, new Observer1(), new Observer2(), new CountingObserver1(), new CountingObserver2()) {

        public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
            // Return bda2 if it is Observer2. Stick anything else which this test isn't about in bda1
            if (beanClass.equals(Observer2.class)) {
                // If Observer2 is requested, then we need to add bda2 to the accessibility graph of bda1
                bda1.getBeanDeploymentArchives().add(bda2);
                return bda2;
            } else {
                return bda1;
            }
        }
    };
    TestContainer container = new TestContainer(deployment);
    // Cause the container to deploy the beans etc.
    container.startContainer();
    // Get the bean manager for bda1 and bda2
    BeanManager beanManager1 = container.getBeanManager(bda1);
    BeanManager beanManager2 = container.getBeanManager(bda2);
    Observer1 observer1 = Utils.getReference(beanManager1, Observer1.class);
    Assert.assertTrue(observer1.isBeforeBeanDiscoveryCalled());
    Assert.assertEquals(beanManager1, observer1.getBeforeBeanDiscoveryBeanManager());
    Assert.assertTrue(observer1.isAfterBeanDiscoveryCalled());
    Assert.assertTrue(observer1.isAfterDeploymentValidationCalled());
    Assert.assertTrue(observer1.isProcessInjectionTargetCalled());
    Assert.assertTrue(observer1.isProcessManagedBeanCalled());
    Assert.assertTrue(observer1.isProcessProducerCalled());
    Assert.assertEquals(1, beanManager2.getBeans(Observer2.class).size());
    // Also check that the accessibility graph has been updated
    Assert.assertEquals(1, beanManager1.getBeans(Observer2.class).size());
    Observer2 observer2 = Utils.getReference(beanManager2, Observer2.class);
    Assert.assertTrue(observer2.isBeforeBeanDiscoveryCalled());
    Assert.assertEquals(beanManager2, observer2.getBeforeBeanDiscoveryBeanManager());
    Assert.assertTrue(observer2.isAfterBeanDiscoveryCalled());
    Assert.assertTrue(observer2.isAfterDeploymentValidationCalled());
    Assert.assertTrue(observer2.isProcessInjectionTargetCalled());
    Assert.assertTrue(observer2.isProcessManagedBeanCalled());
    Assert.assertTrue(observer2.isProcessProducerCalled());
    container.stopContainer();
}
Also used : TestContainer(org.jboss.arquillian.container.weld.embedded.mock.TestContainer) BeanDeploymentArchiveImpl(org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) Deployment(org.jboss.weld.bootstrap.spi.Deployment) BeanManager(javax.enterprise.inject.spi.BeanManager) Test(org.testng.annotations.Test)

Example 10 with Deployment

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

the class NonBdaExtensionTest method testEventsSentOnceOnly.

/*
    * description = "WELD-258"
    */
@Test
public void testEventsSentOnceOnly() {
    // Create the BDA in which we will deploy Observer1 and Foo. This is equivalent to a war or ejb jar
    final BeanDeploymentArchiveImpl bda1 = new BeanDeploymentArchiveImpl("1", CountingObserver1.class, Foo.class);
    // Create the BDA to return from loadBeanDeploymentArchive for Observer2, this is probably a library, though could be another war or ejb jar
    // bda2 is accessible from bda1, but isn't added to it's accessibility graph by default. This similar to an archive which doesn't contain a beans.xml but does contain an extension
    final BeanDeploymentArchive bda2 = new BeanDeploymentArchiveImpl("2", CountingObserver2.class);
    // Create a deployment, that we can use to mirror the structure of one Extension inside a BDA, and one outside
    Deployment deployment = new FlatDeployment(bda1, new Observer1(), new Observer2(), new CountingObserver1(), new CountingObserver2()) {

        public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
            // Return bda2 if it is Observer2. Stick anything else which this test isn't about in bda1
            if (beanClass.equals(CountingObserver2.class) || beanClass.equals(Bar.class)) {
                // If Observer2 is requested, then we need to add bda2 to the accessibility graph of bda1
                bda1.getBeanDeploymentArchives().add(bda2);
                return bda2;
            } else {
                return bda1;
            }
        }
    };
    TestContainer container = new TestContainer(deployment);
    // Cause the container to deploy the beans etc.
    container.startContainer();
    // Get the bean manager for bda1 and bda2
    BeanManager beanManager1 = container.getBeanManager(bda1);
    CountingObserver1 observer1 = Utils.getReference(beanManager1, CountingObserver1.class);
    CountingObserver2 observer2 = Utils.getReference(beanManager1, CountingObserver2.class);
    Assert.assertEquals(1, observer1.getBeforeBeanDiscovery());
    Assert.assertEquals(1, observer1.getProcessFooManagedBean());
    Assert.assertEquals(1, observer1.getProcessBarManagedBean());
    Assert.assertEquals(1, observer2.getBeforeBeanDiscovery());
    Assert.assertEquals(1, observer2.getProcessFooManagedBean());
    Assert.assertEquals(1, observer2.getProcessBarManagedBean());
    container.stopContainer();
}
Also used : TestContainer(org.jboss.arquillian.container.weld.embedded.mock.TestContainer) BeanDeploymentArchiveImpl(org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) Deployment(org.jboss.weld.bootstrap.spi.Deployment) BeanManager(javax.enterprise.inject.spi.BeanManager) Test(org.testng.annotations.Test)

Aggregations

Deployment (org.jboss.weld.bootstrap.spi.Deployment)13 BeanDeploymentArchiveImpl (org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl)11 FlatDeployment (org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment)11 TestContainer (org.jboss.arquillian.container.weld.embedded.mock.TestContainer)10 Test (org.testng.annotations.Test)10 BeanDeploymentArchive (org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)7 BeanManager (javax.enterprise.inject.spi.BeanManager)5 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)3 WeldBootstrap (org.jboss.weld.bootstrap.WeldBootstrap)2 Environment (org.jboss.weld.bootstrap.api.Environment)2 BeansXml (org.jboss.weld.bootstrap.spi.BeansXml)2 WeldDeployment (org.jboss.weld.environment.deployment.WeldDeployment)2 Annotation (java.lang.annotation.Annotation)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 AnnotationLiteral (javax.enterprise.util.AnnotationLiteral)1 Service (org.jboss.weld.bootstrap.api.Service)1 TypeDiscoveryConfiguration (org.jboss.weld.bootstrap.api.TypeDiscoveryConfiguration)1 Metadata (org.jboss.weld.bootstrap.spi.Metadata)1