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.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
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     *
036     * @see <a href="http://forum.java.sun.com/thread.jsp?forum=427&thread=499690">
037     *      WLS8.1 & JSF 1.0 Final: "Faces Servlet" failed to preload</a>
038     */
039    public class WeblogicWorkaroundServlet extends HttpServlet {
040    
041      private static final long serialVersionUID = -8636608446986072719L;
042    
043      private static final Logger LOG = LoggerFactory.getLogger(WeblogicWorkaroundServlet.class);
044    
045      public void init() throws ServletException {
046        if (LOG.isDebugEnabled()) {
047          LOG.debug("1st");
048        }
049        LifecycleFactory factory = (LifecycleFactory)
050            FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
051    
052        if (factory == null) { // Faces ConfigureListener is not called until now!
053          final String className = "com.sun.faces.config.ConfigureListener";
054          if (LOG.isDebugEnabled()) {
055            LOG.debug("Init of " + className + " by servlet!");
056          }
057          callInit(className);
058        }
059    
060        if (LOG.isDebugEnabled()) {
061          LOG.debug("2nd");
062        }
063        TobagoConfig tobagoConfig = (TobagoConfig)
064            getServletContext().getAttribute(TobagoConfig.TOBAGO_CONFIG);
065    
066        if (tobagoConfig == null) { // TobagoServletContextListener is not called until now!
067          final String className = TobagoServletContextListener.class.getName();
068          if (LOG.isDebugEnabled()) {
069            LOG.debug("Init of " + className + " by servlet!");
070          }
071          callInit(className);
072        }
073    
074        if (LOG.isDebugEnabled()) {
075          LOG.debug("3rd");
076        }
077      }
078    
079      private void callInit(String className) {
080        try {
081          Class aClass = Class.forName(className);
082          ServletContextListener listener = (ServletContextListener) aClass.newInstance();
083          listener.contextInitialized(new ServletContextEvent(getServletContext()));
084        } catch (ClassNotFoundException e) {
085          LOG.error("", e);
086        } catch (IllegalAccessException e) {
087          LOG.error("", e);
088        } catch (InstantiationException e) {
089          LOG.error("", e);
090        }
091      }
092    }
093