1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.cocoon;
18
19 import java.util.List;
20
21 import org.apache.cocoon.util.location.LocatedException;
22 import org.apache.cocoon.util.location.LocatedRuntimeException;
23 import org.apache.cocoon.util.location.Location;
24 import org.apache.cocoon.util.location.MultiLocatable;
25
26 /**
27 * This Exception is thrown every time there is a problem in processing
28 * a request.
29 *
30 * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
31 * (Apache Software Foundation)
32 * @version CVS $Id: ProcessingException.java 433543 2006-08-22 06:22:54Z crossley $
33 */
34 public class ProcessingException extends LocatedException {
35
36 /**
37 * Construct a new <code>ProcessingException</code> instance.
38 */
39 public ProcessingException(String message) {
40 super(message);
41 }
42
43 /**
44 * Creates a new <code>ProcessingException</code> instance.
45 *
46 * @param ex an <code>Exception</code> value
47 */
48 public ProcessingException(Exception ex) {
49 super(ex.getMessage(), ex);
50 }
51
52 /**
53 * Construct a new <code>ProcessingException</code> that references
54 * a parent Exception.
55 */
56 public ProcessingException(String message, Throwable t) {
57 super(message, t);
58 }
59
60 /**
61 * Construct a new <code>ProcessingException</code> that has an associated location.
62 */
63 public ProcessingException(String message, Location location) {
64 super(message, location);
65 }
66
67 /**
68 * Construct a new <code>ProcessingException</code> that has a parent exception
69 * and an associated location.
70 * <p>
71 * This constructor is protected to enforce the use of {@link #throwLocated(String, Throwable, Location)}
72 * which limits exception nesting as far as possible.
73 */
74 protected ProcessingException(String message, Throwable t, Location location) {
75 super(message, t, location);
76 }
77
78 /**
79 * Throw a located exception given an existing exception and the location where
80 * this exception was catched.
81 * <p>
82 * If the exception is already a <code>ProcessingException</code> or a {@link LocatedRuntimeException},
83 * the location is added to the original exception's location chain and the original exception
84 * is rethrown (<code>description</code> is ignored) to limit exception nesting. Otherwise, a new
85 * <code>ProcessingException</code> is thrown, wrapping the original exception.
86 * <p>
87 * Note: this method returns an exception as a convenience if you want to keep the <code>throw</code>
88 * semantics in the caller code, i.e. write<br>
89 * <code> throw ProcessingException.throwLocated(...);</code><br>
90 * instead of<br>
91 * <code> ProcessingException.throwLocated(...);</code><br>
92 * <code> return;</code>
93 *
94 * @param message a message (can be <code>null</code>)
95 * @param thr the original exception (can be <code>null</code>)
96 * @param location the location (can be <code>null</code>)
97 * @return a (fake) located exception
98 * @throws ProcessingException or <code>LocatedRuntimeException</code>
99 */
100 public static ProcessingException throwLocated(String message, Throwable thr, Location location) throws ProcessingException {
101 if (thr instanceof ProcessingException) {
102 ProcessingException pe = (ProcessingException)thr;
103 pe.addLocation(location);
104 throw pe;
105
106 } else if (thr instanceof LocatedRuntimeException) {
107 LocatedRuntimeException re = (LocatedRuntimeException)thr;
108 re.addLocation(location);
109 // Rethrow
110 throw re;
111 }
112
113 throw new ProcessingException(message, thr, location);
114 }
115
116 /**
117 * Throw a located exception given an existing exception and the locations where
118 * this exception was catched.
119 * <p>
120 * If the exception is already a <code>ProcessingException</code> or a {@link LocatedRuntimeException},
121 * the locations are added to the original exception's location chain and the original exception
122 * is rethrown (<code>description</code> is ignored) to limit exception nesting. Otherwise, a new
123 * <code>ProcessingException</code> is thrown, wrapping the original exception.
124 * <p>
125 * Note: this method returns an exception as a convenience if you want to keep the <code>throw</code>
126 * semantics in the caller code, i.e. write<br>
127 * <code> throw ProcessingException.throwLocated(...);</code><br>
128 * instead of<br>
129 * <code> ProcessingException.throwLocated(...);</code><br>
130 * <code> return;</code>
131 *
132 * @param message a message (can be <code>null</code>)
133 * @param thr the original exception (can be <code>null</code>)
134 * @param locations the locations (can be <code>null</code>)
135 * @return a (fake) located exception
136 * @throws ProcessingException or <code>LocatedRuntimeException</code>
137 */
138 public static ProcessingException throwLocated(String message, Throwable thr, List locations) throws ProcessingException {
139 MultiLocatable multiloc;
140 if (thr instanceof ProcessingException) {
141 multiloc = (ProcessingException)thr;
142 } else if (thr instanceof LocatedRuntimeException) {
143 multiloc = (LocatedRuntimeException)thr;
144 } else {
145 multiloc = new ProcessingException(message, thr);
146 }
147
148 if (locations != null) {
149 for (int i = 0; i < locations.size(); i++) {
150 multiloc.addLocation((Location)locations.get(i));
151 }
152 }
153
154 if (multiloc instanceof LocatedRuntimeException) {
155 throw (LocatedRuntimeException)multiloc;
156 } else {
157 throw (ProcessingException)multiloc;
158 }
159 }
160 }