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  package org.apache.myfaces.orchestra.lib.jsf;
20  
21  import javax.faces.FacesException;
22  import javax.faces.context.ExternalContext;
23  import javax.faces.context.FacesContext;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.myfaces.orchestra.CoreConfig;
28  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
29  import org.apache.myfaces.orchestra.frameworkAdapter.jsf.JsfFrameworkAdapter;
30  
31  /**
32   * RequestHandler that ensures that the FrameworkAdapter is initialised as a JsfFrameworkAdapter.
33   * 
34   * @since 1.1
35   */
36  class FrameworkAdapterRequestHandler implements RequestHandler
37  {
38      private final static String CONVERSATION_MESSAGER_CLASS =
39          "org.apache.myfaces.orchestra.CONVERSATION_MESSAGER"; // NON-NLS
40  
41      private final Log log = LogFactory.getLog(FrameworkAdapterRequestHandler.class);
42  
43      private JsfFrameworkAdapter adapter;
44  
45      public void init(FacesContext facesContext) throws FacesException
46      {
47          if (log.isDebugEnabled())
48          {
49              log.debug("init");
50          }
51  
52          if (FrameworkAdapter.getCurrentInstance() == null)
53          {
54              if (log.isDebugEnabled())
55              {
56                  log.debug("creating jsfFrameworkAdapter");
57              }
58              // No filter has yet initialised the adapter, so do it here.
59              String conversationMessagerClass = getConversationMessagerClass(facesContext);
60              adapter = new JsfFrameworkAdapter(conversationMessagerClass);
61              adapter.beginRequest();
62          }
63      }
64  
65      public void deinit() throws FacesException
66      {
67          if (adapter != null)
68          {
69              adapter.endRequest();
70          }
71      }
72      
73      private static String getConversationMessagerClass(FacesContext facesContext)
74      {
75          ExternalContext ec = facesContext.getExternalContext();
76  
77          String cn = ec.getInitParameter(CONVERSATION_MESSAGER_CLASS);
78          if (cn != null)
79          {
80              return cn;
81          }
82          
83          cn = ec.getInitParameter(CoreConfig.CONVERSATION_MESSAGER);
84          return cn;
85      }
86  }