Search in sources :

Example 1 with Deployment

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

the class Weld method initialize.

/**
 * Bootstraps a new Weld SE container with the current container id (generated value if not set through {@link #containerId(String)}).
 * <p/>
 * The container must be shut down properly when an application is stopped. Applications are encouraged to use the try-with-resources statement or invoke
 * {@link WeldContainer#shutdown()} explicitly.
 * <p/>
 * However, a shutdown hook is also registered during initialization so that all running containers are shut down automatically when a program exits or VM
 * is terminated. This means that it's not necessary to implement the shutdown logic in a class where a main method is used to start the container.
 *
 * @return the Weld container
 * @see #enableDiscovery()
 * @see WeldContainer#shutdown()
 */
public WeldContainer initialize() {
    // If also building a synthetic bean archive or the implicit scan is enabled, the check for beans.xml is not necessary
    if (!isSyntheticBeanArchiveRequired() && !isImplicitScanEnabled() && resourceLoader.getResource(WeldDeployment.BEANS_XML) == null) {
        throw CommonLogger.LOG.missingBeansXml();
    }
    final WeldBootstrap bootstrap = new WeldBootstrap();
    final Deployment deployment = createDeployment(resourceLoader, bootstrap);
    final ExternalConfigurationBuilder configurationBuilder = new ExternalConfigurationBuilder().add(EXECUTOR_THREAD_POOL_TYPE.get(), COMMON.toString()).add(ConfigurationKey.RELAXED_CONSTRUCTION.get(), true).add(ConfigurationKey.ALLOW_OPTIMIZED_CLEANUP.get(), isEnabled(ALLOW_OPTIMIZED_CLEANUP, true));
    for (Entry<String, Object> property : properties.entrySet()) {
        String key = property.getKey();
        if (SHUTDOWN_HOOK_SYSTEM_PROPERTY.equals(key) || ARCHIVE_ISOLATION_SYSTEM_PROPERTY.equals(key) || DEV_MODE_SYSTEM_PROPERTY.equals(key) || SCAN_CLASSPATH_ENTRIES_SYSTEM_PROPERTY.equals(key) || JAVAX_ENTERPRISE_INJECT_SCAN_IMPLICIT.equals(key)) {
            continue;
        }
        configurationBuilder.add(key, property.getValue());
    }
    deployment.getServices().add(ExternalConfiguration.class, configurationBuilder.build());
    final String containerId = this.containerId != null ? this.containerId : UUID.randomUUID().toString();
    bootstrap.startContainer(containerId, Environments.SE, deployment);
    final WeldContainer weldContainer = WeldContainer.startInitialization(containerId, deployment, bootstrap);
    try {
        bootstrap.startInitialization();
        bootstrap.deployBeans();
        bootstrap.validateBeans();
        bootstrap.endInitialization();
        WeldContainer.endInitialization(weldContainer, isEnabled(SHUTDOWN_HOOK_SYSTEM_PROPERTY, true));
        initializedContainers.put(containerId, weldContainer);
    } catch (Throwable e) {
        // Discard the container if a bootstrap problem occurs, e.g. validation error
        WeldContainer.discard(weldContainer.getId());
        throw e;
    }
    return weldContainer;
}
Also used : WeldBootstrap(org.jboss.weld.bootstrap.WeldBootstrap) ExternalConfigurationBuilder(org.jboss.weld.configuration.spi.helpers.ExternalConfigurationBuilder) Deployment(org.jboss.weld.bootstrap.spi.Deployment) WeldDeployment(org.jboss.weld.environment.deployment.WeldDeployment)

Example 2 with Deployment

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

the class SubclassedComponentDescriptorTest method prepareContainer.

@BeforeClass
public void prepareContainer() {
    final EjbDescriptor<Foo> fooDescriptor = new EjbDescriptorImpl<Foo>(Foo.class, Foo.class, EnhancedFoo.class, SessionBeanType.STATEFUL);
    final EjbDescriptor<Bar> barDescriptor = new EjbDescriptorImpl<Bar>(Bar.class, BarLocal.class, EnhancedBar.class, SessionBeanType.STATEFUL);
    final EjbDescriptor<Baz> bazDescriptor = new EjbDescriptorImpl<Baz>(Baz.class, Baz.class, EnhancedBaz.class, null);
    final BeanDeploymentArchive bda = new BeanDeploymentArchiveImpl("1", Foo.class, Bar.class, BarLocal.class, BarDecorator.class, BarInterceptor.class, BarInterceptorBinding.class, Baz.class) {

        @Override
        public Collection<EjbDescriptor<?>> getEjbs() {
            return ImmutableSet.<EjbDescriptor<?>>of(fooDescriptor, barDescriptor, bazDescriptor);
        }
    };
    final Deployment deployment = new FlatDeployment(bda) {

        @Override
        public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
            return bda;
        }

        @Override
        protected void configureServices(Environment environment) {
            super.configureServices(environment);
            getServices().add(EjbServices.class, new MockEjbServices());
        }
    };
    container = new TestContainer(deployment).startContainer();
    manager = (BeanManagerImpl) container.getBeanManager(bda);
}
Also used : TestContainer(org.jboss.arquillian.container.weld.embedded.mock.TestContainer) BeanDeploymentArchiveImpl(org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl) Deployment(org.jboss.weld.bootstrap.spi.Deployment) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) EjbDescriptor(org.jboss.weld.ejb.spi.EjbDescriptor) MockEjbServices(org.jboss.weld.mock.MockEjbServices) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) Environment(org.jboss.weld.bootstrap.api.Environment) AfterClass(org.testng.annotations.AfterClass) BeforeClass(org.testng.annotations.BeforeClass) BeforeClass(org.testng.annotations.BeforeClass)

