View Javadoc

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  
20  package org.apache.myfaces.tobago.webapp;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.apache.myfaces.tobago.config.TobagoConfig;
25  
26  import javax.faces.FactoryFinder;
27  import javax.faces.lifecycle.LifecycleFactory;
28  import javax.servlet.ServletContextEvent;
29  import javax.servlet.ServletContextListener;
30  import javax.servlet.ServletException;
31  import javax.servlet.http.HttpServlet;
32  
33  /**
34   * Workaround: Weblogic 8.1 calls the ContextListeners after calling
35   * Servlet.init() but, JSF assume it does it before.
36   * Maybe weblogic doesn't call ContextListeners from *.jar!
37   *
38   * @see <a href="http://forum.java.sun.com/thread.jsp?forum=427&thread=499690">
39   *      WLS8.1 & JSF 1.0 Final: "Faces Servlet" failed to preload</a>
40   */
41  public class WeblogicWorkaroundServlet extends HttpServlet {
42  
43    private static final long serialVersionUID = -8636608446986072719L;
44  
45    private static final Logger LOG = LoggerFactory.getLogger(WeblogicWorkaroundServlet.class);
46  
47    public void init() throws ServletException {
48      if (LOG.isDebugEnabled()) {
49        LOG.debug("1st");
50      }
51      LifecycleFactory factory = (LifecycleFactory)
52          FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
53  
54      if (factory == null) { // Faces ConfigureListener is not called until now!
55        final String className = "com.sun.faces.config.ConfigureListener";
56        if (LOG.isDebugEnabled()) {
57          LOG.debug("Init of " + className + " by servlet!");
58        }
59        callInit(className);
60      }
61  
62      if (LOG.isDebugEnabled()) {
63        LOG.debug("2nd");
64      }
65      TobagoConfig tobagoConfig = (TobagoConfig)
66          getServletContext().getAttribute(TobagoConfig.TOBAGO_CONFIG);
67  
68      if (tobagoConfig == null) { // TobagoServletContextListener is not called until now!
69        final String className = TobagoServletContextListener.class.getName();
70        if (LOG.isDebugEnabled()) {
71          LOG.debug("Init of " + className + " by servlet!");
72        }
73        callInit(className);
74      }
75  
76      if (LOG.isDebugEnabled()) {
77        LOG.debug("3rd");
78      }
79    }
80  
81    private void callInit(String className) {
82      try {
83        Class aClass = Class.forName(className);
84        ServletContextListener listener = (ServletContextListener) aClass.newInstance();
85        listener.contextInitialized(new ServletContextEvent(getServletContext()));
86      } catch (ClassNotFoundException e) {
87        LOG.error("", e);
88      } catch (IllegalAccessException e) {
89        LOG.error("", e);
90      } catch (InstantiationException e) {
91        LOG.error("", e);
92      }
93    }
94  }
95