Browse files

Avoid recompilation of web scripts

  • Loading branch information...
1 parent 462440a commit 22e449a1e9d4cac8cc4d67ef2d22b20e4af29329 @skuro committed Sep 22, 2011
View
2 pom.xml
@@ -4,7 +4,7 @@
<groupId>it.sk.spring.surf.clojure</groupId>
<artifactId>spring-webscripts-addon-clojure</artifactId>
<packaging>jar</packaging>
- <version>1.0-SNAPSHOT</version>
+ <version>1.0-alpha2</version>
<name>spring-webscripts-addon-clojure</name>
<url>http://skuro.tk</url>
View
79 ...main/java/org/springframework/extensions/webscripts/processor/ClojureScriptProcessor.java
@@ -1,6 +1,8 @@
package org.springframework.extensions.webscripts.processor;
import java.io.*;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
@@ -13,25 +15,24 @@
/**
* @author Carlo Sciolla &lt;skuro@skuro.tk&gt;
*/
-public class ClojureScriptProcessor extends AbstractScriptProcessor
-{
+public class ClojureScriptProcessor extends AbstractScriptProcessor {
private static final Log log = LogFactory.getLog(ClojureScriptProcessor.class);
+ private Map<String, WebScript> compiledWebScripts = Collections.synchronizedMap(new HashMap<String, WebScript>());
+
/* (non-Javadoc)
* @see org.springframework.extensions.surf.core.processor.Processor#getExtension()
*/
- public String getExtension()
- {
+ public String getExtension() {
return "clj";
}
/* (non-Javadoc)
* @see org.springframework.extensions.surf.core.processor.Processor#getName()
*/
- public String getName()
- {
+ public String getName() {
return "clojure";
}
@@ -41,25 +42,19 @@ public String getName()
* @param is the input stream
* @param out the writer. This can be null if no output is required.
* @param model the context model for the script
- * @return Object the return result of the executed script
+ * @return WebScript a new instance of the requested Clojure backed web script
*/
- @SuppressWarnings (value="unchecked")
- protected Object executeClojureScript(InputStream is, Writer out, Map<String, Object> model)
- {
+ @SuppressWarnings(value = "unchecked")
+ protected WebScript compileClojureScript(InputStream is, Writer out, Map<String, Object> model) {
log.debug("Executing Clojure script");
- log.debug ("This line is to get rid of an IDEA warning: " + out);
-
- this.addProcessorModelExtensions(model);
+ log.debug("This line is to get rid of an IDEA warning: " + out);
- try
- {
- WebScript script = (WebScript)clojure.lang.Compiler.load(new InputStreamReader(is));
- Map<String, Object> cljModel = (Map)script.run(is, out, model);
+ this.addProcessorModelExtensions(model);
- return cljModel;
- }
- catch (Exception exception)
- {
+ try {
+ WebScript script = (WebScript) clojure.lang.Compiler.load(new InputStreamReader(is));
+ return script;
+ } catch (Exception exception) {
throw new ScriptException("Error executing Clojure script", exception);
}
}
@@ -68,21 +63,17 @@ protected Object executeClojureScript(InputStream is, Writer out, Map<String, Ob
* @see org.springframework.extensions.webscripts.ScriptProcessor#findScript(java.lang.String)
*/
- public ScriptContent findScript(String path)
- {
- // TODO: maybe check path against (ns)?
+ public ScriptContent findScript(String path) {
+ // TODO: maybe check path against (ns)?
return getScriptLoader().getScript(path);
}
/* (non-Javadoc)
* @see org.springframework.extensions.webscripts.ScriptProcessor#executeScript(java.lang.String, java.util.Map)
*/
-
- public Object executeScript(String path, Map<String, Object> model)
- {
+ public Object executeScript(String path, Map<String, Object> model) {
ScriptContent scriptContent = findScript(path);
- if (scriptContent == null)
- {
+ if (scriptContent == null) {
throw new ScriptException("Unable to locate: " + path);
}
@@ -93,26 +84,42 @@ public Object executeScript(String path, Map<String, Object> model)
* @see org.springframework.extensions.webscripts.ScriptProcessor#executeScript(org.springframework.extensions.webscripts.ScriptContent, java.util.Map)
*/
- public Object executeScript(ScriptContent scriptContent, Map<String, Object> model)
- {
- return executeClojureScript(scriptContent.getInputStream(), null, model);
+ public Object executeScript(ScriptContent scriptContent, Map<String, Object> model) {
+ String path = scriptContent.getPath();
+ WebScript webscript = this.compiledWebScripts.get(path);
+ if (webscript == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("Compiling new Clojure webscript at path " + path);
+ }
+ webscript = compileClojureScript(scriptContent.getInputStream(), null, model);
+ }
+ if (webscript == null) {
+ throw new ScriptException("Cannot compile Clojure web script at path " + path);
+ }
+
+ synchronized (this.compiledWebScripts) {
+ if (log.isDebugEnabled()) {
+ log.debug("Caching Clojure webscript at path " + path);
+ }
+ this.compiledWebScripts.put(path, webscript);
+ }
+
+ return webscript.run(scriptContent.getInputStream(), null, model);
}
/* (non-Javadoc)
* @see org.springframework.extensions.webscripts.ScriptProcessor#unwrapValue(java.lang.Object)
*/
- public Object unwrapValue(Object value)
- {
+ public Object unwrapValue(Object value) {
return value;
}
/* (non-Javadoc)
* @see org.springframework.extensions.webscripts.ScriptProcessor#reset()
*/
- public void reset()
- {
+ public void reset() {
init();
}
}
View
20 ...st/java/org/springframework/extensions/webscripts/processor/ClojureScriptProcessorIT.java
@@ -29,8 +29,9 @@
/**
* Clojure1 - Simple Response Test
*
- * @throws IOException if Bad Thingsª happen
+ * @throws IOException if Bad Things� happen
*/
+ @Test
public void testClojure1() throws IOException
{
sendRequest(new GetRequest("/test/clojure1"), 200, "VALUE: SUCCESS");
@@ -39,17 +40,18 @@ public void testClojure1() throws IOException
/**
* WithArgs - Getting args from the query string
*/
+ @Test
public void testWithArgs() throws IOException
{
sendRequest(new GetRequest("/test/withargs/1?b=2"), 200, "VALUE: 3");
}
- @Ignore("The current spring web script test classes don't pass an output stream -> NPE")
- public void testWithOutput() throws IOException {
- GetRequest req = new GetRequest("/test/without");
- TestWebScriptServer.Response res = sendRequest(req, 200, "VALUE: SUCCESS");
- byte[] content = res.getContentAsByteArray();
- assertNotNull("failed to retrieve content", content);
- assertEquals("streamed content", new String(content, "UTF-8"));
- }
+// @Ignore("The current spring web script test classes don't pass an output stream -> NPE")
+// public void testWithOutput() throws IOException {
+// GetRequest req = new GetRequest("/test/without");
+// TestWebScriptServer.Response res = sendRequest(req, 200, "VALUE: SUCCESS");
+// byte[] content = res.getContentAsByteArray();
+// assertNotNull("failed to retrieve content", content);
+// assertEquals("streamed content", new String(content, "UTF-8"));
+// }
}

0 comments on commit 22e449a

Please sign in to comment.