KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > util > StrutsUtil


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.util;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.apache.log4j.Logger;
26
27 import org.apache.struts.Globals;
28 import org.apache.struts.action.ActionMessage;
29 import org.apache.struts.action.ActionMessages;
30 import org.apache.struts.action.ActionError;
31 import org.apache.struts.action.ActionErrors;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionForward;
34
35 import org.apache.struts.util.MessageResources;
36 import com.methodhead.MhfException;
37
38 /**
39  * A collection of convenience methods for use with the Struts framework.
40  */

41 public class StrutsUtil {
42
43   // constructors /////////////////////////////////////////////////////////////
44

45   // constants ////////////////////////////////////////////////////////////////
46

47   // classes //////////////////////////////////////////////////////////////////
48

49   // methods //////////////////////////////////////////////////////////////////
50

51   /**
52    * Adds the <tt>msgKey</tt> message to the action messages in
53    * <tt>request</tt> for <tt>property</tt>, creating an
54    * <tt>ActionMessages</tt> object if necessary. <tt>arg0</tt>,
55    * <tt>arg1</tt>, and <tt>arg2</tt> may be null; the appropriate
56    * <tt>ActionMessage</tt> constructor will be used. If the message is not
57    * defined, a warning is logged. If <tt>property</tt> is <tt>null</tt>,
58    * <tt>ActionErrors.GLOBAL_MESSAGE</tt> is assumed.
59    */

60   public static void addMessage(
61     HttpServletRequest JavaDoc request,
62     String JavaDoc property,
63     String JavaDoc msgKey,
64     Object JavaDoc arg0,
65     Object JavaDoc arg1,
66     Object JavaDoc arg2 ) {
67
68     //
69
// force the property if it hasn't been supplied
70
//
71
if ( property == null ) {
72       property = ActionMessages.GLOBAL_MESSAGE;
73     }
74
75     //
76
// get the action messages
77
//
78
ActionMessages messages =
79       ( ActionMessages )request.getAttribute( Globals.MESSAGE_KEY );
80
81     if ( messages == null ) {
82       messages = new ActionMessages();
83       request.setAttribute( Globals.MESSAGE_KEY, messages );
84     }
85
86     //
87
// get the particular message
88
//
89
MessageResources messageResources =
90       ( MessageResources )request.getAttribute( Globals.MESSAGES_KEY );
91
92     if ( messageResources == null )
93       throw new MhfException( "Message resources is null." );
94
95     String JavaDoc message = messageResources.getMessage( msgKey );
96
97     if ( message == null )
98       logger_.warn( msgKey + " resource message is not defined." );
99
100     //
101
// instantiate the appropriate message
102
//
103
ActionMessage actionMessage = null;
104     if ( arg0 == null )
105       actionMessage = new ActionMessage( msgKey );
106     else if ( arg1 == null )
107       actionMessage = new ActionMessage( msgKey, arg0 );
108     else if ( arg2 == null )
109       actionMessage = new ActionMessage( msgKey, arg0, arg1 );
110     else
111       actionMessage = new ActionMessage( msgKey, arg0, arg1, arg2 );
112
113     //
114
// add the message
115
//
116
messages.add( property, actionMessage );
117   }
118
119   /**
120    * Adds the <tt>msgKey</tt> message to the action errors in
121    * <tt>request</tt> for <tt>property</tt>, creating an
122    * <tt>ActionErrors</tt> object if necessary. <tt>arg0</tt>,
123    * <tt>arg1</tt>, and <tt>arg2</tt> may be null; the appropriate
124    * <tt>ActionMessage</tt> constructor will be used. If the message is not
125    * defined, a warning is logged. If <tt>property</tt> is <tt>null</tt>,
126    * <tt>ActionErrors.GLOBAL_ERROR</tt> is assumed.
127    */

128   public static void addError(
129     HttpServletRequest JavaDoc request,
130     String JavaDoc property,
131     String JavaDoc msgKey,
132     Object JavaDoc arg0,
133     Object JavaDoc arg1,
134     Object JavaDoc arg2 ) {
135
136     //
137
// force the property if it hasn't been supplied
138
//
139
if ( property == null ) {
140       property = ActionErrors.GLOBAL_ERROR;
141     }
142
143     //
144
// get the action messages
145
//
146
ActionErrors messages =
147       ( ActionErrors )request.getAttribute( Globals.ERROR_KEY );
148
149     if ( messages == null ) {
150       messages = new ActionErrors();
151       request.setAttribute( Globals.ERROR_KEY, messages );
152     }
153
154     //
155
// get the particular message
156
//
157
MessageResources messageResources =
158       ( MessageResources )request.getAttribute( Globals.MESSAGES_KEY );
159
160     String JavaDoc message = messageResources.getMessage( msgKey );
161
162     if ( message == null )
163       logger_.warn( msgKey + " resource message is not defined." );
164
165     //
166
// instantiate the appropriate message
167
//
168
ActionError actionMessage = null;
169     if ( arg0 == null )
170       actionMessage = new ActionError( msgKey );
171     else if ( arg1 == null )
172       actionMessage = new ActionError( msgKey, arg0 );
173     else if ( arg2 == null )
174       actionMessage = new ActionError( msgKey, arg0, arg1 );
175     else
176       actionMessage = new ActionError( msgKey, arg0, arg1, arg2 );
177
178     //
179
// add the message
180
//
181
messages.add( property, actionMessage );
182   }
183
184   /**
185    * Adds a message as in {@link
186    * #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.
187    */

188   public static void addMessage(
189     HttpServletRequest JavaDoc request,
190     String JavaDoc msgKey,
191     Object JavaDoc arg0,
192     Object JavaDoc arg1,
193     Object JavaDoc arg2 ) {
194
195     addMessage(
196       request, ActionMessages.GLOBAL_MESSAGE, msgKey, arg0, arg1, arg2 );
197   }
198
199   /**
200    * Adds a message to the request's action messages, creating a new
201    * <tt>ActionMessages</tt> object if necessary. An warning is logged if no
202    * message exists for <tt>msgKey</tt>.
203    * @deprecated Use {@link #addMessage(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.Object,java.lang.Object,java.lang.Object) addMessage()}
204    */

205   public static void addMessage(
206     HttpServletRequest JavaDoc request,
207     MessageResources messageResources,
208     String JavaDoc property,
209     String JavaDoc msgKey,
210     Object JavaDoc arg0 ) {
211
212     ActionMessages messages =
213       ( ActionMessages )request.getAttribute( Globals.MESSAGE_KEY );
214
215     if ( messages == null )
216       messages = new ActionMessages();
217
218     String JavaDoc message = messageResources.getMessage( msgKey );
219     if ( message == null )
220       logger_.warn( msgKey + " resource message is not defined." );
221
222     messages.add(
223       property, new ActionMessage( msgKey, arg0 ) );
224
225     request.setAttribute( Globals.MESSAGE_KEY, messages );
226   }
227
228   /**
229    * Adds a message to the request's action messages, creating a new
230    * <tt>ActionMessages</tt> object if necessary. An warning is logged if no
231    * message exists for <tt>msgKey</tt>.
232    * @deprecated Use {@link #addMessage(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.Object,java.lang.Object,java.lang.Object) addMessage()}
233    */

234   public static void addMessage(
235     HttpServletRequest JavaDoc request,
236     MessageResources messageResources,
237     String JavaDoc property,
238     String JavaDoc msgKey,
239     Object JavaDoc arg0,
240     Object JavaDoc arg1 ) {
241
242     ActionMessages messages =
243       ( ActionMessages )request.getAttribute( Globals.MESSAGE_KEY );
244
245     if ( messages == null )
246       messages = new ActionMessages();
247
248     String JavaDoc message = messageResources.getMessage( msgKey );
249     if ( message == null )
250       logger_.warn( msgKey + " resource message is not defined." );
251
252     messages.add(
253       property, new ActionMessage( msgKey, arg0, arg1 ) );
254
255     request.setAttribute( Globals.MESSAGE_KEY, messages );
256   }
257
258   /**
259    * Adds a message to the request's action messages, creating a new
260    * <tt>ActionMessages</tt> object if necessary. An warning is logged if no
261    * message exists for <tt>msgKey</tt>.
262    * @deprecated Use {@link #addMessage(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.Object,java.lang.Object,java.lang.Object) addMessage()}
263    */

264   public static void addMessage(
265     HttpServletRequest JavaDoc request,
266     MessageResources messageResources,
267     String JavaDoc property,
268     String JavaDoc msgKey,
269     Object JavaDoc arg0,
270     Object JavaDoc arg1,
271     Object JavaDoc arg2 ) {
272
273     ActionMessages messages =
274       ( ActionMessages )request.getAttribute( Globals.MESSAGE_KEY );
275
276     if ( messages == null )
277       messages = new ActionMessages();
278
279     String JavaDoc message = messageResources.getMessage( msgKey );
280     if ( message == null )
281       logger_.warn( msgKey + " resource message is not defined." );
282
283     messages.add(
284       property, new ActionMessage( msgKey, arg0, arg1, arg2 ) );
285
286     request.setAttribute( Globals.MESSAGE_KEY, messages );
287   }
288
289   /**
290    * Returns the forward named <tt>name</tt> or throws an exception
291    * with a message indicating the forward could not be found.
292    */

293   public static ActionForward findForward(
294     ActionMapping mapping,
295     String JavaDoc name ) {
296
297     ActionForward forward = mapping.findForward( name );
298
299     if ( forward == null )
300       throw new MhfException( "Couldn't find \"" + name + "\" forward" );
301
302     return forward;
303   }
304
305   /**
306    * NOT UNIT TESTED Instantiates the policy specified by <tt>mapping</tt>'s
307    * parameter.
308    */

309   public static Object JavaDoc getPolicy(
310     ActionMapping mapping ) {
311
312     try {
313       return
314         Class.forName( mapping.getParameter() ).newInstance();
315     }
316     catch ( Exception JavaDoc e ) {
317       throw new MhfException(
318         "Unexpected exception while instantiating \"" +
319         mapping.getParameter() +"\":" + e.toString() );
320     }
321   }
322
323   // properties ///////////////////////////////////////////////////////////////
324

325   // attributes ///////////////////////////////////////////////////////////////
326

327   private static Logger logger_ = Logger.getLogger( "StrutsUtil" );
328 }
329
Popular Tags