View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2toolkit library.
13   *
14   *  The Initial Owner of the Original Code is
15   *  Power Of Two S.R.L. (www.pow2.com)
16   *
17   *  Portions created by Power Of Two S.R.L. are
18   *  Copyright (C) Power Of Two S.R.L.
19   *  All Rights Reserved.
20   *
21   * Contributor(s):
22   */
23  
24  package com.pow2.struts.util;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.ServletRequest;
28  import javax.servlet.jsp.JspException;
29  import javax.servlet.jsp.PageContext;
30  
31  import org.apache.log4j.Category;
32  import org.apache.struts.Globals;
33  import org.apache.struts.util.RequestUtils;
34  
35  import com.pow2.util.Util;
36  
37  
38  /***
39   *  Struts' MessageUtil Class
40   *
41   * @author  Luca Fossato
42   * @created  13 August 2002
43   */
44  public class MessageUtil
45  {
46    /*** Log4J category. */
47    private static Category cat = Category.getInstance(MessageUtil.class);
48  
49  
50    /***
51     *  Gets the message string related to the <code>messageKey</code> key.
52     *  <br>
53     *  This method is a wrapper for
54     *  <code>
55     *    org.apache.struts.util.RequestUtils.message(javax.servlet.jsp.PageContext pageContext,
56     *                                                java.lang.String              bundle,
57     *                                                java.lang.String              locale,
58     *                                                java.lang.String              key)
59     *  </code>;
60     *  it uses the default MESSAGES_KEY and LOCALE_KEY values set by
61     *  <code>org.apache.struts.Action</code>.
62     *
63     * @param  pageContext the pageContext object
64     * @param  messageKey  the message key
65     * @return the message string related to the <code>messageKey</code> key,
66     *         or null if any error occurs
67     */
68    public static String getMessage(PageContext pageContext, String messageKey)
69    {
70      String val = null;
71  
72      try
73      {
74        val = RequestUtils.message(pageContext,
75                                   Globals.MESSAGES_KEY, // this one is wrong: Globals.MESSAGE_KEY,
76                                   Globals.LOCALE_KEY,
77                                   messageKey);
78      }
79      catch(JspException e)
80      {
81        cat.error("::getMessage - error: " + getExceptionMessages(pageContext, e));
82      }
83  
84      return val;
85    }
86  
87  
88    /***
89     *  Gets the message string related to the <code>messageKey</code> key.
90     *  <br>
91     *  This method is a wrapper for
92     *  <code>
93     *    org.apache.struts.util.RequestUtils.message(javax.servlet.jsp.PageContext pageContext,
94     *                                                java.lang.String              bundle,
95     *                                                java.lang.String              locale,
96     *                                                java.lang.String              key,
97     *                                                java.lang.Object[]            args)
98     *  </code>;
99     *  it uses the default MESSAGES_KEY and LOCALE_KEY values set by
100    *  <code>org.apache.struts.Action</code>.
101    *
102    * @param  pageContext the pageContext object
103    * @param  messageKey  the message key
104    * @param  args        replacement parameters for this message
105    * @return the message string related to the <code>messageKey</code> key,
106    *         or null if any error occurs
107    */
108   public static String getMessage(PageContext pageContext,
109                                   String      messageKey,
110                                   Object[]    args)
111   {
112     String val = null;
113 
114     try
115     {
116       val = RequestUtils.message(pageContext,
117                                  Globals.MESSAGES_KEY,
118                                  Globals.LOCALE_KEY,
119                                  messageKey,
120                                  args);
121     }
122     catch(JspException e)
123     {
124       cat.error("::getMessage - error: " + getExceptionMessages(pageContext, e));
125     }
126 
127     return val;
128   }
129 
130 
131   /***
132    *  Get the messages returned by the input JspException object.
133    *
134    * @param pageContext  the pageContext  object
135    * @param jspException the jspException object
136    * @return the messages returned by the input JspException object
137    */
138   private static String getExceptionMessages(PageContext  pageContext,
139                                              JspException jspException)
140   {
141     StringBuffer   msgs    = new StringBuffer();
142     ServletRequest request = null;
143 
144     msgs.append("JspException:\n").append(Util.stackTrace(jspException)).append("\n");
145 
146     // check for Struts exceptions;
147     if ((request = pageContext.getRequest()) != null)
148     {
149       Throwable t = (Throwable)request.getAttribute(Globals.EXCEPTION_KEY);
150       Throwable rootCause = null;
151 
152       if (t != null)
153       {
154         if (t instanceof ServletException)
155         {
156           msgs.append("servlet exception:\n").append(Util.stackTrace(t)).append("\n");
157 
158           if ((rootCause = ((ServletException)t).getRootCause()) != null)
159             msgs.append("root cause:\n").append(Util.stackTrace(rootCause)).append("\n");
160         }
161       }
162     }
163 
164     return msgs.toString();
165   }
166 }