001: /*
002: * $Id: StrutsContext.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.faces.util;
023:
024: import java.util.Locale;
025:
026: import javax.faces.context.ExternalContext;
027: import javax.faces.context.FacesContext;
028: import javax.faces.event.ActionEvent;
029:
030: import javax.sql.DataSource;
031:
032: import org.apache.struts.Globals;
033: import org.apache.struts.action.ActionMapping;
034: import org.apache.struts.action.ActionMessages;
035: import org.apache.struts.action.ActionServlet;
036: import org.apache.struts.config.ModuleConfig;
037: import org.apache.struts.faces.Constants;
038: import org.apache.struts.util.MessageResources;
039:
040: /**
041: * <p>Context bean providing accessors for the Struts related request,
042: * session, and application scope objects reated to this request. Note
043: * that this bean's methods will trigger exceptions unless there is a
044: * <code>FacesContext</code> instance for this request.</p>
045: */
046:
047: public class StrutsContext {
048:
049: // ------------------------------------------------------ Instance Variables
050:
051: /**
052: * <p>The <code>FacesContext</code> for the current request.</p>
053: */
054: private FacesContext fcontext = FacesContext.getCurrentInstance();
055:
056: /**
057: * <p>The <code>ExternalContext</code> for the current request.</p>
058: */
059: private ExternalContext econtext = fcontext.getExternalContext();
060:
061: // ---------------------------------------------------------- Public Methods
062:
063: /**
064: * <p>Return the <code>ActionEvent</code> for the current request
065: * (if any).</p>
066: */
067: public ActionEvent getActionEvent() {
068:
069: return ((ActionEvent) econtext.getRequestMap().get(
070: Constants.ACTION_EVENT_KEY));
071:
072: }
073:
074: /**
075: * <p>Return the <code>ActionMapping</code> for the current
076: * request (if any).</p>
077: */
078: public ActionMapping getActionMapping() {
079:
080: return ((ActionMapping) econtext.getRequestMap().get(
081: Globals.MAPPING_KEY));
082:
083: }
084:
085: /**
086: * <p>Return the <code>ActionMessages</code> instance containing
087: * application error messages for this request (if any).</p>
088: */
089: public ActionMessages getActionMessages() {
090:
091: return ((ActionMessages) econtext.getRequestMap().get(
092: Globals.MESSAGE_KEY));
093:
094: }
095:
096: /**
097: * <p>Return the <code>ActionServlet</code> instance for this
098: * web application.</p>
099: */
100: public ActionServlet getActionServlet() {
101:
102: return ((ActionServlet) econtext.getApplicationMap().get(
103: Globals.ACTION_SERVLET_KEY));
104:
105: }
106:
107: /**
108: * <p>Return <code>true</code> if a Boolean true value has been stored
109: * in the request attribute indicating that this request has been
110: * cancelled.</p>
111: */
112: public boolean isCancelled() {
113:
114: Object value = econtext.getRequestMap().get(Globals.CANCEL_KEY);
115: if (value instanceof Boolean) {
116: return (((Boolean) value).booleanValue());
117: } else {
118: return (false);
119: }
120:
121: }
122:
123: /**
124: * <p>Return the exception that caused one of the Struts custom tags
125: * to report a JspException (if any).</p>
126: */
127: public Throwable getException() {
128:
129: return ((Throwable) econtext.getRequestMap().get(
130: Globals.EXCEPTION_KEY));
131:
132: }
133:
134: /**
135: * <p>Return the <code>ExternalContext</code> for the current request.</p>
136: */
137: public ExternalContext getExternalContext() {
138:
139: return (econtext);
140:
141: }
142:
143: /**
144: * <p>Return the <code>FacesContext</code> for the current request.</p>
145: */
146: public FacesContext getFacesContext() {
147:
148: return (fcontext);
149:
150: }
151:
152: /**
153: * <p>Return the <code>Locale</code> stored in the current user's
154: * session (if any) for Struts based localization.</p>
155: */
156: public Locale getLocale() {
157:
158: if (econtext.getSession(false) != null) {
159: return ((Locale) econtext.getSessionMap().get(
160: Globals.LOCALE_KEY));
161: } else {
162: return (null);
163: }
164:
165: }
166:
167: /**
168: * <p>Return the <code>MessageResources</code> instance for the
169: * application module that is processing this request (if any).</p>
170: */
171: public MessageResources getMessageResources() {
172:
173: return ((MessageResources) econtext.getRequestMap().get(
174: Globals.MESSAGES_KEY));
175:
176: }
177:
178: /**
179: * <p>Return the <code>ModuleConfig</code> for the application module
180: * to which this request has been assigned (if any).</p>
181: */
182: public ModuleConfig getModuleConfig() {
183:
184: return ((ModuleConfig) econtext.getRequestMap().get(
185: Globals.MODULE_KEY));
186:
187: }
188:
189: }
|