01: /*
02: * Copyright 2006-2007 The Scriptella Project Team.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.Script;
19: import org.apache.commons.jexl.ScriptFactory;
20: import scriptella.spi.AbstractConnection;
21: import scriptella.spi.ConnectionParameters;
22: import scriptella.spi.ParametersCallback;
23: import scriptella.spi.ProviderException;
24: import scriptella.spi.QueryCallback;
25: import scriptella.spi.Resource;
26: import scriptella.util.IOUtils;
27:
28: import java.io.IOException;
29: import java.util.IdentityHashMap;
30: import java.util.Map;
31:
32: /**
33: * Scriptella connection adapter for JEXL.
34: * <p>For configuration details and examples see <a href="package-summary.html">overview page</a>.
35: *
36: * @author Fyodor Kupolov
37: * @version 1.0
38: */
39: public class JexlConnection extends AbstractConnection {
40: private Map<Resource, Script> cache = new IdentityHashMap<Resource, Script>();
41:
42: /**
43: * Instantiates a new connection to JEXL.
44: *
45: * @param parameters connection parameters.
46: */
47: public JexlConnection(ConnectionParameters parameters) {
48: super (Driver.DIALECT, parameters);
49: }
50:
51: public void executeScript(Resource scriptContent,
52: ParametersCallback parametersCallback)
53: throws ProviderException {
54: run(scriptContent, new JexlContextMap(parametersCallback));
55: }
56:
57: public void executeQuery(Resource queryContent,
58: ParametersCallback parametersCallback,
59: QueryCallback queryCallback) throws ProviderException {
60: run(queryContent, new JexlContextMap(parametersCallback,
61: queryCallback));
62: }
63:
64: private void run(Resource resource, JexlContextMap ctx) {
65: Script script = cache.get(resource);
66: if (script == null) {
67: String s;
68: try {
69: s = IOUtils.toString(resource.open());
70: } catch (IOException e) {
71: throw new JexlProviderException(
72: "Unable to open resource", e);
73: }
74:
75: try {
76: cache.put(resource, script = ScriptFactory
77: .createScript(s));
78: } catch (Exception e) {
79: throw new JexlProviderException(
80: "Failed to compile JEXL script", e);
81: }
82: }
83: try {
84: script.execute(ctx);
85: } catch (Exception e) {
86: throw new JexlProviderException(
87: "Failed to execute JEXL script", e);
88: }
89: }
90:
91: /**
92: * Closes the connection and releases all related resources.
93: */
94: public void close() throws ProviderException {
95: cache = null;
96: }
97:
98: }
|