| 1 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 ; 29 import java.io.Reader ; 30 import java.io.Writer ; 31 import java.net.URL ; 32 33 36 public class VelocityConnection extends AbstractConnection { 37 public static final String OUTPUT_ENCODING = "output.encoding"; 38 private final URL url; 39 private final VelocityEngine engine; 40 private final VelocityContextAdapter adapter; 41 private Writer writer; private String encoding; 44 45 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", ""); try { 57 engine.init(); 58 } catch (Exception e) { 59 throw new VelocityProviderException("Unable to initialize engine", e); 60 } 61 adapter = new VelocityContextAdapter(); 62 encoding = parameters.getCharsetProperty(OUTPUT_ENCODING); 63 } 64 65 75 public void executeScript(Resource scriptContent, ParametersCallback parametersCallback) throws ProviderException { 76 adapter.setCallback(parametersCallback); Reader reader = null; 80 try { 81 reader = scriptContent.open(); 82 engine.evaluate(adapter, getWriter(), url.getFile(), reader); 83 } catch (Exception e) { 84 throw new VelocityProviderException("Unable to execute script", e); 85 } finally { 86 adapter.setCallback(null); IOUtils.closeSilently(reader); 88 } 89 } 90 91 100 public void executeQuery(Resource queryContent, ParametersCallback parametersCallback, QueryCallback queryCallback) throws ProviderException { 101 throw new UnsupportedOperationException ("Query execution is not supported yet"); 102 } 103 104 private Writer getWriter() { 105 if (writer == null) { 106 try { 107 writer = IOUtils.getWriter(IOUtils.getOutputStream(url), encoding); 108 } catch (IOException e) { 109 throw new VelocityProviderException("Unable to open URL " + url + " for output", e); 110 } 111 } 112 return writer; 113 } 114 115 116 119 public synchronized void close() throws ProviderException { 120 if (writer != null) { 121 IOUtils.closeSilently(writer); 122 writer = null; 123 } 124 } 125 126 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 put(String key, Object value) { 137 throw new UnsupportedOperationException (); 138 } 139 140 public Object get(String key) { 141 return callback.getParameter(key); 142 } 143 144 public boolean containsKey(Object key) { 145 return false; 146 } 147 148 public Object [] getKeys() { 149 throw new UnsupportedOperationException (); 150 } 151 152 public Object remove(Object key) { 153 throw new UnsupportedOperationException (); 154 } 155 } 156 157 } 158 | Popular Tags |