KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > AlertUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.util;
14
15 import info.magnolia.context.Context;
16 import info.magnolia.context.MgnlContext;
17
18 import org.apache.commons.lang.StringUtils;
19
20
21 /**
22  * Stores a message in the request. This message can get alerted from the interface. This is used for example to alert
23  * activation errors.
24  * @author philipp
25  * @version $Revision: 6955 $ ($Author: philipp $)
26  */

27 public class AlertUtil {
28
29     /**
30      * Util: do not instantiate.
31      */

32     private AlertUtil() {
33     }
34
35     /**
36      * Store the message. Does not overwrite an already existing message.
37      * @param msg
38      */

39     public static void setMessage(String JavaDoc msg) {
40         setMessage(msg, MgnlContext.getInstance());
41     }
42
43     public static void setMessage(String JavaDoc msg, Context ctx) {
44         if (!isMessageSet(ctx)) {
45             ctx.setAttribute(Context.ATTRIBUTE_MESSAGE, msg, Context.LOCAL_SCOPE);
46         }
47     }
48
49     /**
50      * create a message containing the exception message
51      * @param msg
52      * @param e
53      */

54     public static void setMessage(String JavaDoc msg, Exception JavaDoc e) {
55         setMessage(msg, e, MgnlContext.getInstance());
56     }
57
58     public static void setMessage(String JavaDoc msg, Exception JavaDoc e, Context ctx) {
59         setMessage(msg + " : " + getExceptionMessage(e), ctx);
60     }
61
62     public static void setException(String JavaDoc msg, Exception JavaDoc e) {
63         setException(msg, e, MgnlContext.getInstance());
64     }
65
66     public static void setException(String JavaDoc msg, Exception JavaDoc e, Context ctx) {
67         setMessage(msg + " : " + getExceptionMessage(e), ctx);
68         setException(e, ctx);
69     }
70
71     /**
72      * Checks if there is a message set
73      * @param request
74      * @return true if set
75      */

76
77     public static boolean isMessageSet() {
78         return isMessageSet(MgnlContext.getInstance());
79     }
80
81     public static boolean isMessageSet(Context ctx) {
82         return StringUtils.isNotEmpty((String JavaDoc) ctx.getAttribute(Context.ATTRIBUTE_MESSAGE, Context.LOCAL_SCOPE));
83     }
84
85     /**
86      * Store the exception. Does not overwrite an already existing one.
87      */

88     public static void setException(Exception JavaDoc e) {
89         setException(e, MgnlContext.getInstance());
90     }
91
92     public static void setException(Exception JavaDoc e, Context ctx) {
93         if (!isExceptionSet(ctx)) {
94             ctx.setAttribute(Context.ATTRIBUTE_EXCEPTION, e, Context.LOCAL_SCOPE);
95             // has only an effect if not yet set
96
setMessage(getExceptionMessage(e), ctx);
97         }
98     }
99
100     /**
101      * Checks if there is a message set
102      * @return true if set
103      */

104     public static boolean isExceptionSet() {
105         return isExceptionSet(MgnlContext.getInstance());
106     }
107
108     public static boolean isExceptionSet(Context ctx) {
109         return ctx.getAttribute(Context.ATTRIBUTE_EXCEPTION, Context.LOCAL_SCOPE) != null;
110     }
111
112     /**
113      * Returns the current set message
114      * @param request
115      * @return the message
116      */

117     public static String JavaDoc getMessage() {
118         return getMessage(MgnlContext.getInstance());
119     }
120
121     public static String JavaDoc getMessage(Context ctx) {
122         return (String JavaDoc) ctx.getAttribute(Context.ATTRIBUTE_MESSAGE, Context.LOCAL_SCOPE);
123     }
124
125     /**
126      * Creates a string message out of an exception. Handles nested exceptions.
127      * @param e
128      * @return the message
129      */

130     public static String JavaDoc getExceptionMessage(Exception JavaDoc e) {
131         String JavaDoc message = e.getMessage();
132         if (StringUtils.isEmpty(message)) {
133             if (e.getCause() != null) {
134                 message = e.getCause().getMessage();
135             }
136             if (message == null) {
137                 message = StringUtils.EMPTY;
138             }
139         }
140         return message;
141     }
142
143     public static Exception JavaDoc getException() {
144         return getException(MgnlContext.getInstance());
145     }
146
147     public static Exception JavaDoc getException(Context ctx) {
148         return (Exception JavaDoc) ctx.getAttribute(Context.ATTRIBUTE_EXCEPTION, Context.LOCAL_SCOPE);
149     }
150 }
Popular Tags