KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > CoreException


1 package com.sslexplorer.core;
2
3 import java.util.Locale JavaDoc;
4
5 import javax.servlet.http.HttpSession JavaDoc;
6
7 import org.apache.struts.Globals;
8 import org.apache.struts.util.MessageResources;
9
10 import com.sslexplorer.boot.CodedException;
11
12
13 /**
14  *
15  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
16  */

17 public class CoreException extends CodedException {
18
19     /**
20      * Default bundle name
21      */

22     public static final String JavaDoc DEFAULT_BUNDLE = "errors";
23     
24     // Private instance variable
25

26     private BundleActionMessage bundleActionMessage;
27     private String JavaDoc category;
28     
29     
30     /**
31      * Constructor.
32      *
33      * @param code
34      * @param category
35      */

36     public CoreException(int code, String JavaDoc category) {
37         this(code, category, (Throwable JavaDoc)null);
38     }
39     
40     /**
41      * Constructor.
42      *
43      * @param code
44      * @param category
45      * @param arg0
46      */

47     public CoreException(int code, String JavaDoc category, String JavaDoc arg0) {
48         this(code, category, DEFAULT_BUNDLE, (Throwable JavaDoc)null, arg0);
49     }
50     
51     /**
52      * Constructor.
53      *
54      * @param code code
55      * @param category category
56      * @param cause
57      */

58     public CoreException(int code, String JavaDoc category, Throwable JavaDoc cause) {
59         this(code, category, DEFAULT_BUNDLE, cause);
60     }
61     
62     /**
63      * Constructor.
64      *
65      * @param code
66      * @param category
67      * @param bundle
68      * @param cause
69      */

70     public CoreException(int code, String JavaDoc category, String JavaDoc bundle, Throwable JavaDoc cause) {
71         this(code, category, bundle, cause, cause == null ? null : cause.getMessage());
72     }
73     
74     /**
75      * Constructor.
76      *
77      * @param code
78      * @param category
79      * @param bundle
80      * @param cause
81      * @param arg0
82      */

83     public CoreException(int code, String JavaDoc category, String JavaDoc bundle, Throwable JavaDoc cause, String JavaDoc arg0) {
84         this(code, category, bundle, cause, arg0, null, null, null);
85         
86     }
87     
88     /**
89      * Constructor.
90      *
91      * @param code
92      * @param category
93      * @param bundle
94      * @param cause
95      * @param arg0
96      * @param arg1
97      * @param arg2
98      * @param arg3
99      */

100     public CoreException(int code, String JavaDoc category, String JavaDoc bundle, Throwable JavaDoc cause, String JavaDoc arg0, String JavaDoc arg1, String JavaDoc arg2, String JavaDoc arg3) {
101         super(code, "Error code " + category + "/" + code, cause);
102         this.bundleActionMessage = new BundleActionMessage(bundle, "error." + category + "." + code, arg0, arg1, arg2, arg3);
103         this.category = category;
104     }
105     
106     /**
107      * Get the bundle action message object.
108      *
109      * @return bundle action message
110      */

111     public BundleActionMessage getBundleActionMessage(){
112         return bundleActionMessage;
113     }
114     
115     /**
116      * Get the bundle name
117      *
118      * @return bundle name
119      */

120     public String JavaDoc getBundle() {
121         return bundleActionMessage.getBundle();
122     }
123
124     /**
125      * Get the localised text for this error message.
126      *
127      * @param session session
128      * @return localised text
129      */

130     public String JavaDoc getLocalizedMessage(HttpSession JavaDoc session) {
131         MessageResources mr = getMessageResources(session, bundleActionMessage.getBundle());
132         if(mr == null) {
133             return "[Could not locate message resources for bundle "+ bundleActionMessage.getBundle() +"]";
134         }
135         Locale JavaDoc l = (Locale JavaDoc)session.getAttribute(Globals.LOCALE_KEY);
136         l = l == null ? Locale.getDefault() : l;
137         String JavaDoc message = mr.getMessage(l, bundleActionMessage.getKey(), bundleActionMessage.getArg0(), bundleActionMessage.getArg1(), bundleActionMessage.getArg2(), bundleActionMessage.getArg3());
138         if(message == null) {
139             message = super.getMessage();
140         }
141         return message;
142     }
143     
144     protected MessageResources getMessageResources(HttpSession JavaDoc session, String JavaDoc bundle) {
145         return CoreUtil.getMessageResources(session, bundle);
146     }
147     
148     protected MessageResources getMessageResources(String JavaDoc bundle) {
149         return (MessageResources)CoreServlet.getServlet().getServletContext().getAttribute(
150             bundle);
151     }
152     
153     /* (non-Javadoc)
154      * @see java.lang.Throwable#getLocalizedMessage()
155      */

156     public String JavaDoc getMessage() {
157         MessageResources cmr = getMessageResources(getBundle());
158         String JavaDoc message = null;
159         if(cmr != null) {
160             message = cmr.getMessage(bundleActionMessage.getKey(), bundleActionMessage.getArg0(), bundleActionMessage.getArg1(), bundleActionMessage.getArg2(), bundleActionMessage.getArg3());
161         }
162         if(message == null) {
163             message = super.getMessage();
164         }
165         return message;
166     }
167
168     /**
169      * Get the error category
170      *
171      * @return error category
172      */

173     public String JavaDoc getCategory() {
174         return category;
175     }
176     
177 }
178
Popular Tags