sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From my...@apache.org
Subject svn commit: r990926 - /sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityTemplatesScriptEngine.java
Date Mon, 30 Aug 2010 20:12:35 GMT
Author: mykee
Date: Mon Aug 30 20:12:34 2010
New Revision: 990926

URL: http://svn.apache.org/viewvc?rev=990926&view=rev
Log:
SLING-1700 contrib/scripting/velocity embeds velocity, causing classloading issue at runtime

Modified:
    sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityTemplatesScriptEngine.java

Modified: sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityTemplatesScriptEngine.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityTemplatesScriptEngine.java?rev=990926&r1=990925&r2=990926&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityTemplatesScriptEngine.java
(original)
+++ sling/trunk/contrib/scripting/velocity/src/main/java/org/apache/sling/scripting/velocity/VelocityTemplatesScriptEngine.java
Mon Aug 30 20:12:34 2010
@@ -41,17 +41,31 @@ public class VelocityTemplatesScriptEngi
     public VelocityTemplatesScriptEngine(ScriptEngineFactory factory) {
         super(factory);
 
-        velocity = new VelocityEngine();
+        final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
         try {
+	        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+	        
+	        velocity = new VelocityEngine();
             velocity.init();
         } catch (Exception e) {
             throw new RuntimeException("Exception in Velocity.init() "
                 + e.getMessage(), e);
-        }
+	    } finally {
+	    	Thread.currentThread().setContextClassLoader(oldClassLoader);
+	    }
+
     }
 
     public Object eval(Reader script, ScriptContext scriptContext)
             throws ScriptException {
+		if (script == null) {
+			throw new IllegalArgumentException(
+					"Method argument script must not be null.");
+		}
+		if (scriptContext == null) {
+			throw new IllegalArgumentException(
+					"Method argument scriptContext must not be null.");
+		}
         Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
         SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
         if (helper == null) {
@@ -66,23 +80,31 @@ public class VelocityTemplatesScriptEngi
 
         String scriptName = helper.getScript().getScriptResource().getPath();
 
-        // initialize the Velocity context
-        final VelocityContext c = new VelocityContext();
-        for (Object entryObj : bindings.entrySet()) {
-            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) entryObj;
-            c.put((String) entry.getKey(), entry.getValue());
-        }
-
-        // let Velocity evaluate the script, and send the output to the browser
-        final String logTag = getClass().getSimpleName();
-        Writer w = scriptContext.getWriter();
+        final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
+        
         try {
-            velocity.evaluate(c, w, logTag, script);
-            w.toString();
-        } catch (Throwable t) {
-            throw new ScriptException("Failure running script " + scriptName
-                + ": " + t);
-        }
+        	Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+        	
+	        // initialize the Velocity context
+	        final VelocityContext c = new VelocityContext();
+	        for (Object entryObj : bindings.entrySet()) {
+	            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) entryObj;
+	            c.put((String) entry.getKey(), entry.getValue());
+	        }
+	
+	        // let Velocity evaluate the script, and send the output to the browser
+	        final String logTag = getClass().getSimpleName();
+	        Writer w = scriptContext.getWriter();
+	        try {
+	            velocity.evaluate(c, w, logTag, script);
+	            w.toString();
+	        } catch (Throwable t) {
+	            throw new ScriptException("Failure running script " + scriptName
+	                + ": " + t);
+	        }
+	    } finally {
+	    	Thread.currentThread().setContextClassLoader(oldClassLoader);
+	    }
 
         return null;
     }



Mime
View raw message