Search in sources :

Example 1 with ExternalConfigurationBuilder

use of org.jboss.weld.configuration.spi.helpers.ExternalConfigurationBuilder 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 ExternalConfigurationBuilder

use of org.jboss.weld.configuration.spi.helpers.ExternalConfigurationBuilder in project wildfly by wildfly.

the class WeldDeploymentProcessor method installBootstrapConfigurationService.

private void installBootstrapConfigurationService(WeldDeployment deployment, DeploymentUnit parentDeploymentUnit) {
    final boolean nonPortableMode = parentDeploymentUnit.getAttachment(WeldConfiguration.ATTACHMENT_KEY).isNonPortableMode();
    final ExternalConfiguration configuration = new ExternalConfigurationBuilder().add(ConfigurationKey.NON_PORTABLE_MODE.get(), nonPortableMode).build();
    deployment.getServices().add(ExternalConfiguration.class, configuration);
}
Also used : ExternalConfigurationBuilder(org.jboss.weld.configuration.spi.helpers.ExternalConfigurationBuilder) ExternalConfiguration(org.jboss.weld.configuration.spi.ExternalConfiguration)

Example 3 with ExternalConfigurationBuilder

use of org.jboss.weld.configuration.spi.helpers.ExternalConfigurationBuilder in project core by weld.

the class WeldServletLifecycle method initialize.

/**
 * @param context
 * @return <code>true</code> if initialized properly, <code>false</code> otherwise
 */
boolean initialize(ServletContext context) {
    isDevModeEnabled = Boolean.valueOf(context.getInitParameter(CONTEXT_PARAM_DEV_MODE));
    WeldManager manager = (WeldManager) context.getAttribute(BEAN_MANAGER_ATTRIBUTE_NAME);
    if (manager != null) {
        isBootstrapNeeded = false;
        String contextId = BeanManagerProxy.unwrap(manager).getContextId();
        context.setInitParameter(org.jboss.weld.Container.CONTEXT_ID_KEY, contextId);
    } else {
        Object container = context.getAttribute(Listener.CONTAINER_ATTRIBUTE_NAME);
        if (container instanceof ContainerInstanceFactory) {
            ContainerInstanceFactory factory = (ContainerInstanceFactory) container;
            // start the container
            ContainerInstance containerInstance = factory.initialize();
            container = containerInstance;
            // we are in charge of shutdown also
            this.shutdownAction = () -> containerInstance.shutdown();
        }
        if (container instanceof ContainerInstance) {
            // the container instance was either passed to us directly or was created in the block above
            ContainerInstance containerInstance = (ContainerInstance) container;
            manager = BeanManagerProxy.unwrap(containerInstance.getBeanManager());
            context.setInitParameter(org.jboss.weld.Container.CONTEXT_ID_KEY, containerInstance.getId());
            isBootstrapNeeded = false;
        }
    }
    final CDI11Bootstrap bootstrap = new WeldBootstrap();
    if (isBootstrapNeeded) {
        final CDI11Deployment deployment = createDeployment(context, bootstrap);
        deployment.getServices().add(ExternalConfiguration.class, new ExternalConfigurationBuilder().add(BEAN_IDENTIFIER_INDEX_OPTIMIZATION.get(), Boolean.FALSE.toString()).build());
        if (deployment.getBeanDeploymentArchives().isEmpty()) {
            // Skip initialization - there is no bean archive in the deployment
            CommonLogger.LOG.initSkippedNoBeanArchiveFound();
            return false;
        }
        ResourceInjectionServices resourceInjectionServices = new ServletResourceInjectionServices() {
        };
        try {
            for (BeanDeploymentArchive archive : deployment.getBeanDeploymentArchives()) {
                archive.getServices().add(ResourceInjectionServices.class, resourceInjectionServices);
            }
        } catch (NoClassDefFoundError e) {
            // Support GAE
            WeldServletLogger.LOG.resourceInjectionNotAvailable();
        }
        String id = context.getInitParameter(org.jboss.weld.Container.CONTEXT_ID_KEY);
        if (id != null) {
            bootstrap.startContainer(id, Environments.SERVLET, deployment);
        } else {
            bootstrap.startContainer(Environments.SERVLET, deployment);
        }
        bootstrap.startInitialization();
        /*
             * Determine the BeanManager used for example for EL resolution - this should work fine as all bean archives share the same classloader. The only
             * difference this can make is per-BDA (CDI 1.0 style) enablement of alternatives, interceptors and decorators. Nothing we can do about that.
             *
             * First try to find the bean archive for WEB-INF/classes. If not found, take the first one available.
             */
        for (BeanDeploymentArchive bda : deployment.getBeanDeploymentArchives()) {
            if (bda.getId().contains(ManagerObjectFactory.WEB_INF_CLASSES_FILE_PATH) || bda.getId().contains(ManagerObjectFactory.WEB_INF_CLASSES)) {
                manager = bootstrap.getManager(bda);
                break;
            }
        }
        if (manager == null) {
            manager = bootstrap.getManager(deployment.getBeanDeploymentArchives().iterator().next());
        }
        // Push the manager into the servlet context so we can access in JSF
        context.setAttribute(BEAN_MANAGER_ATTRIBUTE_NAME, manager);
    }
    ContainerContext containerContext = new ContainerContext(context, manager);
    StringBuilder dump = new StringBuilder();
    Container container = findContainer(containerContext, dump);
    if (container == null) {
        WeldServletLogger.LOG.noSupportedServletContainerDetected();
        WeldServletLogger.LOG.debugv("Exception dump from Container lookup: {0}", dump);
    } else {
        container.initialize(containerContext);
        this.container = container;
    }
    if (Reflections.isClassLoadable(WeldClassLoaderResourceLoader.INSTANCE, JSP_FACTORY_CLASS_NAME) && JspFactory.getDefaultFactory() != null) {
        JspApplicationContext jspApplicationContext = JspFactory.getDefaultFactory().getJspApplicationContext(context);
        // Register the ELResolver with JSP
        jspApplicationContext.addELResolver(manager.getELResolver());
        // Register ELContextListener with JSP
        try {
            jspApplicationContext.addELContextListener(new WeldELContextListener());
        } catch (Exception e) {
            throw WeldServletLogger.LOG.errorLoadingWeldELContextListener(e);
        }
        // Push the wrapped expression factory into the servlet context so that Tomcat or Jetty can hook it in using a container code
        context.setAttribute(EXPRESSION_FACTORY_NAME, manager.wrapExpressionFactory(jspApplicationContext.getExpressionFactory()));
    }
    if (isBootstrapNeeded) {
        bootstrap.deployBeans().validateBeans().endInitialization();
        if (isDevModeEnabled) {
            FilterRegistration.Dynamic filterDynamic = context.addFilter("Weld Probe Filter", DevelopmentMode.PROBE_FILTER_CLASS_NAME);
            filterDynamic.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), true, "/*");
        }
        this.shutdownAction = () -> bootstrap.shutdown();
    }
    return true;
}
Also used : ServletResourceInjectionServices(org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices) ResourceInjectionServices(org.jboss.weld.injection.spi.ResourceInjectionServices) JspApplicationContext(javax.servlet.jsp.JspApplicationContext) CDI11Bootstrap(org.jboss.weld.bootstrap.api.CDI11Bootstrap) WeldBootstrap(org.jboss.weld.bootstrap.WeldBootstrap) ExternalConfigurationBuilder(org.jboss.weld.configuration.spi.helpers.ExternalConfigurationBuilder) CDI11Deployment(org.jboss.weld.bootstrap.spi.CDI11Deployment) WeldManager(org.jboss.weld.manager.api.WeldManager) ContainerInstance(org.jboss.weld.environment.ContainerInstance) UndertowContainer(org.jboss.weld.environment.undertow.UndertowContainer) JettyContainer(org.jboss.weld.environment.jetty.JettyContainer) TomcatContainer(org.jboss.weld.environment.tomcat.TomcatContainer) GwtDevHostedModeContainer(org.jboss.weld.environment.gwtdev.GwtDevHostedModeContainer) ContainerInstanceFactory(org.jboss.weld.environment.ContainerInstanceFactory) WeldBeanDeploymentArchive(org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) WeldELContextListener(org.jboss.weld.module.web.el.WeldELContextListener) ServletResourceInjectionServices(org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices) FilterRegistration(javax.servlet.FilterRegistration)

