Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.environment.servlet;
 
 import  javax.servlet.ServletContextEvent;
 import  javax.servlet.ServletContextListener;
 
Emulates the behavior of the naming resource binding that is typically done using configuration files in Tomcat and Jetty. This listener provides the ability to bind the BeanManager to JNDI without the need for configuration.

Author(s):
Dan Allen
Christian Sadilek <csadilek@redhat.com>
 
 public class BeanManagerResourceBindingListener implements ServletContextListener {
     private static final String RESOURCES_CONTEXT = "java:comp/env";
     private static final String BEAN_MANAGER_JNDI_NAME = "BeanManager";
     private static final String QUALIFIED_BEAN_MANAGER_JNDI_NAME =  + "/" + ;
     private static final String BEAN_MANAGER_OBJECT_FACTORY = "org.jboss.weld.resources.ManagerObjectFactory";
 
     private boolean bound = false;
 
     public void contextInitialized(ServletContextEvent sce) {
         try {
             InitialContext ctx = new InitialContext();
             boolean present = false;
             try {
                 NamingEnumeration<NameClassPairentries = ctx.list();
                 while (entries.hasMoreElements()) {
                     try {
                         NameClassPair e = entries.next();
                         if (e.getName().equals() && e.getClassName().equals(BeanManager.class)) {
                             present = true;
                             break;
                         }
                     } catch (Exception e) {
                         ..problemWhenInterating(e);
                     }
                 }
             } catch (NamingException e) {
                 ..couldNotReadContext();
                 try {
                     Context compCtx = (Contextctx.lookup("java:comp");
                     compCtx.createSubcontext("env");
                 } catch (Exception ex) {
                     ..couldntCreateContext();
                 }
             }
 
             if (!present) {
                 try {
                     // we rebind just in case it really is there and we just couldn't read it
                     ctx.rebind(,
                             new Reference(BeanManager.class.getName(), null));
                      = true;
                 } catch (NamingException e) {
                     throw ..couldNotBindBeanManagerReferenceToJNDI(e.getExplanation());
                 }
             }
         } catch (NamingException e) {
         }
     }
 
     public void contextDestroyed(ServletContextEvent sce) {
         if () {
             try {
                 InitialContext ctx = new InitialContext();
                 ctx.unbind();
             } catch (NamingException e) {
                 ..failedToUnbindBeanManagerReference();
             }
         }
    }
New to GrepCode? Check out our FAQ X