KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > jexl > JexlConnection


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.jexl;
17
18 import org.apache.commons.jexl.JexlContext;
19 import org.apache.commons.jexl.Script;
20 import org.apache.commons.jexl.ScriptFactory;
21 import scriptella.spi.AbstractConnection;
22 import scriptella.spi.ConnectionParameters;
23 import scriptella.spi.ParametersCallback;
24 import scriptella.spi.ProviderException;
25 import scriptella.spi.QueryCallback;
26 import scriptella.spi.Resource;
27 import scriptella.util.IOUtils;
28
29 import java.io.IOException JavaDoc;
30 import java.util.IdentityHashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * Scriptella connection adapter for JEXL.
35  * <p>For configuration details and examples see <a HREF="package-summary.html">overview page</a>.
36  *
37  * @author Fyodor Kupolov
38  * @version 1.0
39  */

40 public class JexlConnection extends AbstractConnection {
41     private Map JavaDoc<Resource, Script> cache = new IdentityHashMap JavaDoc<Resource, Script>();
42
43     /**
44      * Instantiates a new connection to Janino Script Evaluator.
45      *
46      * @param parameters connection parameters.
47      */

48     public JexlConnection(ConnectionParameters parameters) {
49         super(Driver.DIALECT, parameters);
50     }
51
52     public void executeScript(Resource scriptContent, ParametersCallback parametersCallback) throws ProviderException {
53         Script script = compile(scriptContent);
54         try {
55             JexlContext ctx = new JexlContextMap(parametersCallback);
56             script.execute(ctx);
57         } catch (Exception JavaDoc e) {
58             throw new JexlProviderException("Failed to execute JEXL script", e);
59         }
60     }
61
62     public void executeQuery(Resource queryContent, ParametersCallback parametersCallback, QueryCallback queryCallback) throws ProviderException {
63         Script query = compile(queryContent);
64         try {
65             JexlContextMap ctx = new JexlContextMap(parametersCallback);
66             ctx.put("query", new Query(queryCallback, ctx));
67             query.execute(ctx);
68         } catch (Exception JavaDoc e) {
69             throw new JexlProviderException("Failed to execute JEXL script", e);
70         }
71     }
72
73     private Script compile(Resource resource) {
74         Script script = cache.get(resource);
75         if (script == null) {
76             String JavaDoc s;
77             try {
78                 s = IOUtils.toString(resource.open());
79             } catch (IOException JavaDoc e) {
80                 throw new JexlProviderException("Unable to open resource", e);
81             }
82
83             try {
84                 cache.put(resource, script = ScriptFactory.createScript(s));
85             } catch (Exception JavaDoc e) {
86                 throw new JexlProviderException("Failed to compile JEXL script", e);
87             }
88         }
89         return script;
90     }
91
92
93     /**
94      * Closes the connection and releases all related resources.
95      */

96     public void close() throws ProviderException {
97     }
98
99 }
100
Popular Tags