You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by di...@apache.org on 2012/08/09 20:35:28 UTC
svn commit: r1371364 - in
/velocity/sandbox/jsr223/velocity-engine-scripting/src:
main/java/org/apache/velocity/script/
main/java/org/apache/velocity/script/util/
test/java/org/apache/velocity/script/test/resources/
Author: dishara
Date: Thu Aug 9 18:35:28 2012
New Revision: 1371364
URL: http://svn.apache.org/viewvc?rev=1371364&view=rev
Log:
Adding template caching with some enhancement
Added:
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/util/
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/util/ScriptResourceHolder.java
Modified:
velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm
Modified: velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java?rev=1371364&r1=1371363&r2=1371364&view=diff
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java (original)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/VelocityScriptEngine.java Thu Aug 9 18:35:28 2012
@@ -22,6 +22,9 @@ package org.apache.velocity.script;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.script.util.ScriptResourceHolder;
+import sun.font.Script;
import javax.script.*;
import java.io.*;
@@ -196,19 +199,17 @@ public class VelocityScriptEngine implem
* re-parsed or recompiled before execution. State left in the engine from previous executions, including variable values and
* compiled procedures may be visible during this execution.
*
- * @param s The script to be executed by the script engine.
+ * @param s The script to be executed by the script engine.
* @param scriptContext A ScriptContext exposing sets of attributes in different scopes. The meanings of the
* scopes ScriptContext.GLOBAL_SCOPE, and ScriptContext.ENGINE_SCOPE are defined in the specification.
* @return The value returned from the execution of the script.
* @throws ScriptException
*/
public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
-// return eval(new StringReader(s), scriptContext);
- if (s == null) {
+ if (s == null) {
throw new NullPointerException("Reader passed cannot be null");
}
constructVelocityEngine(scriptContext);
-// String fileName = getTargetFilename(scriptContext);
VelocityContext velocityContext = getVelocityContext(scriptContext);
Writer outPut;
@@ -217,14 +218,25 @@ public class VelocityScriptEngine implem
} else {
outPut = new StringWriter();
}
- boolean result;
+ boolean result = false;
try {
// Check for velocity tools vm file
if (scriptContext.getAttribute(VelocityScriptEngine.FILENAME) != null) {
- Template template = velocityEngine.getTemplate(
- scriptContext.getAttribute(VelocityScriptEngine.FILENAME).toString());
- template.merge(velocityContext, outPut);
+ Template template = null;
+ String fileName = scriptContext.getAttribute(VelocityScriptEngine.FILENAME).toString();
+ //Cache hit
+ if (ScriptResourceHolder.hasTemplate(fileName)) {
+ template = ScriptResourceHolder.getTemplate(fileName);
+ } else {
+ try {
+ template = velocityEngine.getTemplate(fileName);
+ ScriptResourceHolder.putTemplate(fileName,template);
+ template.merge(velocityContext, outPut);
+ } catch(ResourceNotFoundException e1){
+ }
+
+ }
}
result = velocityEngine.evaluate(velocityContext, outPut, VelocityScriptEngine.DEFAULT_LOG_TAG, s);
} catch (Exception exp) {
@@ -235,8 +247,6 @@ public class VelocityScriptEngine implem
}
-
-
/**
* Same as eval(String, ScriptContext) where the source of the script is read from a Reader.
*
@@ -252,7 +262,6 @@ public class VelocityScriptEngine implem
throw new NullPointerException("Reader passed cannot be null");
}
constructVelocityEngine(scriptContext);
- String fileName = getTargetFilename(scriptContext);
VelocityContext velocityContext = getVelocityContext(scriptContext);
Writer outPut;
@@ -266,11 +275,24 @@ public class VelocityScriptEngine implem
try {
//Check for velocity tools vm file
if (scriptContext.getAttribute(VelocityScriptEngine.FILENAME) != null) {
- Template template = velocityEngine.getTemplate(
- scriptContext.getAttribute(VelocityScriptEngine.FILENAME).toString());
- template.merge(velocityContext, outPut);
+ Template template = null;
+ String fileName = scriptContext.getAttribute(VelocityScriptEngine.FILENAME).toString();
+ //Cache hit
+ if (ScriptResourceHolder.hasTemplate(fileName)) {
+ template = ScriptResourceHolder.getTemplate(fileName);
+ } else {
+ try {
+ template = velocityEngine.getTemplate(fileName);
+ ScriptResourceHolder.putTemplate(fileName,template);
+ template.merge(velocityContext, outPut);
+ } catch(ResourceNotFoundException e1){
+ }
+
+ }
}
- result = velocityEngine.evaluate(velocityContext, outPut, fileName, reader);
+
+ result = velocityEngine.evaluate(velocityContext, outPut, VelocityScriptEngine.DEFAULT_LOG_TAG, reader);
+
} catch (Exception exp) {
return new ScriptException(exp);
}
Added: velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/util/ScriptResourceHolder.java
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/util/ScriptResourceHolder.java?rev=1371364&view=auto
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/util/ScriptResourceHolder.java (added)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/main/java/org/apache/velocity/script/util/ScriptResourceHolder.java Thu Aug 9 18:35:28 2012
@@ -0,0 +1,47 @@
+package org.apache.velocity.script.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.velocity.Template;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ScriptResourceHolder {
+
+ /**
+ * Holds the template with their matching template file name
+ */
+private static Map<String,Template> templateCache = new HashMap<String, Template>();
+
+
+ public static void putTemplate(String fileName,Template template){
+ templateCache.put(fileName,template);
+ }
+
+ public static Template getTemplate(String fileName){
+ return templateCache.get(fileName);
+ }
+
+ public static boolean hasTemplate(String fileName){
+ return templateCache.containsKey(fileName);
+ }
+
+}
Modified: velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm
URL: http://svn.apache.org/viewvc/velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm?rev=1371364&r1=1371363&r2=1371364&view=diff
==============================================================================
--- velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm (original)
+++ velocity/sandbox/jsr223/velocity-engine-scripting/src/test/java/org/apache/velocity/script/test/resources/eventtool.vm Thu Aug 9 18:35:28 2012
@@ -2,4 +2,4 @@ $event;
Event Created by $event.getName()
Event Created on $event.getDate()
-
+Event ID is $event.getID()