KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > core > ScriptExecutor


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.ContentEl;
19 import scriptella.configuration.OnErrorEl;
20 import scriptella.configuration.ScriptEl;
21 import scriptella.spi.Connection;
22 import scriptella.spi.DialectIdentifier;
23 import scriptella.spi.Resource;
24 import scriptella.util.ExceptionUtils;
25 import scriptella.util.StringUtils;
26
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29
30
31 /**
32  * TODO: Add documentation
33  *
34  * @author Fyodor Kupolov
35  * @version 1.0
36  */

37 public class ScriptExecutor extends ContentExecutor<ScriptEl> {
38     private static final Logger JavaDoc LOG = Logger.getLogger(ScriptExecutor.class.getName());
39     private final boolean debug=LOG.isLoggable(Level.FINE);
40
41     public ScriptExecutor(ScriptEl scriptEl) {
42         super(scriptEl);
43     }
44
45     public void execute(final DynamicContext ctx) {
46         Connection con = ctx.getConnection();
47         ScriptEl scriptEl = getElement();
48
49         Resource content = getContent(con.getDialectIdentifier());
50         if (content == ContentEl.NULL_CONTENT) {
51             warnEmptyContent();
52             return;
53         }
54         if (debug) {
55             LOG.fine("Executing script " + getLocation());
56         }
57         boolean repeat;
58         do {
59             repeat = false;
60             try {
61                 con.executeScript(content, ctx);
62                 if (debug) {
63                     LOG.fine("Script " + getLocation() + " completed");
64                 }
65
66             } catch (Throwable JavaDoc t) {
67                 if (scriptEl.getOnerrorElements() != null) {
68                     repeat = onError(t, new OnErrorHandler(scriptEl), ctx);
69                 } else {
70                     ExceptionUtils.throwUnchecked(t);
71                 }
72             }
73         } while (repeat); //repeat while onError returns retry
74
}
75
76     private void warnEmptyContent() {
77         LOG.info("Script " + getLocation() + " has no supported dialects");
78     }
79
80     /**
81      * Recursive on error fallback.
82      *
83      * @param t
84      * @param errorHandler
85      * @param ctx
86      * @return true if script execution should be retried
87      */

88     private boolean onError(Throwable JavaDoc t, OnErrorHandler errorHandler, DynamicContext ctx) {
89         OnErrorEl onErrorEl = errorHandler.onError(t);
90         Connection con = ctx.getConnection();
91         DialectIdentifier dialectId = con.getDialectIdentifier();
92         if (onErrorEl != null) { //if error handler present for this case
93
Resource content = onErrorEl.getContent(dialectId);
94             if (LOG.isLoggable(Level.INFO)) {
95                 LOG.log(Level.INFO, StringUtils.consoleFormat("Script " + getLocation() + " failed: " + t +
96                         "\nUsing onError handler: " + onErrorEl));
97             }
98
99             try {
100                 con.executeScript(content, ctx);
101                 return onErrorEl.isRetry();
102             } catch (Exception JavaDoc e) {
103                 return onError(e, errorHandler, ctx); //calling this method again and triying to find another onerror
104
}
105         } //if no onError found - rethrow the exception
106
ExceptionUtils.throwUnchecked(t);
107         return false;
108
109     }
110
111     public static ExecutableElement prepare(final ScriptEl s) {
112         ExecutableElement se = new ScriptExecutor(s);
113         se = StatisticInterceptor.prepare(se, s.getLocation());
114         se = TxInterceptor.prepare(se, s);
115         se = ConnectionInterceptor.prepare(se, s);
116         se = ExceptionInterceptor.prepare(se, s.getLocation());
117         se = IfInterceptor.prepare(se, s);
118
119         return se;
120     }
121 }
122
Popular Tags