KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > janino > JaninoConnection


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.driver.janino;
17
18 import scriptella.spi.AbstractConnection;
19 import scriptella.spi.ConnectionParameters;
20 import scriptella.spi.ParametersCallback;
21 import scriptella.spi.ProviderException;
22 import scriptella.spi.QueryCallback;
23 import scriptella.spi.Resource;
24
25 /**
26  * Scriptella connection adapter for Janino Script Evaluator.
27  * <p>For configuration details and examples see <a HREF="package-summary.html">overview page</a>.
28  *
29  * @author Fyodor Kupolov
30  * @version 1.0
31  */

32 public class JaninoConnection extends AbstractConnection {
33     private CodeCompiler compiler = new CodeCompiler();
34
35     /**
36      * Instantiates a new connection to Janino Script Evaluator.
37      * @param parameters connection parameters.
38      */

39     public JaninoConnection(ConnectionParameters parameters) {
40         super(Driver.DIALECT_IDENTIFIER, parameters);
41     }
42
43     public void executeScript(Resource scriptContent, ParametersCallback parametersCallback) throws ProviderException {
44         JaninoScript s = compiler.compileScript(scriptContent);
45         s.setParametersCallback(parametersCallback);
46         try {
47             s.execute();
48         } catch (Exception JavaDoc e) {
49             throw guessErrorStatement(new JaninoProviderException(
50                     "Script execution failed due to exception",e), scriptContent, s.getClass());
51         } finally {
52             //GC unused references
53
s.setParametersCallback(null);
54         }
55     }
56
57     /**
58      * Executes a query specified by its content.
59      * <p/>
60      *
61      * @param queryContent query content.
62      * @param parametersCallback callback to get parameter values.
63      * @param queryCallback callback to call for each result set element produced by this query.
64      * @see #executeScript(scriptella.spi.Resource, scriptella.spi.ParametersCallback)
65      */

66     public void executeQuery(Resource queryContent, ParametersCallback parametersCallback, QueryCallback queryCallback) throws ProviderException {
67
68         JaninoQuery q = compiler.compileQuery(queryContent);
69         q.setParametersCallback(parametersCallback);
70         q.setQueryCallback(queryCallback);
71         try {
72             q.execute();
73         } catch (Exception JavaDoc e) {
74             throw guessErrorStatement(new JaninoProviderException(
75                     "Query execution failed due to exception",e), queryContent, q.getClass());
76         } finally {
77             //GC unused references
78
q.setParametersCallback(null);
79             q.setQueryCallback(null);
80         }
81     }
82
83     /**
84      * Finds an error statement by introspecting the stack trace of exception.
85      * <p>Error statement is fetched from content resource.
86      * @param content script content.
87      * @param e exception to introspect.
88      * @param scriptClass class to use when finding an error statement.
89      * @return the same exception with a guessed error statement.
90      */

91     private static JaninoProviderException guessErrorStatement(JaninoProviderException e, Resource content, Class JavaDoc scriptClass) {
92         Throwable JavaDoc ex = e.getCause();
93         StackTraceElement JavaDoc[] trace = ex.getStackTrace();
94         final String JavaDoc scriptClassName = scriptClass.getName();
95         for (StackTraceElement JavaDoc el : trace) { //find the top most instance of generated class
96
String JavaDoc className = el.getClassName();
97             if (scriptClassName.equals(className)) {
98                 String JavaDoc st = CodeCompiler.getLine(content, el.getLineNumber());
99                 e.setErrorStatement(st);
100                 break;
101             }
102
103         }
104         return e;
105     }
106
107     /**
108      * Closes the connection and releases all related resources.
109      */

110     public void close() throws ProviderException {
111         compiler=null;
112     }
113
114 }
115
Popular Tags