Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2014, Red Hat Middleware LLC, and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * http://www.apache.org/licenses/LICENSE-2.0
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.weld.environment.servlet;
 
 import java.util.Set;
 
 import  javax.servlet.ServletContext;
 import  javax.servlet.jsp.JspApplicationContext;
 import  javax.servlet.jsp.JspFactory;
 

Author(s):
Martin Kouba
See also:
Listener
EnhancedListener
 
 public class WeldServletLifecycle {
 
     public static final String BEAN_MANAGER_ATTRIBUTE_NAME = WeldServletLifecycle.class.getPackage().getName() + "." + BeanManager.class.getName();

    
Must be synchronized with org.jboss.weld.Container.CONTEXT_ID_KEY
 
     private static final String CONTEXT_ID_KEY = "WELD_CONTEXT_ID_KEY";
 
     static final String INSTANCE_ATTRIBUTE_NAME = WeldServletLifecycle.class.getPackage().getName() + ".lifecycleInstance";
 
     private static final String BOOTSTRAP_IMPL_CLASS_NAME = "org.jboss.weld.bootstrap.WeldBootstrap";
 
     private static final String WELD_LISTENER_CLASS_NAME = "org.jboss.weld.servlet.WeldInitialListener";
 
     private static final String EXPRESSION_FACTORY_NAME = "org.jboss.weld.el.ExpressionFactory";
 
     private static final String CONTEXT_PARAM_ARCHIVE_ISOLATION = WeldServletLifecycle.class.getPackage().getName() + ".archive.isolation";
 
     private final transient CDI11Bootstrap bootstrap;
 
     private final transient ServletListener weldListener;
 
     private Container container;
 
     // WELD-1665 Bootstrap might be already performed
     private boolean isBootstrapNeeded = true;
 
     WeldServletLifecycle() {
         try {
              = Reflections.newInstance();
         } catch (IllegalArgumentException e) {
             throw ..errorLoadingWeldBootstrap(e);
         }
         try {
             = Reflections.newInstance();
        } catch (IllegalArgumentException e) {
            throw ..errorLoadingWeldListener(e);
        }
    }

    

Parameters:
context
Returns:
true if initialized properly, false otherwise
    boolean initialize(ServletContext context) {
        WeldManager manager = (WeldManagercontext.getAttribute();
        if (manager != null) {
             = false;
        }
        if () {
            CDI11Deployment deployment = createDeployment(context);
            if (deployment.getBeanDeploymentArchives().isEmpty()) {
                // Skip initialization - there is no bean archive in the deployment
                ..initSkippedNoBeanArchiveFound();
                return false;
            }
            ResourceInjectionServices resourceInjectionServices = new ServletResourceInjectionServices() {
            };
            try {
                for (BeanDeploymentArchive archive : deployment.getBeanDeploymentArchives()) {
                    archive.getServices().add(ResourceInjectionServices.classresourceInjectionServices);
                }
            } catch (NoClassDefFoundError e) {
                // Support GAE
                ..resourceInjectionNotAvailable();
            }
            String id = context.getInitParameter();
            if (id != null) {
                .startContainer(id.deployment);
            } else {
                .startContainer(.deployment);
            }
            .startInitialization();
            /*
             * 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.
             */
            manager = .getManager(deployment.getBeanDeploymentArchives().iterator().next());
            // Push the manager into the servlet context so we can access in JSF
            context.setAttribute(manager);
        }
        ContainerContext containerContext = new ContainerContext(contextmanager);
        StringBuilder dump = new StringBuilder();
        Container container = findContainer(containerContextdump);
        if (container == null) {
            ..debugv("Exception dump from Container lookup: {0}"dump);
        } else {
            container.initialize(containerContext);
            this. = container;
        }
        if (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(Reflections.<ELContextListenernewInstance("org.jboss.weld.el.WeldELContextListener"));
            } catch (IllegalArgumentException e) {
                throw ..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(manager.wrapExpressionFactory(jspApplicationContext.getExpressionFactory()));
        }
        if () {
        }
        return true;
    }
    void destroy(ServletContext context) {
        if () {
            // Shutdown only if bootstrap not skipped
            .shutdown();
        }
        if ( != null) {
            .destroy(new ContainerContext(contextnull));
        }
    }

    

Returns:
the original Weld listener all notifications should be delegated to
        return ;
    }

    
Create servlet deployment. Can be overridden with custom servlet deployment. e.g. exact resources listing in restricted env like GAE

Parameters:
context the servlet context
bootstrap the bootstrap
Returns:
new servlet deployment
    protected CDI11Deployment createDeployment(ServletContext contextCDI11Bootstrap bootstrap) {
        ResourceLoader resourceLoader = new WeldResourceLoader();
        final Iterable<Metadata<Extension>> extensions = bootstrap.loadExtensions(WeldResourceLoader.getClassLoader());
        final TypeDiscoveryConfiguration typeDiscoveryConfiguration = bootstrap.startExtensions(extensions);
        DiscoveryStrategy strategy = DiscoveryStrategyFactory.create(resourceLoaderbootstraptypeDiscoveryConfiguration.getKnownBeanDefiningAnnotations());
        strategy.registerHandler(new ServletContextBeanArchiveHandler(context));
        strategy.setScanner(new WebAppBeanArchiveScanner(resourceLoaderbootstrapcontext));
        Set<WeldBeanDeploymentArchivebeanDeploymentArchives = strategy.performDiscovery();
        String isolation = context.getInitParameter();
        if (isolation != null && Boolean.valueOf(isolation).equals(.)) {
            ..archiveIsolationDisabled();
            beanDeploymentArchives = Collections.singleton(WeldBeanDeploymentArchive.merge(bootstrapbeanDeploymentArchives));
        } else {
            ..archiveIsolationEnabled();
        }
        CDI11Deployment deployment = new WeldDeployment(resourceLoaderbootstrapbeanDeploymentArchivesextensions);
        if (strategy.getClassFileServices() != null) {
            deployment.getServices().add(ClassFileServices.classstrategy.getClassFileServices());
        }
        return deployment;
    }

    
Find container env.

Parameters:
ctx the container context
dump the exception dump
Returns:
valid container or null
    protected Container findContainer(ContainerContext ctxStringBuilder dump) {
        Container container = null;
        // 1. Custom container class
        String containerClass = ctx.getServletContext().getInitParameter(.);
        if (containerClass != null) {
            try {
                container = Reflections.newInstance(containerClass);
                ..containerDetectionSkipped(containerClass);
            } catch (IllegalArgumentException e) {
                ..unableToInstantiateCustomContainerClass(containerClass);
            }
        }
        if (container == null) {
            // 2. Service providers
            Iterable<ContainerextContainers = ServiceLoader.load(Container.classgetClass().getClassLoader());
            container = checkContainers(ctxdumpextContainers);
            if (container == null) {
                // 3. Built-in containers in predefined order
                container = checkContainers(ctxdump, Arrays.asList(...));
            }
        }
        return container;
    }
    protected Container checkContainers(ContainerContext ccStringBuilder dumpIterable<Containercontainers) {
        for (Container c : containers) {
            try {
                if (c.touch(cc)) {
                    return c;
                }
            } catch (Throwable t) {
                dump.append(c).append("->").append(t.getMessage()).append("\n");
            }
        }
        return null;
    }
New to GrepCode? Check out our FAQ X