You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:04:23 UTC

[sling-org-apache-sling-scripting-java] 07/16: SLING-1385 : Recompile java scripts on modification and avoid periodic checks

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.scripting.java-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-java.git

commit bfc4376767c22c779d16078f9ad8369a1ac02308
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Feb 17 15:31:27 2010 +0000

    SLING-1385 : Recompile java scripts on modification and avoid periodic checks
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/scripting/java@911018 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/scripting/java/CompilationContext.java   | 20 ++++---
 .../scripting/java/JavaScriptEngineFactory.java    | 66 +++++++++++++++++-----
 .../org/apache/sling/scripting/java/Options.java   | 32 -----------
 .../sling/scripting/java/ServletWrapper.java       | 10 ++--
 .../OSGI-INF/metatype/metatype.properties          | 10 ----
 5 files changed, 68 insertions(+), 70 deletions(-)

diff --git a/src/main/java/org/apache/sling/scripting/java/CompilationContext.java b/src/main/java/org/apache/sling/scripting/java/CompilationContext.java
index ebf646c..764d95b 100644
--- a/src/main/java/org/apache/sling/scripting/java/CompilationContext.java
+++ b/src/main/java/org/apache/sling/scripting/java/CompilationContext.java
@@ -52,7 +52,7 @@ public class CompilationContext {
 
     private ServletCache servletCache;
 
-    private long lastModificationTest = 0L;
+    private volatile long lastModificationTest = 0L;
     private int removed = 0;
 
     private Class<?> servletClass;
@@ -131,18 +131,22 @@ public class CompilationContext {
         return false;
     }
 
+    public long getLastModificationTest() {
+        return this.lastModificationTest;
+    }
+
+    public void setLastModificationTest(final long value) {
+        this.lastModificationTest = value;
+    }
+
     /**
      * Check if the compiled class file is older than the source file
      */
     public boolean isOutDated() {
-        if (this.options.getModificationTestInterval() > 0) {
-
-            if (this.lastModificationTest
-                + (this.options.getModificationTestInterval() * 1000) > System.currentTimeMillis()) {
-                return false;
-            }
-            this.lastModificationTest = System.currentTimeMillis();
+        if ( this.lastModificationTest > 0 ) {
+            return false;
         }
+        this.lastModificationTest = System.currentTimeMillis();
 
         final long sourceLastModified = this.ioProvider.lastModified(getSourcePath());
 
diff --git a/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java b/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
index 605c0ca..2e1e4a6 100644
--- a/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
+++ b/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
@@ -19,6 +19,8 @@ package org.apache.sling.scripting.java;
 import static org.apache.sling.api.scripting.SlingBindings.SLING;
 
 import java.io.Reader;
+import java.util.Dictionary;
+import java.util.Hashtable;
 
 import javax.script.Bindings;
 import javax.script.ScriptContext;
@@ -27,6 +29,7 @@ import javax.script.ScriptException;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 
+import org.apache.sling.api.SlingConstants;
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingIOException;
 import org.apache.sling.api.SlingServletException;
@@ -39,7 +42,10 @@ import org.apache.sling.commons.classloader.ClassLoaderWriter;
 import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
 import org.apache.sling.scripting.api.AbstractScriptEngineFactory;
 import org.apache.sling.scripting.api.AbstractSlingScriptEngine;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.ComponentContext;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -49,19 +55,19 @@ import org.slf4j.LoggerFactory;
  * @scr.component label="%javahandler.name" description="%javahandler.description"
  * @scr.property name="service.description" value="Java Servlet Script Handler"
  * @scr.property name="service.vendor" value="The Apache Software Foundation"
- * @scr.service
+ * @scr.service interface="javax.script.ScriptEngineFactory"
  *
  * @scr.property name="java.javaEncoding" value="UTF-8"
  * @scr.property name="java.compilerSourceVM" value="1.5"
  * @scr.property name="java.compilerTargetVM" value="1.5"
- * @scr.property name="java.development" value="true" type="Boolean"
- * @scr.property name="java.modificationTestInterval" value="-1"
  * @scr.property name="java.classdebuginfo" value="true" type="Boolean"
  */
-public class JavaScriptEngineFactory extends AbstractScriptEngineFactory {
+public class JavaScriptEngineFactory
+    extends AbstractScriptEngineFactory
+    implements EventHandler {
 
     /** default logger */
-    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Logger logger = LoggerFactory.getLogger(getClass());
 
     /**
      * @scr.reference
@@ -90,6 +96,8 @@ public class JavaScriptEngineFactory extends AbstractScriptEngineFactory {
     /** Compiler options. */
     private Options compilerOptions;
 
+    private ServiceRegistration eventHandlerRegistration;
+
     public static final String SCRIPT_TYPE = "java";
 
     /**
@@ -124,7 +132,7 @@ public class JavaScriptEngineFactory extends AbstractScriptEngineFactory {
      * Activate this engine
      * @param componentContext
      */
-    protected void activate(ComponentContext componentContext) {
+    protected void activate(final ComponentContext componentContext) {
         this.ioProvider = new SlingIOProvider(this.classLoaderWriter);
         this.servletCache = new ServletCache();
 
@@ -135,20 +143,31 @@ public class JavaScriptEngineFactory extends AbstractScriptEngineFactory {
             componentContext.getProperties());
         this.compilerOptions = new Options(componentContext,
                                            this.javaClassLoader);
-        if (log.isDebugEnabled()) {
-            log.debug("JavaServletScriptEngine.activate()");
+        final Dictionary<String, String> props = new Hashtable<String, String>();
+        props.put("event.topics","org/apache/sling/api/resource/*");
+        props.put("service.description","Java Servlet Script Modification Handler");
+        props.put("service.vendor","The Apache Software Foundation");
+
+        this.eventHandlerRegistration = componentContext.getBundleContext()
+                  .registerService(EventHandler.class.getName(), this, props);
+        if (logger.isDebugEnabled()) {
+            logger.debug("JavaServletScriptEngine.activate()");
         }
     }
 
     /**
      * Deactivate this engine
-     * @param oldComponentContext
+     * @param componentContext
      */
-    protected void deactivate(ComponentContext oldComponentContext) {
-        if (log.isDebugEnabled()) {
-            log.debug("JavaServletScriptEngine.deactivate()");
+    protected void deactivate(final ComponentContext componentContext) {
+        if (logger.isDebugEnabled()) {
+            logger.debug("JavaServletScriptEngine.deactivate()");
         }
 
+        if ( this.eventHandlerRegistration != null ) {
+            this.eventHandlerRegistration.unregister();
+            this.eventHandlerRegistration = null;
+        }
         ioProvider = null;
         javaServletContext = null;
         servletConfig = null;
@@ -164,7 +183,6 @@ public class JavaScriptEngineFactory extends AbstractScriptEngineFactory {
      * @throws SlingServletException
      * @throws SlingIOException
      */
-    @SuppressWarnings("unchecked")
     private void callServlet(final Bindings bindings,
                              final SlingScriptHelper scriptHelper,
                              final ScriptContext context) {
@@ -185,8 +203,8 @@ public class JavaScriptEngineFactory extends AbstractScriptEngineFactory {
         }
     }
 
-    private ServletWrapper getWrapperAdapter(
-            SlingScriptHelper scriptHelper) throws SlingException {
+    private ServletWrapper getWrapperAdapter(final SlingScriptHelper scriptHelper)
+    throws SlingException {
 
         SlingScript script = scriptHelper.getScript();
         final String scriptName = script.getScriptResource().getPath();
@@ -247,6 +265,24 @@ public class JavaScriptEngineFactory extends AbstractScriptEngineFactory {
     }
     // ---------- Internal -----------------------------------------------------
 
+    /**
+     * @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event)
+     */
+    public void handleEvent(Event event) {
+        if ( SlingConstants.TOPIC_RESOURCE_CHANGED.equals(event.getTopic()) ) {
+            this.handleModification((String)event.getProperty(SlingConstants.PROPERTY_PATH));
+        } else if ( SlingConstants.TOPIC_RESOURCE_REMOVED.equals(event.getTopic()) ) {
+            this.handleModification((String)event.getProperty(SlingConstants.PROPERTY_PATH));
+        }
+    }
+
+    private void handleModification(final String scriptName) {
+        final ServletWrapper wrapper = this.servletCache.getWrapper(scriptName);
+        if ( wrapper != null ) {
+            wrapper.getCompilationContext().setLastModificationTest(0);
+        }
+    }
+
     private static class JavaScriptEngine extends AbstractSlingScriptEngine {
 
         JavaScriptEngine(JavaScriptEngineFactory factory) {
diff --git a/src/main/java/org/apache/sling/scripting/java/Options.java b/src/main/java/org/apache/sling/scripting/java/Options.java
index 375c348..4dea523 100644
--- a/src/main/java/org/apache/sling/scripting/java/Options.java
+++ b/src/main/java/org/apache/sling/scripting/java/Options.java
@@ -34,21 +34,12 @@ public class Options {
 
     private static final String PROPERTY_COMPILER_TARGET_V_M = "compilerTargetVM";
 
-    private static final String PROPERTY_DEVELOPMENT = "development";
-
-    private static final String PROPERTY_MODIFICATION_TEST_INTERVAL = "modificationTestInterval";
-
     private static final String PROPERTY_CLASSDEBUGINFO = "classdebuginfo";
 
     /** Default source and target VM version (value is "1.5"). */
     private static final String DEFAULT_VM_VERSION = "1.5";
 
     /**
-     * Is the engine being used in development mode?
-     */
-    private final boolean development;
-
-    /**
      * Do we want to include debugging information in the class file?
      */
     private final boolean classDebugInfo;
@@ -69,11 +60,6 @@ public class Options {
     private final String javaEncoding;
 
     /**
-     * Modification test interval.
-     */
-    private final int modificationTestInterval;
-
-    /**
      * Classloader
      */
     private final ClassLoader classLoader;
@@ -91,8 +77,6 @@ public class Options {
         final Properties properties = new Properties();
         // set default values first
         properties.put(PROPERTY_CLASSDEBUGINFO, "true");
-        properties.put(PROPERTY_DEVELOPMENT, "true");
-        properties.put(PROPERTY_MODIFICATION_TEST_INTERVAL, "-1");
         properties.put(PROPERTY_COMPILER_TARGET_V_M, DEFAULT_VM_VERSION);
         properties.put(PROPERTY_COMPILER_SOURCE_V_M, DEFAULT_VM_VERSION);
         properties.put(PROPERTY_JAVA_ENCODING, "UTF-8");
@@ -112,8 +96,6 @@ public class Options {
         }
 
         this.classDebugInfo = Boolean.valueOf(properties.get(PROPERTY_CLASSDEBUGINFO).toString());
-        this.modificationTestInterval = Integer.valueOf(properties.get(PROPERTY_MODIFICATION_TEST_INTERVAL).toString());
-        this.development = Boolean.valueOf(properties.get(PROPERTY_DEVELOPMENT).toString());
         this.compilerTargetVM = properties.get(PROPERTY_COMPILER_TARGET_V_M).toString();
         this.compilerSourceVM = properties.get(PROPERTY_COMPILER_SOURCE_V_M).toString();
         this.javaEncoding = properties.get(PROPERTY_JAVA_ENCODING).toString();
@@ -133,20 +115,6 @@ public class Options {
         return this.classDebugInfo;
     }
 
-    /**
-     * Modification test interval.
-     */
-    public int getModificationTestInterval() {
-        return this.modificationTestInterval;
-    }
-
-    /**
-     * Is the engine being used in development mode?
-     */
-    public boolean getDevelopment() {
-        return this.development;
-    }
-
     public ClassLoader getClassLoader() {
         return this.classLoader;
     }
diff --git a/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java b/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java
index 4d948f5..481d832 100644
--- a/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java
+++ b/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java
@@ -53,9 +53,6 @@ public class ServletWrapper {
     /** Reload flag. */
     private boolean reload = true;
 
-    /** Compiler options. */
-    private final Options options;
-
     private Servlet theServlet;
     private long available = 0L;
     private boolean firstTime = true;
@@ -72,7 +69,6 @@ public class ServletWrapper {
                           final ServletCache servletCache) {
         this.config = config;
         this.servletUri = servletPath;
-        this.options = options;
         this.ctxt = new CompilationContext(servletUri, options,
                 ioProvider, servletCache, this);
     }
@@ -85,6 +81,10 @@ public class ServletWrapper {
         this.reload = reload;
     }
 
+    public CompilationContext getCompilationContext() {
+        return this.ctxt;
+    }
+
     /**
      * Get the servlet.
      * @return The servlet.
@@ -189,7 +189,7 @@ public class ServletWrapper {
             /*
              * (1) Compile
              */
-            if (options.getDevelopment() || firstTime ) {
+            if (firstTime || ctxt.getLastModificationTest() == 0 ) {
                 synchronized (this) {
                     firstTime = false;
 
diff --git a/src/main/resources/OSGI-INF/metatype/metatype.properties b/src/main/resources/OSGI-INF/metatype/metatype.properties
index c6e2467..dad70ff 100644
--- a/src/main/resources/OSGI-INF/metatype/metatype.properties
+++ b/src/main/resources/OSGI-INF/metatype/metatype.properties
@@ -30,16 +30,6 @@ java.classdebuginfo.name = Generate Debug Info
 java.classdebuginfo.description = Should the class file be compiled with \
  debugging information? true or false, default true.
  
-java.modificationTestInterval.name = Modification Check Interval
-java.modificationTestInterval.description = Checks for modification for a \
- given Java file will be performed only once every \
- specified amount of seconds. Setting this to -1 will cause the source to be checked \
- on every access. Default is -1.
-
-java.development.name = Development Mode
-java.development.description = Is scripting used in development mode (will check \
- for modification on every access)? true or false, default true.
- 
 java.javaEncoding.name = Source Encoding
 java.javaEncoding.description = Encoding to be used to read the source files. \
  This defaults to UTF-8 and should only be changed in very specific circumstances.

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.