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.component.UIViewRoot;
30  import javax.faces.context.FacesContext;
31  
32  import java.io.IOException;
33  
34  /**
35   * An implementation of the FrameworkAdapter for JSF environments.
36   * <p>
37   * This class defaults to using a JsfConversationMessager instance. 
38   */
39  public class JsfFrameworkAdapter extends FrameworkAdapter
40  {
41      private final Log log = LogFactory.getLog(JsfFrameworkAdapter.class);
42  
43      public JsfFrameworkAdapter(String conversationMessagerClass)
44      {
45          ConversationMessager cm = _FrameworkAdapterUtils.createConversationMessager(conversationMessagerClass,
46                  JsfConversationMessager.class);
47          setConversationMessager(cm);
48      }
49  
50      /**
51       * This method should be invoked at the start of each JSF request cycle, before any Orchestra
52       * functionality is invoked.
53       * 
54       * @since 1.1
55       */
56      public void beginRequest()
57      {
58          log.debug("Beginning request");
59          FrameworkAdapter.setCurrentInstance(this);
60      }
61  
62      /**
63       * This method should be invoked at the end of each JSF request cycle, after the last
64       * Orchestra functionality is invoked.
65       * 
66       * @since 1.1
67       */
68      public void endRequest()
69      {
70          log.debug("Ending request");
71          FrameworkAdapter.setCurrentInstance(null);
72      }
73  
74      protected ConversationMessager createDefaultConversationMessager()
75      {
76          return new JsfConversationMessager();
77      }
78  
79      protected FacesContext getFacesContext()
80      {
81          FacesContext fc = FacesContext.getCurrentInstance();
82          if (fc == null)
83          {
84              throw new OrchestraException("Missing FacesContext");
85          }
86          return fc;
87      }
88  
89      public String getInitParameter(String key)
90      {
91          FacesContext context = getFacesContext();
92  
93          return context.getExternalContext().getInitParameter(key);
94      }
95  
96      public Object getRequestParameterAttribute(String key)
97      {
98          FacesContext context = getFacesContext();
99          return context.getExternalContext().getRequestParameterMap().get(key);
100     }
101 
102     public boolean containsRequestParameterAttribute(String key)
103     {
104         FacesContext context = getFacesContext();
105         return context.getExternalContext().getRequestParameterMap().containsKey(key);
106     }
107 
108     public Object getRequestAttribute(String key)
109     {
110         FacesContext context = getFacesContext();
111         return context.getExternalContext().getRequestMap().get(key);
112     }
113 
114     public void setRequestAttribute(String key, Object value)
115     {
116         FacesContext context = getFacesContext();
117         context.getExternalContext().getRequestMap().put(key, value);
118     }
119 
120     public boolean containsRequestAttribute(String key)
121     {
122         FacesContext context = getFacesContext();
123         return context.getExternalContext().getRequestMap().containsKey(key);
124     }
125 
126     public Object getSessionAttribute(String key)
127     {
128         FacesContext context = getFacesContext();
129         return context.getExternalContext().getSessionMap().get(key);
130     }
131 
132     public void setSessionAttribute(String key, Object value)
133     {
134         FacesContext context = getFacesContext();
135         context.getExternalContext().getSessionMap().put(key, value);
136     }
137 
138     public boolean containsSessionAttribute(String key)
139     {
140         FacesContext context = getFacesContext();
141         return context.getExternalContext().getSessionMap().containsKey(key);
142     }
143 
144     protected String getRequestContextPath()
145     {
146         FacesContext context = getFacesContext();
147         return context.getExternalContext().getRequestContextPath();
148     }
149 
150     public void redirect(String url) throws IOException
151     {
152         StringBuffer redir = new StringBuffer();
153         if (url.startsWith("/"))
154         {
155             redir.append(getRequestContextPath());
156         }
157         redir.append(url);
158 
159 
160         FacesContext context = getFacesContext();
161 
162         String actionUrl = context.getExternalContext().encodeActionURL(redir.toString());
163         context.getExternalContext().redirect(actionUrl);
164         context.responseComplete();
165     }
166 
167     public Object getBean(String name)
168     {
169         FacesContext context = getFacesContext();
170         if (context == null)
171         {
172             throw new IllegalStateException("getBean invoked before FacesServlet");
173         }
174 
175         try
176         {
177             return context.getApplication()
178                 .getVariableResolver().resolveVariable(context, name);
179         }
180         catch(RuntimeException e)
181         {
182             // Some libraries don't always report the name of the bean when a lookup
183             // failure occurs. In particular, Spring does not do this when the associated
184             // scope does not exist.
185             log.error("Failed to resolve variable [" + name + "]", e);
186             throw e;
187         }
188     }
189 
190     public void invokeNavigation(String navigationName)
191     {
192         FacesContext context = getFacesContext();
193 
194         context.getApplication().getNavigationHandler().handleNavigation(context, null, navigationName);
195     }
196 
197     /**
198      * Return the current JSF viewId.
199      * <p>
200      * Null is returned if there is not yet a UIViewRoot set up for the current request.
201      */
202     public String getCurrentViewId()
203     {
204         UIViewRoot vr = getFacesContext().getViewRoot();
205         if (vr == null)
206         {
207             return null;
208         }
209         
210         return vr.getViewId();
211     }
212 }