View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.util;
35  
36  import info.magnolia.context.Context;
37  import info.magnolia.context.MgnlContext;
38  
39  import org.apache.commons.lang.StringUtils;
40  
41  
42  /**
43   * Stores a message in the request. This message can get alerted from the interface. This is used for example to alert
44   * activation errors.
45   * @author philipp
46   * @version $Revision: 32667 $ ($Author: gjoseph $)
47   */
48  public class AlertUtil {
49  
50      /**
51       * Util: do not instantiate.
52       */
53      private AlertUtil() {
54      }
55  
56      /**
57       * Store the message. Does not overwrite an already existing message.
58       * @param msg
59       */
60      public static void setMessage(String msg) {
61          setMessage(msg, MgnlContext.getInstance());
62      }
63  
64      public static void setMessage(String msg, Context ctx) {
65          if (!isMessageSet(ctx)) {
66              ctx.setAttribute(Context.ATTRIBUTE_MESSAGE, msg, Context.LOCAL_SCOPE);
67          }
68      }
69  
70      /**
71       * create a message containing the exception message
72       * @param msg
73       * @param e
74       */
75      public static void setMessage(String msg, Exception e) {
76          setMessage(msg, e, MgnlContext.getInstance());
77      }
78  
79      public static void setMessage(String msg, Exception e, Context ctx) {
80          setMessage(msg + " : " + getExceptionMessage(e), ctx);
81      }
82  
83      public static void setException(String msg, Exception e) {
84          setException(msg, e, MgnlContext.getInstance());
85      }
86  
87      public static void setException(String msg, Exception e, Context ctx) {
88          setMessage(msg + " : " + getExceptionMessage(e), ctx);
89          setException(e, ctx);
90      }
91  
92      /**
93       * Checks if there is a message set
94       * @param request
95       * @return true if set
96       */
97  
98      public static boolean isMessageSet() {
99          return isMessageSet(MgnlContext.getInstance());
100     }
101 
102     public static boolean isMessageSet(Context ctx) {
103         return StringUtils.isNotEmpty((String) ctx.getAttribute(Context.ATTRIBUTE_MESSAGE, Context.LOCAL_SCOPE));
104     }
105 
106     /**
107      * Store the exception. Does not overwrite an already existing one.
108      */
109     public static void setException(Exception e) {
110         setException(e, MgnlContext.getInstance());
111     }
112 
113     public static void setException(Exception e, Context ctx) {
114         if (!isExceptionSet(ctx)) {
115             ctx.setAttribute(Context.ATTRIBUTE_EXCEPTION, e, Context.LOCAL_SCOPE);
116             // has only an effect if not yet set
117             setMessage(getExceptionMessage(e), ctx);
118         }
119     }
120 
121     /**
122      * Checks if there is a message set
123      * @return true if set
124      */
125     public static boolean isExceptionSet() {
126         return isExceptionSet(MgnlContext.getInstance());
127     }
128 
129     public static boolean isExceptionSet(Context ctx) {
130         return ctx.getAttribute(Context.ATTRIBUTE_EXCEPTION, Context.LOCAL_SCOPE) != null;
131     }
132 
133     /**
134      * Returns the current set message
135      * @param request
136      * @return the message
137      */
138     public static String getMessage() {
139         return getMessage(MgnlContext.getInstance());
140     }
141 
142     public static String getMessage(Context ctx) {
143         return (String) ctx.getAttribute(Context.ATTRIBUTE_MESSAGE, Context.LOCAL_SCOPE);
144     }
145 
146     /**
147      * Creates a string message out of an exception. Handles nested exceptions.
148      * @param e
149      * @return the message
150      */
151     public static String getExceptionMessage(Exception e) {
152         String message = e.getMessage();
153         if (StringUtils.isEmpty(message)) {
154             if (e.getCause() != null) {
155                 message = e.getCause().getMessage();
156             }
157             if (message == null) {
158                 message = StringUtils.EMPTY;
159             }
160         }
161         return message;
162     }
163 
164     public static Exception getException() {
165         return getException(MgnlContext.getInstance());
166     }
167 
168     public static Exception getException(Context ctx) {
169         return (Exception) ctx.getAttribute(Context.ATTRIBUTE_EXCEPTION, Context.LOCAL_SCOPE);
170     }
171 }