KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > core > ExceptionInterceptor


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 scriptella.core;
17
18 import scriptella.configuration.Location;
19
20
21 /**
22  * Intercepts exceptions thrown by wrapped executable elements.
23  * Additionally checks if ETL has been cancelled.
24  *
25  * @author Fyodor Kupolov
26  * @version 1.0
27  */

28 public class ExceptionInterceptor extends ElementInterceptor {
29     private Location location;
30
31     public ExceptionInterceptor(ExecutableElement next, Location location) {
32         super(next);
33         this.location = location;
34     }
35
36     public void execute(final DynamicContext ctx) {
37         try {
38             EtlCancelledException.checkEtlCancelled();
39             executeNext(ctx);
40         } catch (ExecutionException e) {
41             //do not wrap already intercepted exceptions
42
throw e;
43         } catch (Exception JavaDoc e) {
44             throw new ExecutionException(e, location);
45         }
46     }
47
48     public static ExecutableElement prepare(
49             final ExecutableElement next, final Location loc) {
50         return new ExceptionInterceptor(next, loc);
51     }
52
53     public static class ExecutionException extends SystemException {
54         private Location location;
55
56         public ExecutionException(Throwable JavaDoc cause, Location location) {
57             this(location + " failed: " + cause.getMessage(), cause, location);
58         }
59
60         public ExecutionException(String JavaDoc message, Throwable JavaDoc cause,
61                                   Location location) {
62             super(message, cause);
63             this.location = location;
64         }
65
66         /**
67          * @return Failed element location.
68          */

69         public Location getLocation() {
70             return location;
71         }
72     }
73 }
74
Popular Tags