Aggregations

ExternalConfigurationBuilder (org.jboss.weld.configuration.spi.helpers.ExternalConfigurationBuilder)3 WeldBootstrap (org.jboss.weld.bootstrap.WeldBootstrap)2 FilterRegistration (javax.servlet.FilterRegistration)1 JspApplicationContext (javax.servlet.jsp.JspApplicationContext)1 CDI11Bootstrap (org.jboss.weld.bootstrap.api.CDI11Bootstrap)1 BeanDeploymentArchive (org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)1 CDI11Deployment (org.jboss.weld.bootstrap.spi.CDI11Deployment)1 Deployment (org.jboss.weld.bootstrap.spi.Deployment)1 ExternalConfiguration (org.jboss.weld.configuration.spi.ExternalConfiguration)1 ContainerInstance (org.jboss.weld.environment.ContainerInstance)1 ContainerInstanceFactory (org.jboss.weld.environment.ContainerInstanceFactory)1 WeldBeanDeploymentArchive (org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive)1 WeldDeployment (org.jboss.weld.environment.deployment.WeldDeployment)1 GwtDevHostedModeContainer (org.jboss.weld.environment.gwtdev.GwtDevHostedModeContainer)1 JettyContainer (org.jboss.weld.environment.jetty.JettyContainer)1 ServletResourceInjectionServices (org.jboss.weld.environment.servlet.services.ServletResourceInjectionServices)1 TomcatContainer (org.jboss.weld.environment.tomcat.TomcatContainer)1 UndertowContainer (org.jboss.weld.environment.undertow.UndertowContainer)1 ResourceInjectionServices (org.jboss.weld.injection.spi.ResourceInjectionServices)1 WeldManager (org.jboss.weld.manager.api.WeldManager)1