Author: tfischer
Date: Sun May 5 20:30:29 2013
New Revision: 1479384
URL: http://svn.apache.org/r1479384
Log:
TORQUE-272 cache groovy shell and compiled groovy scripts
Modified:
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/TemplateOutletImpl.java
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyScriptOutlet.java
Modified:
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/TemplateOutletImpl.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/TemplateOutletImpl.java?rev=1479384&r1=1479383&r2=1479384&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/TemplateOutletImpl.java
(original)
+++
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/TemplateOutletImpl.java
Sun May 5 20:30:29 2013
@@ -99,11 +99,11 @@ public abstract class TemplateOutletImpl
* @throws ConfigurationException if the template cannot be loaded.
*/
protected TemplateOutletImpl(
- QualifiedName name,
- ConfigurationProvider configurationProvider,
- String path,
- String encoding,
- TemplateFilter templateFilter)
+ final QualifiedName name,
+ final ConfigurationProvider configurationProvider,
+ final String path,
+ final String encoding,
+ final TemplateFilter templateFilter)
throws ConfigurationException
{
super(name);
@@ -122,11 +122,10 @@ public abstract class TemplateOutletImpl
this.templateFilter = templateFilter;
}
- public String getContent(ControllerState controllerState)
+ public String getContent(final ControllerState controllerState)
throws ConfigurationException
{
- TokenReplacer tokenReplacer = new TokenReplacer(controllerState);
- String detokenizedPath = tokenReplacer.process(path);
+ String detokenizedPath = getDetokenizedPath(controllerState);
String result = contentMap.get(detokenizedPath);
if (result == null)
@@ -163,6 +162,13 @@ public abstract class TemplateOutletImpl
return result;
}
+ protected String getDetokenizedPath(final ControllerState controllerState)
+ {
+ TokenReplacer tokenReplacer = new TokenReplacer(controllerState);
+ String detokenizedPath = tokenReplacer.process(path);
+ return detokenizedPath;
+ }
+
/**
* Loads the template, possibly filtering the content..
*
@@ -176,9 +182,9 @@ public abstract class TemplateOutletImpl
* @throws IOException if an error occurs while reading the template.
*/
protected String load(
- InputStream inputStream,
- String encoding,
- TemplateFilter filter)
+ final InputStream inputStream,
+ final String encoding,
+ final TemplateFilter filter)
throws IOException
{
InputStream filteredStream;
@@ -216,6 +222,16 @@ public abstract class TemplateOutletImpl
}
/**
+ * Returns the path to the template.
+ *
+ * @return the path to the template, not null.
+ */
+ public String getPath()
+ {
+ return path;
+ }
+
+ /**
* Returns a String representation of this outlet for debugging purposes.
*
* @return a String representation of this outlet, never null.
Modified:
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyScriptOutlet.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyScriptOutlet.java?rev=1479384&r1=1479383&r2=1479384&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyScriptOutlet.java
(original)
+++
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/template/groovy/GroovyScriptOutlet.java
Sun May 5 20:30:29 2013
@@ -21,7 +21,9 @@ package org.apache.torque.generator.temp
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
+import groovy.lang.Script;
+import java.util.HashMap;
import java.util.Map;
import org.apache.torque.generator.GeneratorException;
@@ -36,6 +38,12 @@ import org.apache.torque.generator.qname
*/
public class GroovyScriptOutlet extends GroovyOutlet
{
+ /** The groovy shell to execute groovy scripts. */
+ private static GroovyShell groovyShell = new GroovyShell();
+
+ /** The cached compiled scripts, keyed by the detokenized path. */
+ private static Map<String, Script> cachedScripts
+ = new HashMap<String, Script>();
/**
* Constructs a new GroovyScriptOutlet.
@@ -69,9 +77,16 @@ public class GroovyScriptOutlet extends
{
try
{
+ final String detokenizedPath = getDetokenizedPath(controllerState);
+ Script script = cachedScripts.get(detokenizedPath);
+ if (script == null)
+ {
+ script = groovyShell.parse(getContent(controllerState));
+ cachedScripts.put(detokenizedPath, script);
+ }
final Binding scriptBinding = new Binding(binding);
- final GroovyShell shell = new GroovyShell(scriptBinding);
- final Object result = shell.evaluate(getContent(controllerState));
+ script.setBinding(scriptBinding);
+ final Object result = script.run();
if (result == null)
{
return "";
@@ -85,4 +100,5 @@ public class GroovyScriptOutlet extends
e);
}
}
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]