001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon;
018:
019: import java.util.List;
020:
021: import org.apache.cocoon.util.location.LocatedException;
022: import org.apache.cocoon.util.location.LocatedRuntimeException;
023: import org.apache.cocoon.util.location.Location;
024: import org.apache.cocoon.util.location.MultiLocatable;
025:
026: /**
027: * This Exception is thrown every time there is a problem in processing
028: * a request.
029: *
030: * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
031: * (Apache Software Foundation)
032: * @version CVS $Id: ProcessingException.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public class ProcessingException extends LocatedException {
035:
036: /**
037: * Construct a new <code>ProcessingException</code> instance.
038: */
039: public ProcessingException(String message) {
040: super (message);
041: }
042:
043: /**
044: * Creates a new <code>ProcessingException</code> instance.
045: *
046: * @param ex an <code>Exception</code> value
047: */
048: public ProcessingException(Exception ex) {
049: super (ex.getMessage(), ex);
050: }
051:
052: /**
053: * Construct a new <code>ProcessingException</code> that references
054: * a parent Exception.
055: */
056: public ProcessingException(String message, Throwable t) {
057: super (message, t);
058: }
059:
060: /**
061: * Construct a new <code>ProcessingException</code> that has an associated location.
062: */
063: public ProcessingException(String message, Location location) {
064: super (message, location);
065: }
066:
067: /**
068: * Construct a new <code>ProcessingException</code> that has a parent exception
069: * and an associated location.
070: * <p>
071: * This constructor is protected to enforce the use of {@link #throwLocated(String, Throwable, Location)}
072: * which limits exception nesting as far as possible.
073: */
074: protected ProcessingException(String message, Throwable t,
075: Location location) {
076: super (message, t, location);
077: }
078:
079: /**
080: * Throw a located exception given an existing exception and the location where
081: * this exception was catched.
082: * <p>
083: * If the exception is already a <code>ProcessingException</code> or a {@link LocatedRuntimeException},
084: * the location is added to the original exception's location chain and the original exception
085: * is rethrown (<code>description</code> is ignored) to limit exception nesting. Otherwise, a new
086: * <code>ProcessingException</code> is thrown, wrapping the original exception.
087: * <p>
088: * Note: this method returns an exception as a convenience if you want to keep the <code>throw</code>
089: * semantics in the caller code, i.e. write<br>
090: * <code> throw ProcessingException.throwLocated(...);</code><br>
091: * instead of<br>
092: * <code> ProcessingException.throwLocated(...);</code><br>
093: * <code> return;</code>
094: *
095: * @param message a message (can be <code>null</code>)
096: * @param thr the original exception (can be <code>null</code>)
097: * @param location the location (can be <code>null</code>)
098: * @return a (fake) located exception
099: * @throws ProcessingException or <code>LocatedRuntimeException</code>
100: */
101: public static ProcessingException throwLocated(String message,
102: Throwable thr, Location location)
103: throws ProcessingException {
104: if (thr instanceof ProcessingException) {
105: ProcessingException pe = (ProcessingException) thr;
106: pe.addLocation(location);
107: throw pe;
108:
109: } else if (thr instanceof LocatedRuntimeException) {
110: LocatedRuntimeException re = (LocatedRuntimeException) thr;
111: re.addLocation(location);
112: // Rethrow
113: throw re;
114: }
115:
116: throw new ProcessingException(message, thr, location);
117: }
118:
119: /**
120: * Throw a located exception given an existing exception and the locations where
121: * this exception was catched.
122: * <p>
123: * If the exception is already a <code>ProcessingException</code> or a {@link LocatedRuntimeException},
124: * the locations are added to the original exception's location chain and the original exception
125: * is rethrown (<code>description</code> is ignored) to limit exception nesting. Otherwise, a new
126: * <code>ProcessingException</code> is thrown, wrapping the original exception.
127: * <p>
128: * Note: this method returns an exception as a convenience if you want to keep the <code>throw</code>
129: * semantics in the caller code, i.e. write<br>
130: * <code> throw ProcessingException.throwLocated(...);</code><br>
131: * instead of<br>
132: * <code> ProcessingException.throwLocated(...);</code><br>
133: * <code> return;</code>
134: *
135: * @param message a message (can be <code>null</code>)
136: * @param thr the original exception (can be <code>null</code>)
137: * @param locations the locations (can be <code>null</code>)
138: * @return a (fake) located exception
139: * @throws ProcessingException or <code>LocatedRuntimeException</code>
140: */
141: public static ProcessingException throwLocated(String message,
142: Throwable thr, List locations) throws ProcessingException {
143: MultiLocatable multiloc;
144: if (thr instanceof ProcessingException) {
145: multiloc = (ProcessingException) thr;
146: } else if (thr instanceof LocatedRuntimeException) {
147: multiloc = (LocatedRuntimeException) thr;
148: } else {
149: multiloc = new ProcessingException(message, thr);
150: }
151:
152: if (locations != null) {
153: for (int i = 0; i < locations.size(); i++) {
154: multiloc.addLocation((Location) locations.get(i));
155: }
156: }
157:
158: if (multiloc instanceof LocatedRuntimeException) {
159: throw (LocatedRuntimeException) multiloc;
160: } else {
161: throw (ProcessingException) multiloc;
162: }
163: }
164: }
|