1 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 27 public class AlertUtil { 28 29 32 private AlertUtil() { 33 } 34 35 39 public static void setMessage(String msg) { 40 setMessage(msg, MgnlContext.getInstance()); 41 } 42 43 public static void setMessage(String msg, Context ctx) { 44 if (!isMessageSet(ctx)) { 45 ctx.setAttribute(Context.ATTRIBUTE_MESSAGE, msg, Context.LOCAL_SCOPE); 46 } 47 } 48 49 54 public static void setMessage(String msg, Exception e) { 55 setMessage(msg, e, MgnlContext.getInstance()); 56 } 57 58 public static void setMessage(String msg, Exception e, Context ctx) { 59 setMessage(msg + " : " + getExceptionMessage(e), ctx); 60 } 61 62 public static void setException(String msg, Exception e) { 63 setException(msg, e, MgnlContext.getInstance()); 64 } 65 66 public static void setException(String msg, Exception e, Context ctx) { 67 setMessage(msg + " : " + getExceptionMessage(e), ctx); 68 setException(e, ctx); 69 } 70 71 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 ) ctx.getAttribute(Context.ATTRIBUTE_MESSAGE, Context.LOCAL_SCOPE)); 83 } 84 85 88 public static void setException(Exception e) { 89 setException(e, MgnlContext.getInstance()); 90 } 91 92 public static void setException(Exception e, Context ctx) { 93 if (!isExceptionSet(ctx)) { 94 ctx.setAttribute(Context.ATTRIBUTE_EXCEPTION, e, Context.LOCAL_SCOPE); 95 setMessage(getExceptionMessage(e), ctx); 97 } 98 } 99 100 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 117 public static String getMessage() { 118 return getMessage(MgnlContext.getInstance()); 119 } 120 121 public static String getMessage(Context ctx) { 122 return (String ) ctx.getAttribute(Context.ATTRIBUTE_MESSAGE, Context.LOCAL_SCOPE); 123 } 124 125 130 public static String getExceptionMessage(Exception e) { 131 String 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 getException() { 144 return getException(MgnlContext.getInstance()); 145 } 146 147 public static Exception getException(Context ctx) { 148 return (Exception ) ctx.getAttribute(Context.ATTRIBUTE_EXCEPTION, Context.LOCAL_SCOPE); 149 } 150 } | Popular Tags |