Example 3 with Deployment

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

the class StaticResourceTest method testStaticResourceInjectionWithInjectionServices.

@Test
public void testStaticResourceInjectionWithInjectionServices() {
    // Create the BDA in which we will deploy FooExtension. This BDA does not have access to bda2
    final BeanDeploymentArchive bda = new BeanDeploymentArchiveImpl("1", InjectedClass.class) {

        @Override
        protected void configureServices(Environment environment) {
            getServices().add(InjectionServices.class, new TestInjectionServices());
        }
    };
    // Create a deployment, that we can use to mirror the structure of one Extension inside a BDA, and one outside
    Deployment deployment = new FlatDeployment(new BeanDeploymentArchive[] { bda }) {

        @Override
        protected void configureServices(Environment environment) {
            super.configureServices(environment);
            getServices().add(TransactionServices.class, new TestTransactionServices());
        }

        @Override
        public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
            return bda;
        }
    };
    final WeldBootstrap bootstrap = new WeldBootstrap();
    bootstrap.startContainer(Environments.EE, deployment).startInitialization().deployBeans().validateBeans().endInitialization();
    try {
        final BeanManagerImpl manager = bootstrap.getManager(bda);
        final SpecialResource resource = manager.instance().select(SpecialResource.class).get();
        Assert.assertNotNull(resource);
        Assert.assertEquals(resource.getName(), TestInjectionServices.RESOURCE_NAME);
    } finally {
        bootstrap.shutdown();
    }
}
Also used : BeanDeploymentArchiveImpl(org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) WeldBootstrap(org.jboss.weld.bootstrap.WeldBootstrap) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) Environment(org.jboss.weld.bootstrap.api.Environment) Deployment(org.jboss.weld.bootstrap.spi.Deployment) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) Test(org.testng.annotations.Test)

Example 4 with Deployment

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

the class TransitiveResolutionTest method testDecoratorEnabledInWarButPackagedInEjbJar.

/*
    * WELD-507
    */
@Test
public void testDecoratorEnabledInWarButPackagedInEjbJar() {
    // Create the BDA in which we will deploy Foo. This is equivalent to a ejb
    // jar
    final BeanDeploymentArchiveImpl ejbJar = new BeanDeploymentArchiveImpl("ejb-jar", Blah.class, BlahDecorator.class, BlahImpl.class);
    // Create the BDA in which we will deploy Bar. This is equivalent to a war
    BeansXml beansXml = new BeansXmlImpl(null, null, Collections.singletonList(BlahDecorator.class.getName()), null);
    final BeanDeploymentArchiveImpl war = new BeanDeploymentArchiveImpl("war", beansXml, BlahImpl2.class);
    // The war can access the ejb jar
    war.getBeanDeploymentArchives().add(ejbJar);
    // Create a deployment, any other classes are put into the ejb-jar (not
    // relevant to test)
    Deployment deployment = new FlatDeployment(new BeanDeploymentArchive[] { war, ejbJar }) {

        public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
            return ejbJar;
        }
    };
    TestContainer container = new TestContainer(deployment);
    container.startContainer();
    container.ensureRequestActive();
    // Get the bean manager for war and ejb jar
    BeanManager warBeanManager = container.getBeanManager(war);
    BeanManager ejbJarBeanManager = container.getBeanManager(ejbJar);
    BasicInterceptor.reset();
    Blah blah = getReference(ejbJarBeanManager, Blah.class);
    blah.ping(10);
    assertEquals(10, blah.getI());
    BasicInterceptor.reset();
    blah = getReference(warBeanManager, Blah.class, new AnnotationLiteral<Baz>() {
    });
    blah.ping(10);
    assertEquals(11, blah.getI());
    container.stopContainer();
}
Also used : TestContainer(org.jboss.arquillian.container.weld.embedded.mock.TestContainer) BeanDeploymentArchiveImpl(org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl) AnnotationLiteral(javax.enterprise.util.AnnotationLiteral) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) Deployment(org.jboss.weld.bootstrap.spi.Deployment) FlatDeployment(org.jboss.arquillian.container.weld.embedded.mock.FlatDeployment) BeanManager(javax.enterprise.inject.spi.BeanManager) Test(org.testng.annotations.Test)

Example 5 with Deployment

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

the class TransitiveResolutionTest method testBeansXmlIsolation.

/*
    * description = "WELD-319"
    */
@Test
public void testBeansXmlIsolation() {
    BeanDeploymentArchiveImpl jar1 = new BeanDeploymentArchiveImpl("first-jar", new BeansXmlImpl(Collections.singletonList(Alt.class.getName()), null, null, null), Alt.class);
    BeanDeploymentArchiveImpl jar2 = new BeanDeploymentArchiveImpl("second-jar", 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.assertTrue(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)

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