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.frameworkAdapter.jsf;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.myfaces.orchestra.conversation.ConversationMessager;
24  import org.apache.myfaces.orchestra.conversation.jsf.JsfConversationMessager;
25  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
26  import org.apache.myfaces.orchestra.frameworkAdapter._FrameworkAdapterUtils;
27  import org.apache.myfaces.orchestra.lib.OrchestraException;
28  
29  import javax.faces.context.FacesContext;
30  
31  import java.io.IOException;
32  
33  /**
34   * An implementation of the FrameworkAdapter for JSF environments.
35   * <p>
36   * This class defaults to using a JsfConversationMessager instance. 
37   */
38  public class JsfFrameworkAdapter extends FrameworkAdapter
39  {
40      private final Log log = LogFactory.getLog(JsfFrameworkAdapter.class);
41  
42      public JsfFrameworkAdapter(String conversationMessagerClass)
43      {
44          ConversationMessager cm = _FrameworkAdapterUtils.createConversationMessager(conversationMessagerClass,
45                  JsfConversationMessager.class);
46          setConversationMessager(cm);
47      }
48  
49      /**
50       * This method should be invoked at the start of each JSF request cycle, before any Orchestra
51       * functionality is invoked.
52       * 
53       * @since 1.1
54       */
55      public void beginRequest()
56      {
57          log.debug("Beginning request");
58          FrameworkAdapter.setCurrentInstance(this);
59      }
60  
61      /**
62       * This method should be invoked at the end of each JSF request cycle, after the last
63       * Orchestra functionality is invoked.
64       * 
65       * @since 1.1
66       */
67      public void endRequest()
68      {
69          log.debug("Ending request");
70          FrameworkAdapter.setCurrentInstance(null);
71      }
72  
73      protected ConversationMessager createDefaultConversationMessager()
74      {
75          return new JsfConversationMessager();
76      }
77  
78      protected FacesContext getFacesContext()
79      {
80          FacesContext fc = FacesContext.getCurrentInstance();
81          if (fc == null)
82          {
83              throw new OrchestraException("Missing FacesContext");
84          }
85          return fc;
86      }
87  
88      public String getInitParameter(String key)
89      {
90          FacesContext context = getFacesContext();
91  
92          return context.getExternalContext().getInitParameter(key);
93      }
94  
95      public Object getRequestParameterAttribute(String key)
96      {
97          FacesContext context = getFacesContext();
98          return context.getExternalContext().getRequestParameterMap().get(key);
99      }
100 
101     public boolean containsRequestParameterAttribute(String key)
102     {
103         FacesContext context = getFacesContext();
104         return context.getExternalContext().getRequestParameterMap().containsKey(key);
105     }
106 
107     public Object getRequestAttribute(String key)
108     {
109         FacesContext context = getFacesContext();
110         return context.getExternalContext().getRequestMap().get(key);
111     }
112 
113     public void setRequestAttribute(String key, Object value)
114     {
115         FacesContext context = getFacesContext();
116         context.getExternalContext().getRequestMap().put(key, value);
117     }
118 
119     public boolean containsRequestAttribute(String key)
120     {
121         FacesContext context = getFacesContext();
122         return context.getExternalContext().getRequestMap().containsKey(key);
123     }
124 
125     public Object getSessionAttribute(String key)
126     {
127         FacesContext context = getFacesContext();
128         return context.getExternalContext().getSessionMap().get(key);
129     }
130 
131     public void setSessionAttribute(String key, Object value)
132     {
133         FacesContext context = getFacesContext();
134         context.getExternalContext().getSessionMap().put(key, value);
135     }
136 
137     public boolean containsSessionAttribute(String key)
138     {
139         FacesContext context = getFacesContext();
140         return context.getExternalContext().getSessionMap().containsKey(key);
141     }
142 
143     protected String getRequestContextPath()
144     {
145         FacesContext context = getFacesContext();
146         return context.getExternalContext().getRequestContextPath();
147     }
148 
149     public void redirect(String url) throws IOException
150     {
151         StringBuffer redir = new StringBuffer();
152         if (url.startsWith("/"))
153         {
154             redir.append(getRequestContextPath());
155         }
156         redir.append(url);
157 
158 
159         FacesContext context = getFacesContext();
160 
161         String actionUrl = context.getExternalContext().encodeActionURL(redir.toString());
162         context.getExternalContext().redirect(actionUrl);
163         context.responseComplete();
164     }
165 
166     public Object getBean(String name)
167     {
168         FacesContext context = getFacesContext();
169         if (context == null)
170         {
171             throw new IllegalStateException("getBean invoked before FacesServlet");
172         }
173 
174         return context.getApplication()
175                 .getVariableResolver().resolveVariable(context, name);
176     }
177 
178     public void invokeNavigation(String navigationName)
179     {
180         FacesContext context = getFacesContext();
181 
182         context.getApplication().getNavigationHandler().handleNavigation(context, null, navigationName);
183     }
184 
185     /**
186      * Return the current JSF viewId.
187      */
188     public String getCurrentViewId()
189     {
190     	return getFacesContext().getViewRoot().getViewId();
191     }
192 }