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