KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > velocity > VelocityConnection


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.velocity;
17
18 import org.apache.velocity.app.VelocityEngine;
19 import org.apache.velocity.context.Context;
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 JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.io.Writer JavaDoc;
31 import java.net.URL JavaDoc;
32
33 /**
34  * Represents a session to velocity engine.
35  */

36 public class VelocityConnection extends AbstractConnection {
37     public static final String JavaDoc OUTPUT_ENCODING = "output.encoding";
38     private final URL JavaDoc url;
39     private final VelocityEngine engine;
40     private final VelocityContextAdapter adapter;
41     private Writer JavaDoc writer;//lazy initialized
42
private String JavaDoc encoding;//encoding for writer
43

44
45     /**
46      * Instantiates a velocity connection.
47      *
48      * @param parameters connection parameters.
49      */

50     public VelocityConnection(ConnectionParameters parameters) {
51         super(Driver.DIALECT, parameters);
52         url = parameters.getResolvedUrl();
53         engine = new VelocityEngine();
54         engine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, Driver.LOG_SYSTEM);
55         engine.setProperty("velocimacro.library", "");//unnecessary file in our case
56
try {
57             engine.init();
58         } catch (Exception JavaDoc e) {
59             throw new VelocityProviderException("Unable to initialize engine", e);
60         }
61         adapter = new VelocityContextAdapter();
62         encoding = parameters.getCharsetProperty(OUTPUT_ENCODING);
63     }
64
65     /**
66      * Executes a script specified by its content.
67      * <p>scriptContent may be used as a key for caching purposes, i.e.
68      * provider may precompile scripts and use compiled versions for subsequent executions.
69      * <p>This method is synchronized to to prevent multiple threads from working with the same writer.
70      * Additionally single velocityEngine and context adapter instances are used.
71      *
72      * @param scriptContent script content.
73      * @param parametersCallback callback to get parameter values.
74      */

75     public void executeScript(Resource scriptContent, ParametersCallback parametersCallback) throws ProviderException {
76         //todo Current solution is slow, use per scriptContent caching by providing a custom Velocity ResourceLoader
77
//todo also make Resource identifiable, i.e. replace url.getFile with resource name/location
78
adapter.setCallback(parametersCallback);//we may use single context+engine because method is synchronized
79
Reader JavaDoc reader = null;
80         try {
81             reader = scriptContent.open();
82             engine.evaluate(adapter, getWriter(), url.getFile(), reader);
83         } catch (Exception JavaDoc e) {
84             throw new VelocityProviderException("Unable to execute script", e);
85         } finally {
86             adapter.setCallback(null);//cleaning up to avoid mem leaks
87
IOUtils.closeSilently(reader);
88         }
89     }
90
91     /**
92      * Executes a query specified by its content.
93      * <p/>
94      *
95      * @param queryContent query content.
96      * @param parametersCallback callback to get parameter values.
97      * @param queryCallback callback to call for each result set element produced by this query.
98      * @see #executeScript(scriptella.spi.Resource, scriptella.spi.ParametersCallback)
99      */

100     public void executeQuery(Resource queryContent, ParametersCallback parametersCallback, QueryCallback queryCallback) throws ProviderException {
101         throw new UnsupportedOperationException JavaDoc("Query execution is not supported yet");
102     }
103
104     private Writer JavaDoc getWriter() {
105         if (writer == null) {
106             try {
107                 writer = IOUtils.getWriter(IOUtils.getOutputStream(url), encoding);
108             } catch (IOException JavaDoc e) {
109                 throw new VelocityProviderException("Unable to open URL " + url + " for output", e);
110             }
111         }
112         return writer;
113     }
114
115
116     /**
117      * Closes the connection and releases all related resources.
118      */

119     public synchronized void close() throws ProviderException {
120         if (writer != null) {
121             IOUtils.closeSilently(writer);
122             writer = null;
123         }
124     }
125
126     /**
127      * Velocity Context adapter class for {@link ParametersCallback}.
128      */

129     private static class VelocityContextAdapter implements Context {
130         private ParametersCallback callback;
131
132         public void setCallback(ParametersCallback callback) {
133             this.callback = callback;
134         }
135
136         public Object JavaDoc put(String JavaDoc key, Object JavaDoc value) {
137             throw new UnsupportedOperationException JavaDoc();
138         }
139
140         public Object JavaDoc get(String JavaDoc key) {
141             return callback.getParameter(key);
142         }
143
144         public boolean containsKey(Object JavaDoc key) {
145             return false;
146         }
147
148         public Object JavaDoc[] getKeys() {
149             throw new UnsupportedOperationException JavaDoc();
150         }
151
152         public Object JavaDoc remove(Object JavaDoc key) {
153             throw new UnsupportedOperationException JavaDoc();
154         }
155     }
156
157 }
158
Popular Tags