View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.struts.faces.util;
18  
19  
20  import java.util.Locale;
21  
22  import javax.faces.context.ExternalContext;
23  import javax.faces.context.FacesContext;
24  import javax.faces.event.ActionEvent;
25  
26  import javax.sql.DataSource;
27  
28  import org.apache.struts.Globals;
29  import org.apache.struts.action.ActionMapping;
30  import org.apache.struts.action.ActionMessages;
31  import org.apache.struts.action.ActionServlet;
32  import org.apache.struts.config.ModuleConfig;
33  import org.apache.struts.faces.Constants;
34  import org.apache.struts.util.MessageResources;
35  
36  
37  /***
38   * <p>Context bean providing accessors for the Struts related request,
39   * session, and application scope objects reated to this request.  Note
40   * that this bean's methods will trigger exceptions unless there is a
41   * <code>FacesContext</code> instance for this request.</p>
42   */
43  
44  public class StrutsContext {
45  
46  
47      // ------------------------------------------------------ Instance Variables
48  
49  
50      /***
51       * <p>The <code>FacesContext</code> for the current request.</p>
52       */
53      private FacesContext fcontext =
54          FacesContext.getCurrentInstance();
55  
56  
57      /***
58       * <p>The <code>ExternalContext</code> for the current request.</p>
59       */
60      private ExternalContext econtext =
61          fcontext.getExternalContext();
62  
63  
64      // ---------------------------------------------------------- Public Methods
65  
66  
67      /***
68       * <p>Return the <code>ActionEvent</code> for the current request
69       * (if any).</p>
70       */
71      public ActionEvent getActionEvent() {
72  
73          return ((ActionEvent) econtext.getRequestMap().
74                  get(Constants.ACTION_EVENT_KEY));
75  
76      }
77  
78  
79      /***
80       * <p>Return the <code>ActionMapping</code> for the current
81       * request (if any).</p>
82       */
83      public ActionMapping getActionMapping() {
84  
85          return ((ActionMapping) econtext.getRequestMap().
86                  get(Globals.MAPPING_KEY));
87  
88      }
89  
90  
91      /***
92       * <p>Return the <code>ActionMessages</code> instance containing
93       * application error messages for this request (if any).</p>
94       */
95      public ActionMessages getActionMessages() {
96  
97          return ((ActionMessages) econtext.getRequestMap().
98                  get(Globals.MESSAGE_KEY));
99  
100     }
101 
102 
103     /***
104      * <p>Return the <code>ActionServlet</code> instance for this
105      * web application.</p>
106      */
107     public ActionServlet getActionServlet() {
108 
109         return ((ActionServlet) econtext.getApplicationMap().
110                 get(Globals.ACTION_SERVLET_KEY));
111 
112     }
113 
114 
115     /***
116      * <p>Return <code>true</code> if a Boolean true value has been stored
117      * in the request attribute indicating that this request has been
118      * cancelled.</p>
119      */
120     public boolean isCancelled() {
121 
122         Object value = econtext.getRequestMap().get(Globals.CANCEL_KEY);
123         if (value instanceof Boolean) {
124             return (((Boolean) value).booleanValue());
125         } else {
126             return (false);
127         }
128 
129     }
130 
131 
132 
133     /***
134      * <p>Return the exception that caused one of the Struts custom tags
135      * to report a JspException (if any).</p>
136      */
137     public Throwable getException() {
138 
139         return ((Throwable) econtext.getRequestMap().
140                 get(Globals.EXCEPTION_KEY));
141 
142     }
143 
144 
145     /***
146      * <p>Return the <code>ExternalContext</code> for the current request.</p>
147      */
148     public ExternalContext getExternalContext() {
149 
150         return (econtext);
151 
152     }
153 
154 
155     /***
156      * <p>Return the <code>FacesContext</code> for the current request.</p>
157      */
158     public FacesContext getFacesContext() {
159 
160         return (fcontext);
161 
162     }
163 
164 
165     /***
166      * <p>Return the <code>Locale</code> stored in the current user's
167      * session (if any) for Struts based localization.</p>
168      */
169     public Locale getLocale() {
170 
171         if (econtext.getSession(false) != null) {
172             return ((Locale) econtext.getSessionMap().
173                     get(Globals.LOCALE_KEY));
174         } else {
175             return (null);
176         }
177 
178     }
179 
180 
181     /***
182      * <p>Return the <code>MessageResources</code> instance for the
183      * application module that is processing this request (if any).</p>
184      */
185     public MessageResources getMessageResources() {
186 
187         return ((MessageResources) econtext.getRequestMap().
188                 get(Globals.MESSAGES_KEY));
189 
190     }
191 
192 
193     /***
194      * <p>Return the <code>ModuleConfig</code> for the application module
195      * to which this request has been assigned (if any).</p>
196      */
197     public ModuleConfig getModuleConfig() {
198 
199         return ((ModuleConfig) econtext.getRequestMap().
200                 get(Globals.MODULE_KEY));
201 
202     }
203 
204 
205 }