KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > notification > DefaultNotifyingBuilder


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.notification;
17
18 import org.apache.avalon.framework.component.Component;
19
20 import org.apache.commons.lang.SystemUtils;
21 import org.apache.commons.lang.exception.ExceptionUtils;
22 import org.xml.sax.SAXParseException JavaDoc;
23
24 import javax.xml.transform.SourceLocator JavaDoc;
25 import javax.xml.transform.TransformerException JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * Generates an Notifying representation of widely used objects.
33  *
34  * @author <a HREF="mailto:nicolaken@apache.org">Nicola Ken Barozzi</a>
35  * @author Marc Liyanage (futureLAB AG)
36  * @version CVS $Id: DefaultNotifyingBuilder.java 280628 2005-09-13 19:14:35Z sylvain $
37  */

38 public class DefaultNotifyingBuilder implements NotifyingBuilder, Component {
39
40     /**
41      * Builds a Notifying object (SimpleNotifyingBean in this case)
42      * that tries to explain what the Object o can reveal.
43      *
44      * @param sender who sent this Object.
45      * @param o the object to use when building the SimpleNotifyingBean
46      * @return the Notifying Object that was build
47      * @see org.apache.cocoon.components.notification.Notifying
48      */

49     public Notifying build (Object JavaDoc sender, Object JavaDoc o) {
50         if (o instanceof Notifying) {
51             return (Notifying) o;
52         } else if (o instanceof Throwable JavaDoc) {
53             Throwable JavaDoc t = (Throwable JavaDoc) o;
54             SimpleNotifyingBean n = new SimpleNotifyingBean(sender);
55             n.setType(Notifying.ERROR_NOTIFICATION);
56             n.setTitle("An Error Occurred");
57
58             if (t != null) {
59                 Throwable JavaDoc rootCause = getRootCause(t);
60
61                 n.setSource(t.getClass().getName());
62
63                 // NullPointerException usually does not have a message
64
if (rootCause.getMessage() != null) {
65                     n.setMessage(rootCause.getMessage());
66                 } else {
67                     n.setMessage(t.getMessage());
68                 }
69
70                 n.setDescription(t.toString());
71                 n.addExtraDescription(Notifying.EXTRA_CAUSE, rootCause.toString());
72
73                 if (rootCause instanceof SAXParseException JavaDoc) {
74                     SAXParseException JavaDoc saxParseException = (SAXParseException JavaDoc) rootCause;
75
76                     n.addExtraDescription(Notifying.EXTRA_LOCATION,
77                                           String.valueOf(saxParseException.getSystemId()));
78                     n.addExtraDescription(Notifying.EXTRA_LINE,
79                                           String.valueOf(saxParseException.getLineNumber()));
80                     n.addExtraDescription(Notifying.EXTRA_COLUMN,
81                                           String.valueOf(saxParseException.getColumnNumber()));
82                 } else if (rootCause instanceof TransformerException JavaDoc) {
83                     TransformerException JavaDoc transformerException = (TransformerException JavaDoc) rootCause;
84                     SourceLocator JavaDoc sourceLocator = transformerException.getLocator();
85
86                     if (null != sourceLocator) {
87                         n.addExtraDescription(Notifying.EXTRA_LOCATION,
88                                               String.valueOf(sourceLocator.getSystemId()));
89                         n.addExtraDescription(Notifying.EXTRA_LINE,
90                                               String.valueOf(sourceLocator.getLineNumber()));
91                         n.addExtraDescription(Notifying.EXTRA_COLUMN,
92                                               String.valueOf(sourceLocator.getColumnNumber()));
93                     }
94                 }
95
96                 // Add root cause exception stacktrace
97
StringWriter JavaDoc sw = new StringWriter JavaDoc();
98                 rootCause.printStackTrace(new PrintWriter JavaDoc(sw));
99                 n.addExtraDescription(Notifying.EXTRA_STACKTRACE, sw.toString());
100
101                 // Add full exception chain
102
sw = new StringWriter JavaDoc();
103                 appendTraceChain(sw, t);
104                 n.addExtraDescription(Notifying.EXTRA_FULLTRACE, sw.toString());
105             }
106
107             return n;
108         } else {
109             SimpleNotifyingBean n = new SimpleNotifyingBean(sender);
110             n.setType(Notifying.UNKNOWN_NOTIFICATION);
111             n.setTitle("Object Notification");
112             n.setMessage(String.valueOf(o));
113             n.setDescription("No details available.");
114             return n;
115         }
116     }
117
118     /**
119      * Builds a Notifying object (SimpleNotifyingBean in this case)
120      * that explains a notification.
121      *
122      * @param sender who sent this Object.
123      * @param o the object to use when building the SimpleNotifyingBean
124      * @param type see the Notifying apidocs
125      * @param title see the Notifying apidocs
126      * @param source see the Notifying apidocs
127      * @param message see the Notifying apidocs
128      * @param description see the Notifying apidocs
129      * @param extra see the Notifying apidocs
130      * @return the Notifying Object that was build
131      * @see org.apache.cocoon.components.notification.Notifying
132      */

133     public Notifying build(Object JavaDoc sender, Object JavaDoc o, String JavaDoc type, String JavaDoc title,
134                            String JavaDoc source, String JavaDoc message, String JavaDoc description, Map JavaDoc extra) {
135         // NKB Cast here is secure, the method is of this class
136
SimpleNotifyingBean n = (SimpleNotifyingBean) build (sender, o);
137
138         if (type != null)
139             n.setType(type);
140         if (title != null)
141             n.setTitle(title);
142         if (source != null)
143             n.setSource(source);
144         if (message != null)
145             n.setMessage(message);
146         if (description != null)
147             n.setDescription(description);
148         if (extra != null)
149             n.addExtraDescriptions(extra);
150
151         return n;
152     }
153
154
155     /**
156      * Print stacktrace of the Throwable and stacktraces of its all nested causes into a Writer.
157      */

158     private static void appendTraceChain(Writer JavaDoc out, Throwable JavaDoc t) {
159         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(out);
160         if (SystemUtils.isJavaVersionAtLeast(140)) {
161             t.printStackTrace(pw);
162         } else {
163             for (Throwable JavaDoc cause = t; cause != null; cause = ExceptionUtils.getCause(cause)) {
164                 if (cause != t) {
165                     pw.println();
166                 }
167                 cause.printStackTrace(pw);
168             }
169         }
170     }
171
172     /**
173      * Get root cause Throwable.
174      */

175     public static Throwable JavaDoc getRootCause (Throwable JavaDoc t) {
176       Throwable JavaDoc rootCause = ExceptionUtils.getRootCause(t);
177       return rootCause != null ? rootCause : t;
178     }
179 }
180
Popular Tags