001    package org.apache.myfaces.tobago.webapp;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.myfaces.tobago.config.TobagoConfig;
023    
024    import javax.faces.FactoryFinder;
025    import javax.faces.lifecycle.LifecycleFactory;
026    import javax.servlet.ServletContextEvent;
027    import javax.servlet.ServletContextListener;
028    import javax.servlet.ServletException;
029    import javax.servlet.http.HttpServlet;
030    
031    /**
032     * Workaround: Weblogic 8.1 calls the ContextListeners after calling
033     * Servlet.init() but, JSF assume it does it before.
034     * Maybe weblogic doesn't call ContextListeners from *.jar!
035     * http://forum.java.sun.com/thread.jsp?forum=427&thread=499690
036     */
037    
038    public class WeblogicWorkaroundServlet extends HttpServlet {
039    
040      private static final Log LOG
041          = LogFactory.getLog(WeblogicWorkaroundServlet.class);
042    
043      public void init() throws ServletException {
044        if (LOG.isDebugEnabled()) {
045          LOG.debug("1st");
046        }
047        LifecycleFactory factory = (LifecycleFactory)
048            FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
049    
050        if (factory == null) { // Faces ConfigureListener is not called until now!
051          final String className = "com.sun.faces.config.ConfigureListener";
052          if (LOG.isDebugEnabled()) {
053            LOG.debug("Init of " + className + " by servlet!");
054          }
055          callInit(className);
056        }
057    
058        if (LOG.isDebugEnabled()) {
059          LOG.debug("2nd");
060        }
061        TobagoConfig tobagoConfig = (TobagoConfig)
062            getServletContext().getAttribute(TobagoConfig.TOBAGO_CONFIG);
063    
064        if (tobagoConfig == null) { // TobagoServletContextListener is not called until now!
065          final String className = TobagoServletContextListener.class.getName();
066          if (LOG.isDebugEnabled()) {
067            LOG.debug("Init of " + className + " by servlet!");
068          }
069          callInit(className);
070        }
071    
072        if (LOG.isDebugEnabled()) {
073          LOG.debug("3rd");
074        }
075      }
076    
077      private void callInit(String className) {
078        try {
079          Class aClass = Class.forName(className);
080          ServletContextListener listener = (ServletContextListener) aClass.newInstance();
081          listener.contextInitialized(new ServletContextEvent(getServletContext()));
082        } catch (ClassNotFoundException e) {
083          LOG.error("", e);
084        } catch (IllegalAccessException e) {
085          LOG.error("", e);
086        } catch (InstantiationException e) {
087          LOG.error("", e);
088        }
089      }
090    }
091