KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > ErrorGenerator


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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.outerj.daisy.frontend;
17
18 import org.apache.cocoon.generation.Generator;
19 import org.apache.cocoon.ProcessingException;
20 import org.apache.cocoon.util.location.LocationUtils;
21 import org.apache.cocoon.util.location.LocatableException;
22 import org.apache.cocoon.util.location.MultiLocatable;
23 import org.apache.cocoon.util.location.Location;
24 import org.apache.cocoon.environment.SourceResolver;
25 import org.apache.cocoon.environment.ObjectModelHelper;
26 import org.apache.cocoon.environment.Request;
27 import org.apache.cocoon.xml.XMLConsumer;
28 import org.apache.cocoon.xml.AttributesImpl;
29 import org.apache.cocoon.xml.SaxBuffer;
30 import org.apache.avalon.framework.parameters.Parameters;
31 import org.apache.avalon.framework.service.Serviceable;
32 import org.apache.avalon.framework.service.ServiceManager;
33 import org.apache.avalon.framework.service.ServiceException;
34 import org.apache.avalon.framework.context.Contextualizable;
35 import org.apache.avalon.framework.context.Context;
36 import org.apache.avalon.framework.context.ContextException;
37 import org.apache.commons.lang.exception.ExceptionUtils;
38 import org.xml.sax.SAXException JavaDoc;
39 import org.xml.sax.ContentHandler JavaDoc;
40 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyPropagatedException;
41 import org.outerj.daisy.repository.clientimpl.infrastructure.MyStackTraceElement;
42 import org.outerj.daisy.repository.Repository;
43 import org.outerj.daisy.repository.LocalizedException;
44 import org.outerj.daisy.frontend.components.siteconf.SiteConf;
45
46 import java.io.IOException JavaDoc;
47 import java.util.Map JavaDoc;
48 import java.util.Locale JavaDoc;
49 import java.util.List JavaDoc;
50
51 public class ErrorGenerator implements Generator, Serviceable, Contextualizable {
52     private Throwable JavaDoc throwable;
53     private XMLConsumer consumer;
54     private String JavaDoc mountPoint;
55     private SiteConf siteConf;
56     private String JavaDoc layoutType;
57     private String JavaDoc skin;
58     private Repository repository;
59     private ServiceManager serviceManager;
60     private Context context;
61     private Locale JavaDoc locale;
62
63     public void service(ServiceManager serviceManager) throws ServiceException {
64         this.serviceManager = serviceManager;
65     }
66
67     public void contextualize(Context context) throws ContextException {
68         this.context = context;
69     }
70
71     public void setup(SourceResolver sourceResolver, Map JavaDoc objectModel, String JavaDoc s, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
72         throwable = ObjectModelHelper.getThrowable(objectModel);
73         Request request = ObjectModelHelper.getRequest(objectModel);
74
75         mountPoint = (String JavaDoc)request.getAttribute("mountPoint");
76         siteConf = (SiteConf)request.getAttribute("siteConf");
77         skin = (String JavaDoc)request.getAttribute("skin");
78
79         try {
80             repository = WikiHelper.getRepository(request, serviceManager);
81         } catch (Exception JavaDoc e) {
82             // ignore on purpose
83
}
84
85         if ("true".equals(request.getAttribute("smallErrorPage")))
86             layoutType = "plain";
87         else
88             layoutType = null;
89
90         locale = (Locale JavaDoc)request.getAttribute("locale");
91         if (locale == null)
92             locale = Locale.US;
93
94         if (throwable == null)
95             throw new ProcessingException("No throwable found in object model.");
96     }
97
98     public void generate() throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
99         consumer.startDocument();
100         consumer.startElement("", "page", "page", new AttributesImpl());
101
102         if (siteConf != null)
103             new PageContext(mountPoint, siteConf, repository, layoutType, skin, context).toSAX(consumer);
104         else {
105             SaxBuffer skinConf = null;
106             try {
107                 skinConf = SkinConfHelper.getGlobalSkinConf(serviceManager);
108             } catch (Exception JavaDoc e) {
109                 // ignore
110
}
111             new PageContext(mountPoint, repository, layoutType, skin, skinConf, context).toSAX(consumer);
112         }
113
114         consumer.startElement("", "error", "error", new AttributesImpl());
115
116         //
117
// Cocoon stacktrace
118
//
119
generateCocoonStacktrace();
120
121         //
122
// Java stacktrace
123
//
124
consumer.startElement("", "exceptionChain", "exceptionChain", new AttributesImpl());
125
126         while (throwable != null) {
127             generateThrowable(throwable);
128             throwable = ExceptionUtils.getCause(throwable);
129         }
130
131         consumer.endElement("", "exceptionChain", "exceptionChain");
132         consumer.endElement("", "error", "error");
133         consumer.endElement("", "page", "page");
134         consumer.endDocument();
135     }
136
137     private void generateThrowable(Throwable JavaDoc throwable) throws SAXException JavaDoc {
138         if (throwable instanceof DaisyPropagatedException) {
139             DaisyPropagatedException dpe = (DaisyPropagatedException)throwable;
140             AttributesImpl attrs = new AttributesImpl();
141             attrs.addCDATAAttribute("message", String.valueOf(dpe.getUserMessage()));
142             attrs.addCDATAAttribute("class", dpe.getRemoteClassName());
143             consumer.startElement("", "throwable", "throwable", attrs);
144
145             AttributesImpl stackTraceAttrs = new AttributesImpl();
146             stackTraceAttrs.addCDATAAttribute("remote", "true");
147             consumer.startElement("", "stackTrace", "stackTrace", stackTraceAttrs);
148             MyStackTraceElement[] stacktrace = dpe.getRemoteStackTrace();
149             for (int i = 0; i < stacktrace.length; i++) {
150                 AttributesImpl steAttrs = new AttributesImpl();
151                 if (stacktrace[i].isNativeMethod())
152                     steAttrs.addCDATAAttribute("nativeMethod", String.valueOf(stacktrace[i].isNativeMethod()));
153                 steAttrs.addCDATAAttribute("className", stacktrace[i].getClassName());
154                 steAttrs.addCDATAAttribute("fileName", stacktrace[i].getFileName());
155                 steAttrs.addCDATAAttribute("lineNumber", String.valueOf(stacktrace[i].getLineNumber()));
156                 steAttrs.addCDATAAttribute("methodName", String.valueOf(stacktrace[i].getMethodName()));
157                 consumer.startElement("", "stackTraceElement", "stackTraceElement", steAttrs);
158                 consumer.endElement("", "stackTraceElement", "stackTraceElement");
159             }
160             consumer.endElement("", "stackTrace", "stackTrace");
161
162             consumer.endElement("", "throwable", "throwable");
163         } else {
164             String JavaDoc message;
165             if (throwable instanceof LocalizedException)
166                 message = ((LocalizedException)throwable).getMessage(locale);
167             else if (throwable instanceof LocatableException)
168                 message = ((LocatableException)throwable).getRawMessage();
169             else
170                 message = throwable.getMessage();
171             if (message == null)
172                 message = "";
173
174             AttributesImpl attrs = new AttributesImpl();
175             attrs.addCDATAAttribute("message", message);
176             attrs.addCDATAAttribute("class", throwable.getClass().getName());
177             consumer.startElement("", "throwable", "throwable", attrs);
178
179             consumer.startElement("", "stackTrace", "stackTrace", new AttributesImpl());
180             StackTraceElement JavaDoc[] stacktrace = throwable.getStackTrace();
181             for (int i = 0; i < stacktrace.length; i++) {
182                 AttributesImpl steAttrs = new AttributesImpl();
183                 if (stacktrace[i].isNativeMethod())
184                     steAttrs.addCDATAAttribute("nativeMethod", String.valueOf(stacktrace[i].isNativeMethod()));
185                 steAttrs.addCDATAAttribute("className", stacktrace[i].getClassName());
186                 steAttrs.addCDATAAttribute("fileName", stacktrace[i].getFileName());
187                 steAttrs.addCDATAAttribute("lineNumber", String.valueOf(stacktrace[i].getLineNumber()));
188                 steAttrs.addCDATAAttribute("methodName", String.valueOf(stacktrace[i].getMethodName()));
189                 consumer.startElement("", "stackTraceElement", "stackTraceElement", steAttrs);
190                 consumer.endElement("", "stackTraceElement", "stackTraceElement");
191             }
192             consumer.endElement("", "stackTrace", "stackTrace");
193
194             consumer.endElement("", "throwable", "throwable");
195         }
196
197     }
198
199     private void generateCocoonStacktrace() throws SAXException JavaDoc {
200         // Cocoon stacktrace: dump all located exceptions in the exception stack
201
AttributesImpl attr = new AttributesImpl();
202         consumer.startElement("", "cocoonStackTrace", "cocoonStackTrace", attr);
203         Throwable JavaDoc current = throwable;
204         while (current != null) {
205             Location loc = LocationUtils.getLocation(current);
206             if (LocationUtils.isKnown(loc)) {
207                 // One or more locations: dump it
208
consumer.startElement("", "exception", "exception", attr);
209
210                 String JavaDoc message = current instanceof LocatableException ? ((LocatableException)current).getRawMessage() : current.getMessage();
211                 consumer.startElement("", "message", "message", new AttributesImpl());
212                 consumer.characters(message.toCharArray(), 0, message.length());
213                 consumer.endElement("", "message", "message");
214
215                 attr.clear();
216                 consumer.startElement("", "locations", "locations", attr);
217                 dumpLocation(loc, attr);
218
219                 if (current instanceof MultiLocatable) {
220                     List JavaDoc locations = ((MultiLocatable)current).getLocations();
221                     for (int i = 1; i < locations.size(); i++) { // start at 1 because we already dumped the first one
222
attr.clear();
223                         dumpLocation((Location)locations.get(i), attr);
224                     }
225                 }
226                 consumer.endElement("", "locations", "locations");
227                 consumer.endElement("", "exception", "exception");
228             }
229
230
231             // Dump parent location
232
current = ExceptionUtils.getCause(current);
233         }
234
235         consumer.endElement("", "cocoonStackTrace", "cocoonStackTrace");
236     }
237
238     // This method is based on the corresponding one from Cocoon's ExceptionGenerator
239
private void dumpLocation(Location loc, AttributesImpl attr) throws SAXException JavaDoc {
240         attr.addCDATAAttribute("uri", loc.getURI());
241         attr.addCDATAAttribute("line", Integer.toString(loc.getLineNumber()));
242         attr.addCDATAAttribute("column", Integer.toString(loc.getColumnNumber()));
243         consumer.startElement("", "location", "location", attr);
244         String JavaDoc description = loc.getDescription();
245         if (description != null)
246             consumer.characters(description.toCharArray(), 0, description.length());
247         consumer.endElement("", "location", "location");
248     }
249
250     public void setConsumer(XMLConsumer xmlConsumer) {
251         this.consumer = xmlConsumer;
252     }
253
254 }
255
Popular Tags