Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
   *
   * Use is subject to license terms.
   *
   * JBoss, Home of Professional Open Source
   * Copyright 2008, Red Hat, Inc., 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.servlet;
 
 import static org.jboss.weld.servlet.ConversationFilter.CONVERSATION_FILTER_REGISTERED;
 import static org.jboss.weld.servlet.api.InitParameters.CONVERSATION_CONTEXT_LAZY_PARAM;
 
 import java.util.List;
 
 import  javax.servlet.ServletContext;
 import  javax.servlet.ServletContextEvent;
 import  javax.servlet.ServletRequestEvent;
 import  javax.servlet.http.HttpServletRequest;
 import  javax.servlet.http.HttpSessionEvent;
 
The initial Weld listener. It should always be registered as the first listener, before any other (application) listeners.

Listens for context/session/request creation/destruction.

Delegates work to the HttpContextLifecycle.

Author(s):
Nicklas Karlsson
Dan Allen
Ales Justin
Lincoln Baxter, III
Jozef Hartinger
Marko Luksa
 
 public class WeldInitialListener extends AbstractServletListener {
 
     private static final String CONTEXT_IGNORE_GUARD_PARAMETER = "org.jboss.weld.context.ignore.guard";
 
     @Inject
     private BeanManagerImpl beanManager;
     private HttpContextLifecycle lifecycle;
 
     @Override
     public void contextInitialized(ServletContextEvent sce) {
         final ServletContext ctx = sce.getServletContext();
         // First try to use the context id obtained from the servlet context (OSGi, Servlet containers, etc.)
         if ( == null) {
             String contextId = ctx.getInitParameter(.);
             if (contextId != null) {
                 List<BeanManagerImplmanagers = new ArrayList<BeanManagerImpl>(Container.instance(contextId).beanDeploymentArchives().values());
                 Collections.sort(managers.);
                  = managers.get(0);
             }
         }
         // servlet containers may not be able to inject fields in a servlet listener
         if ( == null) {
              = BeanManagerProxy.unwrap(CDI.current().getBeanManager());
         }
         HttpContextActivationFilter filter = ServletUtils.getContextActivationFilter(ctx);
         final boolean ignoreForwards = getBooleanInitParameter(ctx.false);
         final boolean ignoreIncludes = getBooleanInitParameter(ctx.false);
         final boolean nestedInvocationGuard = getBooleanInitParameter(ctxtrue);
         final boolean lazyConversationContext = initLazyConversationContext(ctx);
         this. = new HttpContextLifecycle(filterignoreForwardsignoreIncludeslazyConversationContextnestedInvocationGuard);
         if (Boolean.valueOf(ctx.getInitParameter())) {
             this..setConversationActivationEnabled(false);
        }
        this..contextInitialized(ctx);
        ctx.setAttribute(WeldInitialListener.class.getName(), this);
    }
    private boolean getBooleanInitParameter(ServletContext ctxString parameterNameboolean defaultValue) {
        String value = ctx.getInitParameter(parameterName);
        if (value == null) {
            return defaultValue;
        }
        return Boolean.valueOf(value);
    }

    
The lazy conversation context can be configured to be enabled or disabled in web.xml. If not configured, the default behavior depends on whether an observer for the Initialized(ConversationScoped.class) event is present or not. If an observer is present, the lazy conversation context is disabled by default. Otherwise, it is enabled.
    private boolean initLazyConversationContext(BeanManagerImpl manager, ServletContext ctx) {
        ObserverNotifier notifier = manager.getAccessibleLenientObserverNotifier();
        boolean noObservers = notifier.resolveObserverMethods(notifier.buildEventResolvable(HttpServletRequest.class.)).isEmpty()
                && notifier.resolveObserverMethods(notifier.buildEventResolvable(HttpServletRequest.class.)).isEmpty();
        return getBooleanInitParameter(ctxnoObservers);
    }
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        .contextDestroyed(sce.getServletContext());
    }
    @Override
    public void sessionCreated(HttpSessionEvent event) {
        .sessionCreated(event.getSession());
    }
    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        .sessionDestroyed(event.getSession());
    }
    @Override
    public void requestDestroyed(ServletRequestEvent event) {
        if (event.getServletRequest() instanceof HttpServletRequest) {
            .requestDestroyed((HttpServletRequest) event.getServletRequest());
        } else {
            throw ..onlyHttpServletLifecycleDefined();
        }
    }
    @Override
    public void requestInitialized(ServletRequestEvent event) {
        if (!.isConversationActivationSet()) {
            Object value = event.getServletContext().getAttribute();
            if (..equals(value)) {
                this..setConversationActivationEnabled(false);
            } else {
                this..setConversationActivationEnabled(true);
            }
        }
        if (event.getServletRequest() instanceof HttpServletRequest) {
            .requestInitialized((HttpServletRequest) event.getServletRequest(), event.getServletContext());
        } else {
            throw ..onlyHttpServletLifecycleDefined();
        }
    }
New to GrepCode? Check out our FAQ X