/*
 * 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 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.servlet;

import javax.enterprise.inject.spi.BeanManager;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletRequestEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionEvent;

import org.jboss.weld.BeanManagerImpl;
import org.jboss.weld.Container;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.context.ContextLifecycle;
import org.jboss.weld.servlet.api.ServletServices;
import org.jboss.weld.servlet.api.helpers.AbstractServletListener;

/**
 * The Weld listener
 * 
 * Listens for context/session creation/destruction.
 * 
 * Delegates work to the ServletLifeCycle.
 * 
 * @author Nicklas Karlsson
 *
 */
public class WeldListener extends AbstractServletListener
{
   
   private ServletLifecycle lifecycle;
   
   private ServletLifecycle getLifecycle()
   {
      if (lifecycle == null)
      {
         this.lifecycle = new ServletLifecycle(Container.instance().deploymentServices().get(ContextLifecycle.class));
      }
      return lifecycle;
   }
   
   private static BeanManagerImpl getBeanManager(ServletContext ctx)
   {
      BeanDeploymentArchive war = Container.instance().deploymentServices().get(ServletServices.class).getBeanDeploymentArchive(ctx);
      return Container.instance().beanDeploymentArchives().get(war);
   }
   
   @Override
   public void contextInitialized(ServletContextEvent sce)
   {
      super.contextInitialized(sce);
      sce.getServletContext().setAttribute(BeanManager.class.getName(), getBeanManager(sce.getServletContext()));
   }
   
   @Override
   public void contextDestroyed(ServletContextEvent sce)
   {
      sce.getServletContext().removeAttribute(BeanManager.class.getName());
      super.contextDestroyed(sce);
   }

   /**
    * Called when the session is created
    * 
    * @param event The session event
    */
   @Override
   public void sessionCreated(HttpSessionEvent event) 
   {
      // JBoss AS will still start the deployment even if WB fails
      if (Container.instance() != null && Container.instance().isInitialized())
      {
         getLifecycle().beginSession(event.getSession());
      }
   }

   /**
    * Called when the session is destroyed
    * 
    * @param event The session event
    */
   @Override
   public void sessionDestroyed(HttpSessionEvent event) 
   {
      // JBoss AS will still start the deployment even if WB fails
      if (Container.instance() != null && Container.instance().isInitialized())
      {
         getLifecycle().endSession(event.getSession());
      }
   }

   /**
    * Called when the request is destroyed
    * 
    * @param event The request event
    */
   @Override
   public void requestDestroyed(ServletRequestEvent event)
   {
      // JBoss AS will still start the deployment even if WB fails
      if (Container.instance() != null && Container.instance().isInitialized())
      {
         if (event.getServletRequest() instanceof HttpServletRequest)
         {
            getLifecycle().endRequest((HttpServletRequest) event.getServletRequest());
         }
         else
         {
            throw new IllegalStateException("Non HTTP-Servlet lifecycle not defined");
         }
      }
   }

   /**
    * Called when the request is initialized
    * 
    * @param event The request event
    */
   @Override
   public void requestInitialized(ServletRequestEvent event)
   {
      // JBoss AS will still start the deployment even if WB fails
      if (Container.instance() != null && Container.instance().isInitialized())
      {
         if (event.getServletRequest() instanceof HttpServletRequest)
         {
            getLifecycle().beginRequest((HttpServletRequest) event.getServletRequest());
         }
         else
         {
            throw new IllegalStateException("Non HTTP-Servlet lifecycle not defined");
         }
      }
   }

}

@@ -2274,156 +2274,902 @@
     
-return Container.instance().beanDeploymentArchives().get(war);%0A   %7D%0A   %0A   @Override%0A   public void contextInitialized(ServletContextEvent sce)%0A   %7B
+if (war == null)%0A      %7B%0A         throw new IllegalStateException(%22Unable to locate BeanDeploymentArchive. ServletContext: %22 + ctx);%0A      %7D%0A      BeanManagerImpl beanManager = Container.instance().beanDeploymentArchives().get(war);%0A      if (beanManager == null)%0A      %7B%0A         throw new IllegalStateException(%22Unable to locate BeanManager. ServletContext: %22 + ctx + %22; BeanDeploymentArchive: %22 + war);%0A      %7D%0A      return beanManager;%0A   %7D%0A   %0A   @Override%0A   public void contextInitialized(ServletContextEvent sce)%0A   %7B%0A      if (!Container.instance().isInitialized())%0A      %7B%0A         throw new IllegalStateException(%22Weld bootstrap must be complete before contextInitialized event%22);%0A      %7D%0A      if (!Container.instance().deploymentServices().contains(ServletServices.class))%0A      %7B%0A         throw new IllegalStateException(%22Cannot use WeldListener without ServletServices%22);%0A      %7D
 %0A   

Validate the environment