KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > ErrorHandling


1 package org.apache.beehive.netui.tags;
2
3 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
4
5 import org.apache.beehive.netui.util.Bundle;
6 import org.apache.beehive.netui.util.logging.Logger;
7 import org.apache.struts.util.ResponseUtils;
8
9 import javax.servlet.jsp.JspException JavaDoc;
10 import javax.servlet.jsp.PageContext JavaDoc;
11 import javax.servlet.jsp.tagext.JspTag JavaDoc;
12 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.Writer JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 public class ErrorHandling
19 {
20     /**
21      * static flag indicating if we are reporting errors in-page or throwing JspExceptions.
22      */

23     private static boolean reportErrorInPage = true;
24     private static final Logger logger = Logger.getInstance(ErrorHandling.class);
25
26     private List JavaDoc _errors;
27
28     /**
29      * This will report an error from a tag. The error will
30      * contain a message. If error reporting is turned off,
31      * the message will be returned and the caller should throw
32      * a JspException to report the error.
33      * @param message - the message to register with the error
34      * @throws javax.servlet.jsp.JspException - if in-page error reporting is turned off this method will always
35      * throw a JspException.
36      */

37     public void registerTagError(String JavaDoc message, String JavaDoc tagName, JspTag JavaDoc tag, Throwable JavaDoc e)
38             throws JspException JavaDoc
39     {
40         assert (message != null) : "parameter 'message' must not be null.";
41
42         // add the error to the list of errors
43
if (_errors == null)
44             _errors = new ArrayList JavaDoc();
45
46         TagErrorInfo tei = new TagErrorInfo();
47
48         tei.tagType = tagName;
49         tei.message = message;
50         _errors.add(tei);
51
52         IErrorReporter er = getErrorReporter(tag);
53         if (er == null) {
54             tei.errorNo = -1;
55             if (!reportErrorInPage) {
56                 String JavaDoc s = Bundle.getString("Tags_NoInPageErrorReporting", new Object JavaDoc[]{message});
57                 if (e == null)
58                     logger.error(s);
59                 else
60                     logger.error(s, e);
61                 //localRelease();
62
throw new JspException JavaDoc(message);
63             }
64             return;
65         }
66
67         // add the error to the ErrorReporter tag
68
er.addError(tei);
69         assert (tei.errorNo > 0);
70         if (!reportErrorInPage) {
71             String JavaDoc s = Bundle.getString("Tags_NoInPageErrorReporting", new Object JavaDoc[]{message});
72             if (e == null)
73                 logger.error(s);
74             else
75                 logger.error(s, e);
76             //localRelease();
77
throw new JspException JavaDoc(s);
78         }
79         return;
80     }
81
82     /**
83      * This method will add an error to the errors begin tracked by the tag. After the first time this method
84      * is called, <code>hasErrors</code> will return true.
85      * @param error The <code>EvalErrorInfo</code> describing the error.
86      */

87     public void registerTagError(AbstractPageError error, JspTag JavaDoc tag)
88             throws JspException JavaDoc
89     {
90         assert (error != null);
91
92         // add the error to the list of errors
93
if (_errors == null)
94             _errors = new ArrayList JavaDoc();
95
96         _errors.add(error);
97
98         IErrorReporter er = getErrorReporter(tag);
99         if (er == null) {
100             error.errorNo = -1;
101             return;
102         }
103
104         // add the error to the ErrorReporter tag
105
er.addError(error);
106         assert (error.errorNo > 0);
107     }
108
109     /**
110      * This method will return <code>true</code> if there have been any errors registered on this
111      * tag. Otherwise it returns <code>false</code>
112      * @return <code>true</code> if errors have been reported on this tag.
113      * @see #registerTagError
114      */

115     public boolean hasErrors()
116     {
117         return (_errors != null);
118     }
119
120     /**
121      * This method will write out the <code>String</code> returned by <code>getErrorsReport</code> to the
122      * response output stream.
123      * @throws JspException if <code>write</code> throws an exception.
124      * @see #getErrorsReport
125      */

126     public void reportErrors(Writer JavaDoc writer, String JavaDoc tagName)
127             throws JspException JavaDoc
128     {
129         try {
130             writer.write(getErrorsReport(tagName));
131         }
132         catch (IOException JavaDoc e) {
133             throw new JspException JavaDoc(e.getMessage(), e);
134         }
135     }
136
137     public String JavaDoc getInlineError(String JavaDoc tagName)
138     {
139         if (isInlineErrors()) {
140             AbstractPageError info = (AbstractPageError) _errors.get(0);
141             return getInlineError(info, tagName);
142         }
143         return null;
144     }
145
146
147     /**
148      * This method will return a <code>String<code> that represents all of the errors that were
149      * registered for the tag. This method assumes that there are errors in the tag and asserts
150      * this is true. Code will typically call <code>hasErrors</code> before calling this method.
151      * @return A <code>String</code> that contains all of the errors registered on this tag.
152      * @see #registerTagError
153      */

154     public String JavaDoc getErrorsReport(String JavaDoc tagName)
155     {
156         assert _errors != null;
157         assert _errors.size() > 0;
158
159         int cnt = _errors.size();
160
161         InternalStringBuilder sb = new InternalStringBuilder(128);
162
163         // check the first error to see if we are reporting errors at the end
164
AbstractPageError info = (AbstractPageError) _errors.get(0);
165         if (isInlineErrors()) {
166             String JavaDoc s = getInlineError(info, tagName);
167             return s;
168         }
169
170         // create the errors
171
String JavaDoc s;
172         s = Bundle.getString("Tag_Header",
173                 new Object JavaDoc[]{tagName, Integer.toString(cnt)});
174         sb.append(s);
175
176         Object JavaDoc[] args = new Object JavaDoc[4];
177         for (int i = 0; i < cnt; i++) {
178             Object JavaDoc o = _errors.get(i);
179             if (o instanceof EvalErrorInfo) {
180                 EvalErrorInfo e = (EvalErrorInfo) o;
181                 assert info != null;
182
183                 args[0] = Bundle.getString("Expression_Error");
184                 args[1] = e.attr;
185                 args[2] = e.expression;
186                 args[3] = e.evalExcp.getMessage();
187                 s = Bundle.getString("Expression_Error_Line", args);
188             }
189             else if (o instanceof TagErrorInfo) {
190                 TagErrorInfo e = (TagErrorInfo) o;
191                 assert info != null;
192
193                 args[0] = Bundle.getString("Tag_Error");
194                 args[1] = e.message;
195                 s = Bundle.getString("Tag_Error_Line", args);
196             }
197             sb.append(s);
198         }
199
200         s = Bundle.getString("Tag_Footer");
201         sb.append(s);
202         return sb.toString();
203     }
204
205     /**
206      * This method get the current errors and write the formated output
207      * @param sb
208      */

209     public static void reportCollectedErrors(InternalStringBuilder sb, JspTag JavaDoc tag)
210     {
211         IErrorReporter er = getErrorReporter(tag);
212         if (er == null)
213             return;
214
215         assert (sb != null);
216         ArrayList JavaDoc errors = er.returnErrors();
217         if (errors == null || errors.size() == 0)
218             return;
219
220         assert(errors.size() > 0);
221
222         String JavaDoc s;
223         // write the error header
224
s = Bundle.getString("Footer_Error_Header");
225         sb.append(s);
226
227         int cnt = errors.size();
228         Object JavaDoc[] args = new Object JavaDoc[5];
229         for (int i = 0; i < cnt; i++) {
230             Object JavaDoc o = errors.get(i);
231             assert (o != null);
232             if (o instanceof EvalErrorInfo) {
233                 EvalErrorInfo err = (EvalErrorInfo) o;
234                 args[0] = Integer.toString(err.errorNo);
235                 args[1] = err.tagType;
236                 args[2] = err.attr;
237                 args[3] = err.expression;
238                 args[4] = err.evalExcp.getMessage();
239                 s = Bundle.getString("Footer_Error_Expr_Body", args);
240                 sb.append(s);
241             }
242             else if (o instanceof TagErrorInfo) {
243                 TagErrorInfo tei = (TagErrorInfo) o;
244                 args[0] = Integer.toString(tei.errorNo);
245                 args[1] = tei.tagType;
246                 args[2] = tei.message;
247                 s = Bundle.getString("Footer_Error_Tag_Body", args);
248                 sb.append(s);
249             }
250         }
251
252         // write the error footer
253
s = Bundle.getString("Footer_Error_Footer");
254         sb.append(s);
255     }
256
257     /**
258      * This method get the current errors and write the formated output
259      * @param pc
260      */

261     public static void reportCollectedErrors(PageContext JavaDoc pc, JspTag JavaDoc tag)
262     {
263         IErrorReporter er = getErrorReporter(tag);
264         if (er == null)
265             return;
266
267         assert (pc != null);
268         ArrayList JavaDoc errors = er.returnErrors();
269         if (errors == null || errors.size() == 0)
270             return;
271
272         assert(errors.size() > 0);
273
274         String JavaDoc s;
275         // write the error header
276
s = Bundle.getString("Footer_Error_Header");
277         write(pc, s);
278
279         int cnt = errors.size();
280         Object JavaDoc[] args = new Object JavaDoc[5];
281         for (int i = 0; i < cnt; i++) {
282             Object JavaDoc o = errors.get(i);
283             assert (o != null);
284             if (o instanceof EvalErrorInfo) {
285                 EvalErrorInfo err = (EvalErrorInfo) o;
286                 args[0] = Integer.toString(err.errorNo);
287                 args[1] = err.tagType;
288                 args[2] = err.attr;
289                 args[3] = err.expression;
290                 args[4] = err.evalExcp.getMessage();
291                 s = Bundle.getString("Footer_Error_Expr_Body", args);
292                 write(pc, s);
293             }
294             else if (o instanceof TagErrorInfo) {
295                 TagErrorInfo tei = (TagErrorInfo) o;
296                 args[0] = Integer.toString(tei.errorNo);
297                 args[1] = tei.tagType;
298                 args[2] = tei.message;
299                 s = Bundle.getString("Footer_Error_Tag_Body", args);
300                 write(pc, s);
301             }
302         }
303
304         // write the error footer
305
s = Bundle.getString("Footer_Error_Footer");
306         write(pc, s);
307     }
308
309     private boolean isInlineErrors()
310     {
311         AbstractPageError info = (AbstractPageError) _errors.get(0);
312         return (info.errorNo > 0);
313     }
314
315     private String JavaDoc getInlineError(AbstractPageError info, String JavaDoc tagName)
316     {
317         String JavaDoc s;
318         if (info instanceof EvalErrorInfo) {
319             s = Bundle.getString("Expression_Error");
320             s = Bundle.getString("Inline_error",
321                     new Object JavaDoc[]{
322                         s,
323                         Integer.toString(info.errorNo),
324                         tagName,
325                     });
326         }
327         else if (info instanceof TagErrorInfo) {
328             s = Bundle.getString("Tag_Error");
329             s = Bundle.getString("Inline_error",
330                     new Object JavaDoc[]{
331                         s,
332                         Integer.toString(info.errorNo),
333                         tagName,
334                     });
335         }
336         else {
337             s = null;
338             assert true : "Unhandled type";
339         }
340         return s;
341     }
342
343     /**
344      * This method will return the first <code>ErrorReporter</code> in the parental chain of the
345      * tag. Searching starts with this tag.
346      * @return an <code>ErrorReporter</code> if one is found in the parental chain, otherwise null.
347      */

348     private static IErrorReporter getErrorReporter(JspTag JavaDoc tag)
349     {
350         if (tag instanceof IErrorReporter && ((IErrorReporter) tag).isReporting())
351             return (IErrorReporter) tag;
352
353         // check to see if this tag has is an ErrorReporter or has an ErrorReporter as a parent
354
IErrorReporter er = (IErrorReporter) SimpleTagSupport.findAncestorWithClass(tag, IErrorReporter.class);
355         while (er != null) {
356             if (er.isReporting())
357                 return er;
358             er = (IErrorReporter) SimpleTagSupport.findAncestorWithClass((JspTag JavaDoc) er, IErrorReporter.class);
359         }
360         return null;
361     }
362
363     private static final void write(PageContext JavaDoc pc, String JavaDoc string)
364     {
365         try {
366             ResponseUtils.write(pc, string);
367         }
368         catch (JspException JavaDoc e) {
369             logger.error(Bundle.getString("Tags_WriteException"), e);
370         }
371     }
372 }
373
Popular Tags