001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.util;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.apache.log4j.Logger;
026:
027: import org.apache.struts.Globals;
028: import org.apache.struts.action.ActionMessage;
029: import org.apache.struts.action.ActionMessages;
030: import org.apache.struts.action.ActionError;
031: import org.apache.struts.action.ActionErrors;
032: import org.apache.struts.action.ActionMapping;
033: import org.apache.struts.action.ActionForward;
034:
035: import org.apache.struts.util.MessageResources;
036: import com.methodhead.MhfException;
037:
038: /**
039: * A collection of convenience methods for use with the Struts framework.
040: */
041: public class StrutsUtil {
042:
043: // constructors /////////////////////////////////////////////////////////////
044:
045: // constants ////////////////////////////////////////////////////////////////
046:
047: // classes //////////////////////////////////////////////////////////////////
048:
049: // methods //////////////////////////////////////////////////////////////////
050:
051: /**
052: * Adds the <tt>msgKey</tt> message to the action messages in
053: * <tt>request</tt> for <tt>property</tt>, creating an
054: * <tt>ActionMessages</tt> object if necessary. <tt>arg0</tt>,
055: * <tt>arg1</tt>, and <tt>arg2</tt> may be null; the appropriate
056: * <tt>ActionMessage</tt> constructor will be used. If the message is not
057: * defined, a warning is logged. If <tt>property</tt> is <tt>null</tt>,
058: * <tt>ActionErrors.GLOBAL_MESSAGE</tt> is assumed.
059: */
060: public static void addMessage(HttpServletRequest request,
061: String property, String msgKey, Object arg0, Object arg1,
062: Object arg2) {
063:
064: //
065: // force the property if it hasn't been supplied
066: //
067: if (property == null) {
068: property = ActionMessages.GLOBAL_MESSAGE;
069: }
070:
071: //
072: // get the action messages
073: //
074: ActionMessages messages = (ActionMessages) request
075: .getAttribute(Globals.MESSAGE_KEY);
076:
077: if (messages == null) {
078: messages = new ActionMessages();
079: request.setAttribute(Globals.MESSAGE_KEY, messages);
080: }
081:
082: //
083: // get the particular message
084: //
085: MessageResources messageResources = (MessageResources) request
086: .getAttribute(Globals.MESSAGES_KEY);
087:
088: if (messageResources == null)
089: throw new MhfException("Message resources is null.");
090:
091: String message = messageResources.getMessage(msgKey);
092:
093: if (message == null)
094: logger_.warn(msgKey + " resource message is not defined.");
095:
096: //
097: // instantiate the appropriate message
098: //
099: ActionMessage actionMessage = null;
100: if (arg0 == null)
101: actionMessage = new ActionMessage(msgKey);
102: else if (arg1 == null)
103: actionMessage = new ActionMessage(msgKey, arg0);
104: else if (arg2 == null)
105: actionMessage = new ActionMessage(msgKey, arg0, arg1);
106: else
107: actionMessage = new ActionMessage(msgKey, arg0, arg1, arg2);
108:
109: //
110: // add the message
111: //
112: messages.add(property, actionMessage);
113: }
114:
115: /**
116: * Adds the <tt>msgKey</tt> message to the action errors in
117: * <tt>request</tt> for <tt>property</tt>, creating an
118: * <tt>ActionErrors</tt> object if necessary. <tt>arg0</tt>,
119: * <tt>arg1</tt>, and <tt>arg2</tt> may be null; the appropriate
120: * <tt>ActionMessage</tt> constructor will be used. If the message is not
121: * defined, a warning is logged. If <tt>property</tt> is <tt>null</tt>,
122: * <tt>ActionErrors.GLOBAL_ERROR</tt> is assumed.
123: */
124: public static void addError(HttpServletRequest request,
125: String property, String msgKey, Object arg0, Object arg1,
126: Object arg2) {
127:
128: //
129: // force the property if it hasn't been supplied
130: //
131: if (property == null) {
132: property = ActionErrors.GLOBAL_ERROR;
133: }
134:
135: //
136: // get the action messages
137: //
138: ActionErrors messages = (ActionErrors) request
139: .getAttribute(Globals.ERROR_KEY);
140:
141: if (messages == null) {
142: messages = new ActionErrors();
143: request.setAttribute(Globals.ERROR_KEY, messages);
144: }
145:
146: //
147: // get the particular message
148: //
149: MessageResources messageResources = (MessageResources) request
150: .getAttribute(Globals.MESSAGES_KEY);
151:
152: String message = messageResources.getMessage(msgKey);
153:
154: if (message == null)
155: logger_.warn(msgKey + " resource message is not defined.");
156:
157: //
158: // instantiate the appropriate message
159: //
160: ActionError actionMessage = null;
161: if (arg0 == null)
162: actionMessage = new ActionError(msgKey);
163: else if (arg1 == null)
164: actionMessage = new ActionError(msgKey, arg0);
165: else if (arg2 == null)
166: actionMessage = new ActionError(msgKey, arg0, arg1);
167: else
168: actionMessage = new ActionError(msgKey, arg0, arg1, arg2);
169:
170: //
171: // add the message
172: //
173: messages.add(property, actionMessage);
174: }
175:
176: /**
177: * Adds a message as in {@link
178: * #addMessage(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.Object,java.lang.Object,java.lang.Object) addMessage()}, assuming the <tt>GLOBAL_MESSAGE</tt> property.
179: */
180: public static void addMessage(HttpServletRequest request,
181: String msgKey, Object arg0, Object arg1, Object arg2) {
182:
183: addMessage(request, ActionMessages.GLOBAL_MESSAGE, msgKey,
184: arg0, arg1, arg2);
185: }
186:
187: /**
188: * Adds a message to the request's action messages, creating a new
189: * <tt>ActionMessages</tt> object if necessary. An warning is logged if no
190: * message exists for <tt>msgKey</tt>.
191: * @deprecated Use {@link #addMessage(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.Object,java.lang.Object,java.lang.Object) addMessage()}
192: */
193: public static void addMessage(HttpServletRequest request,
194: MessageResources messageResources, String property,
195: String msgKey, Object arg0) {
196:
197: ActionMessages messages = (ActionMessages) request
198: .getAttribute(Globals.MESSAGE_KEY);
199:
200: if (messages == null)
201: messages = new ActionMessages();
202:
203: String message = messageResources.getMessage(msgKey);
204: if (message == null)
205: logger_.warn(msgKey + " resource message is not defined.");
206:
207: messages.add(property, new ActionMessage(msgKey, arg0));
208:
209: request.setAttribute(Globals.MESSAGE_KEY, messages);
210: }
211:
212: /**
213: * Adds a message to the request's action messages, creating a new
214: * <tt>ActionMessages</tt> object if necessary. An warning is logged if no
215: * message exists for <tt>msgKey</tt>.
216: * @deprecated Use {@link #addMessage(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.Object,java.lang.Object,java.lang.Object) addMessage()}
217: */
218: public static void addMessage(HttpServletRequest request,
219: MessageResources messageResources, String property,
220: String msgKey, Object arg0, Object arg1) {
221:
222: ActionMessages messages = (ActionMessages) request
223: .getAttribute(Globals.MESSAGE_KEY);
224:
225: if (messages == null)
226: messages = new ActionMessages();
227:
228: String message = messageResources.getMessage(msgKey);
229: if (message == null)
230: logger_.warn(msgKey + " resource message is not defined.");
231:
232: messages.add(property, new ActionMessage(msgKey, arg0, arg1));
233:
234: request.setAttribute(Globals.MESSAGE_KEY, messages);
235: }
236:
237: /**
238: * Adds a message to the request's action messages, creating a new
239: * <tt>ActionMessages</tt> object if necessary. An warning is logged if no
240: * message exists for <tt>msgKey</tt>.
241: * @deprecated Use {@link #addMessage(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.Object,java.lang.Object,java.lang.Object) addMessage()}
242: */
243: public static void addMessage(HttpServletRequest request,
244: MessageResources messageResources, String property,
245: String msgKey, Object arg0, Object arg1, Object arg2) {
246:
247: ActionMessages messages = (ActionMessages) request
248: .getAttribute(Globals.MESSAGE_KEY);
249:
250: if (messages == null)
251: messages = new ActionMessages();
252:
253: String message = messageResources.getMessage(msgKey);
254: if (message == null)
255: logger_.warn(msgKey + " resource message is not defined.");
256:
257: messages.add(property, new ActionMessage(msgKey, arg0, arg1,
258: arg2));
259:
260: request.setAttribute(Globals.MESSAGE_KEY, messages);
261: }
262:
263: /**
264: * Returns the forward named <tt>name</tt> or throws an exception
265: * with a message indicating the forward could not be found.
266: */
267: public static ActionForward findForward(ActionMapping mapping,
268: String name) {
269:
270: ActionForward forward = mapping.findForward(name);
271:
272: if (forward == null)
273: throw new MhfException("Couldn't find \"" + name
274: + "\" forward");
275:
276: return forward;
277: }
278:
279: /**
280: * NOT UNIT TESTED Instantiates the policy specified by <tt>mapping</tt>'s
281: * parameter.
282: */
283: public static Object getPolicy(ActionMapping mapping) {
284:
285: try {
286: return Class.forName(mapping.getParameter()).newInstance();
287: } catch (Exception e) {
288: throw new MhfException(
289: "Unexpected exception while instantiating \""
290: + mapping.getParameter() + "\":"
291: + e.toString());
292: }
293: }
294:
295: // properties ///////////////////////////////////////////////////////////////
296:
297: // attributes ///////////////////////////////////////////////////////////////
298:
299: private static Logger logger_ = Logger.getLogger("StrutsUtil");
300: }
|