Coverage Report - org.apache.myfaces.webapp.StartupServletContextListener
 
Classes in this File Line Coverage Branch Coverage Complexity
StartupServletContextListener
0%
0/72
0%
0/36
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.webapp;
 20  
 
 21  
 import java.util.Enumeration;
 22  
 
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 import org.apache.myfaces.config.ManagedBeanBuilder;
 26  
 import org.apache.myfaces.util.ContainerUtils;
 27  
 import org.apache.myfaces.shared_impl.util.ClassUtils;
 28  
 
 29  
 import javax.faces.FactoryFinder;
 30  
 import javax.servlet.ServletContext;
 31  
 import javax.servlet.ServletContextEvent;
 32  
 import javax.servlet.ServletContextListener;
 33  
 
 34  
 /**
 35  
  * Initialise the MyFaces system.
 36  
  * <p>
 37  
  * This context listener is registered by the JSP TLD file for the standard
 38  
  * JSF "f" components. Normally, servlet containers will automatically load
 39  
  * and process .tld files at startup time, and therefore register and run
 40  
  * this class automatically.
 41  
  * <p>
 42  
  * Some very old servlet containers do not do this correctly, so in those
 43  
  * cases this listener may be registered manually in web.xml. Registering
 44  
  * it twice (ie in both .tld and web.xml) will result in a harmless warning
 45  
  * message being generated. Very old versions of MyFaces Core do not register
 46  
  * the listener in the .tld file, so those also need a manual entry in web.xml.
 47  
  * However all versions since at least 1.1.2 have this entry in the tld.
 48  
  * 
 49  
  * @author Manfred Geiler (latest modification by $Author: lu4242 $)
 50  
  * @version $Revision: 924403 $ $Date: 2010-03-17 13:28:20 -0500 (Wed, 17 Mar 2010) $
 51  
  */
 52  0
 public class StartupServletContextListener extends AbstractMyFacesListener
 53  
     implements ServletContextListener
 54  
 {
 55  0
     static final String FACES_INIT_DONE = StartupServletContextListener.class.getName() + ".FACES_INIT_DONE";
 56  
     /*comma delimited list of plugin classes which can be hooked into myfaces*/
 57  
     static final String FACES_INIT_PLUGINS = "org.apache.myfaces.FACES_INIT_PLUGINS";
 58  
 
 59  
     private static final byte FACES_INIT_PHASE_PREINIT = 0;
 60  
     private static final byte FACES_INIT_PHASE_POSTINIT = 1;
 61  
     private static final byte FACES_INIT_PHASE_PREDESTROY = 2;
 62  
     private static final byte FACES_INIT_PHASE_POSTDESTROY = 3;
 63  
 
 64  0
     private static final Log log = LogFactory.getLog(StartupServletContextListener.class);
 65  
 
 66  
     private FacesInitializer _facesInitializer;
 67  
     private ServletContext _servletContext;
 68  
 
 69  
 
 70  
     /**
 71  
      * the central initialisation event dispatcher which calls
 72  
      * our listeners
 73  
      * @param event
 74  
      * @param operation
 75  
      */
 76  
     private void dispatchInitializationEvent(ServletContextEvent event, int operation) {
 77  0
         String [] pluginEntries = (String []) _servletContext.getAttribute(FACES_INIT_PLUGINS);
 78  
 
 79  0
         if(pluginEntries == null) {
 80  0
             String plugins = (String) _servletContext.getInitParameter(FACES_INIT_PLUGINS);
 81  0
             log.info("Checking for plugins:"+FACES_INIT_PLUGINS);
 82  0
             if(plugins == null) return;
 83  0
             log.info("Plugins found");
 84  0
             pluginEntries = plugins.split(",");
 85  0
             _servletContext.setAttribute(FACES_INIT_PLUGINS, pluginEntries);
 86  
         }
 87  
 
 88  
         //now we process the plugins
 89  0
         for(String plugin: pluginEntries) {
 90  0
             log.info("Processing plugin:"+plugin);
 91  
             try {
 92  
                 //for now the initializers have to be stateless to
 93  
                 //so that we do not have to enforce that the initializer
 94  
                 //must be serializable
 95  0
                 Class pluginClass = ClassUtils.getContextClassLoader().loadClass(plugin);
 96  0
                 if (pluginClass == null)
 97  
                 {
 98  0
                     pluginClass = this.getClass().getClassLoader().loadClass(plugin);
 99  
                 }
 100  0
                 StartupListener initializer = (StartupListener) pluginClass.newInstance();
 101  
                 
 102  0
                 switch(operation) {
 103  
                     case FACES_INIT_PHASE_PREINIT:
 104  0
                         initializer.preInit(event);
 105  0
                         break;
 106  
                     case FACES_INIT_PHASE_POSTINIT:
 107  0
                         initializer.postInit(event);
 108  0
                         break;
 109  
                     case FACES_INIT_PHASE_PREDESTROY:
 110  0
                         initializer.preDestroy(event);
 111  0
                         break;
 112  
                     default:
 113  0
                         initializer.postDestroy(event);
 114  
                         break;
 115  
                 }
 116  
 
 117  
                
 118  0
             } catch (ClassNotFoundException e) {
 119  0
                 log.error(e);
 120  0
             } catch (IllegalAccessException e) {
 121  0
                 log.error(e);
 122  0
             } catch (InstantiationException e) {
 123  0
                 log.error(e);
 124  0
             }
 125  
 
 126  
         }
 127  0
         log.info("Processing plugins done");
 128  0
     }
 129  
 
 130  
 
 131  
     public void contextInitialized(ServletContextEvent event)
 132  
     {
 133  0
         if (_servletContext != null)
 134  
         {
 135  0
             throw new IllegalStateException("context is already initialized");
 136  
         }
 137  0
         _servletContext = event.getServletContext();
 138  0
         Boolean b = (Boolean) _servletContext.getAttribute(FACES_INIT_DONE);
 139  
 
 140  0
         if (b == null || b.booleanValue() == false)
 141  
         {
 142  0
             dispatchInitializationEvent(event, FACES_INIT_PHASE_PREINIT);
 143  0
             getFacesInitializer().initFaces(_servletContext);
 144  0
             dispatchInitializationEvent(event, FACES_INIT_PHASE_POSTINIT);
 145  0
             _servletContext.setAttribute(FACES_INIT_DONE, Boolean.TRUE);
 146  
         }
 147  
         else
 148  
         {
 149  0
             log.info("MyFaces already initialized");
 150  
         }
 151  0
     }
 152  
 
 153  
     protected FacesInitializer getFacesInitializer()
 154  
     {
 155  0
         if (_facesInitializer == null)
 156  
         {
 157  0
             if (ContainerUtils.isJsp21()) 
 158  
             {
 159  0
                 _facesInitializer = new Jsp21FacesInitializer();
 160  
             } 
 161  
             else 
 162  
             {
 163  0
                 _facesInitializer = new Jsp20FacesInitializer();
 164  
             }
 165  
         }
 166  
         
 167  0
         return _facesInitializer;
 168  
     }
 169  
 
 170  
     /**
 171  
      * configure the faces initializer
 172  
      * 
 173  
      * @param facesInitializer
 174  
      */
 175  
     public void setFacesInitializer(FacesInitializer facesInitializer)
 176  
     {
 177  0
         if (_facesInitializer != null && _facesInitializer != facesInitializer && _servletContext != null)
 178  
         {
 179  0
             _facesInitializer.destroyFaces(_servletContext);
 180  
         }
 181  0
         _facesInitializer = facesInitializer;
 182  0
         if (_servletContext != null)
 183  
         {
 184  0
             facesInitializer.initFaces(_servletContext);
 185  
         }
 186  0
     }
 187  
 
 188  
     public void contextDestroyed(ServletContextEvent event)
 189  
     {
 190  0
         doPredestroy(event);
 191  
         
 192  0
         if (_facesInitializer != null && _servletContext != null)
 193  
         {
 194  0
             _facesInitializer.destroyFaces(_servletContext);
 195  
         }
 196  0
         FactoryFinder.releaseFactories();
 197  0
         dispatchInitializationEvent(event, FACES_INIT_PHASE_POSTDESTROY);
 198  
 
 199  0
         _servletContext = null;
 200  0
     }
 201  
     
 202  
     private void doPredestroy(ServletContextEvent event) {
 203  
                 
 204  0
            ServletContext ctx = event.getServletContext();
 205  
 
 206  0
            dispatchInitializationEvent(event, FACES_INIT_PHASE_PREDESTROY);
 207  0
            Enumeration<String> attributes = ctx.getAttributeNames();
 208  
 
 209  0
            while(attributes.hasMoreElements()) 
 210  
            {
 211  0
                String name = attributes.nextElement();
 212  0
                Object value = ctx.getAttribute(name);
 213  0
                doPreDestroy(value, name, ManagedBeanBuilder.APPLICATION);
 214  0
            }
 215  0
     }
 216  
 }