Search in sources :

Example 1 with CDI11Deployment

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

use of org.jboss.weld.bootstrap.spi.CDI11Deployment 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)

Example 3 with CDI11Deployment

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

the class WeldStartup method startContainer.

public WeldRuntime startContainer(String contextId, Environment environment, Deployment deployment) {
    if (deployment == null) {
        throw BootstrapLogger.LOG.deploymentRequired();
    }
    tracker.start(Tracker.OP_BOOTSTRAP);
    tracker.start(Tracker.OP_START_CONTAINER);
    checkApiVersion();
    final ServiceRegistry registry = deployment.getServices();
    // initiate part of registry in order to allow access to WeldConfiguration
    new AdditionalServiceLoader(deployment).loadAdditionalServices(registry);
    // Resource Loader has to be loaded prior to WeldConfiguration
    if (!registry.contains(ResourceLoader.class)) {
        registry.add(ResourceLoader.class, DefaultResourceLoader.INSTANCE);
    }
    WeldConfiguration configuration = new WeldConfiguration(registry, deployment);
    registry.add(WeldConfiguration.class, configuration);
    String finalContextId = BeanDeployments.getFinalId(contextId, registry.get(WeldConfiguration.class).getStringProperty(ROLLING_UPGRADES_ID_DELIMITER));
    this.contextId = finalContextId;
    this.deployment = deployment;
    this.environment = environment;
    if (this.extensions == null) {
        setExtensions(deployment.getExtensions());
    }
    // Add extension to register built-in components
    this.extensions.add(MetadataImpl.from(new WeldExtension()));
    // Add Weld extensions
    String vetoTypeRegex = configuration.getStringProperty(ConfigurationKey.VETO_TYPES_WITHOUT_BEAN_DEFINING_ANNOTATION);
    if (!vetoTypeRegex.isEmpty()) {
        this.extensions.add(MetadataImpl.from(new WeldVetoExtension(vetoTypeRegex)));
    }
    // Finish the rest of registry init, setupInitialServices() requires already changed finalContextId
    tracker.start(Tracker.OP_INIT_SERVICES);
    setupInitialServices();
    registry.addAll(initialServices.entrySet());
    if (!registry.contains(ProxyServices.class)) {
        registry.add(ProxyServices.class, new SimpleProxyServices());
    }
    if (!registry.contains(SecurityServices.class)) {
        registry.add(SecurityServices.class, NoopSecurityServices.INSTANCE);
    }
    addImplementationServices(registry);
    tracker.end();
    verifyServices(registry, environment.getRequiredDeploymentServices(), contextId);
    if (!registry.contains(TransactionServices.class)) {
        BootstrapLogger.LOG.jtaUnavailable();
    }
    this.deploymentManager = BeanManagerImpl.newRootManager(finalContextId, "deployment", registry);
    Container.initialize(finalContextId, deploymentManager, ServiceRegistries.unmodifiableServiceRegistry(deployment.getServices()));
    getContainer().setState(ContainerState.STARTING);
    tracker.start(Tracker.OP_CONTEXTS);
    this.contexts = createContexts(registry);
    tracker.end();
    this.bdaMapping = new BeanDeploymentArchiveMapping();
    this.deploymentVisitor = new DeploymentVisitor(deploymentManager, environment, deployment, contexts, bdaMapping);
    if (deployment instanceof CDI11Deployment) {
        registry.add(BeanManagerLookupService.class, new BeanManagerLookupService((CDI11Deployment) deployment, bdaMapping.getBdaToBeanManagerMap()));
    } else {
        BootstrapLogger.LOG.legacyDeploymentMetadataProvided();
    }
    // Read the deployment structure, bdaMapping will be the physical structure
    // as caused by the presence of beans.xml
    tracker.start(Tracker.OP_READ_DEPLOYMENT);
    deploymentVisitor.visit();
    tracker.end();
    WeldRuntime weldRuntime = new WeldRuntime(finalContextId, deploymentManager, bdaMapping.getBdaToBeanManagerMap());
    tracker.end();
    return weldRuntime;
}
Also used : ProxyServices(org.jboss.weld.serialization.spi.ProxyServices) SimpleProxyServices(org.jboss.weld.bean.proxy.util.SimpleProxyServices) DefaultResourceLoader(org.jboss.weld.resources.DefaultResourceLoader) ResourceLoader(org.jboss.weld.resources.spi.ResourceLoader) SecurityServices(org.jboss.weld.security.spi.SecurityServices) NoopSecurityServices(org.jboss.weld.security.NoopSecurityServices) BeanManagerLookupService(org.jboss.weld.manager.BeanManagerLookupService) CDI11Deployment(org.jboss.weld.bootstrap.spi.CDI11Deployment) SimpleProxyServices(org.jboss.weld.bean.proxy.util.SimpleProxyServices) TransactionServices(org.jboss.weld.transaction.spi.TransactionServices) ServiceRegistry(org.jboss.weld.bootstrap.api.ServiceRegistry) SimpleServiceRegistry(org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry) WeldConfiguration(org.jboss.weld.config.WeldConfiguration)

Aggregations

CDI11Deployment (org.jboss.weld.bootstrap.spi.CDI11Deployment)3 BeanDeploymentArchive (org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)2 WeldBeanDeploymentArchive (org.jboss.weld.environment.deployment.WeldBeanDeploymentArchive)2 HashSet (java.util.HashSet)1 Extension (javax.enterprise.inject.spi.Extension)1 FilterRegistration (javax.servlet.FilterRegistration)1 JspApplicationContext (javax.servlet.jsp.JspApplicationContext)1 SimpleProxyServices (org.jboss.weld.bean.proxy.util.SimpleProxyServices)1 WeldBootstrap (org.jboss.weld.bootstrap.WeldBootstrap)1 CDI11Bootstrap (org.jboss.weld.bootstrap.api.CDI11Bootstrap)1 ServiceRegistry (org.jboss.weld.bootstrap.api.ServiceRegistry)1 TypeDiscoveryConfiguration (org.jboss.weld.bootstrap.api.TypeDiscoveryConfiguration)1 SimpleServiceRegistry (org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry)1 EEModuleDescriptor (org.jboss.weld.bootstrap.spi.EEModuleDescriptor)1 Metadata (org.jboss.weld.bootstrap.spi.Metadata)1 EEModuleDescriptorImpl (org.jboss.weld.bootstrap.spi.helpers.EEModuleDescriptorImpl)1 WeldConfiguration (org.jboss.weld.config.WeldConfiguration)1 ExternalConfigurationBuilder (org.jboss.weld.configuration.spi.helpers.ExternalConfigurationBuilder)1 ContainerInstance (org.jboss.weld.environment.ContainerInstance)1 ContainerInstanceFactory (org.jboss.weld.environment.ContainerInstanceFactory)1