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:06:04 UTC

[sling-org-apache-sling-scripting-java] 03/05: SLING-6289 : Improve java resource change listener to use globs

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.1.2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-java.git

commit cb2093892549283ca7e24aca95fb372b14865192
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Nov 15 18:08:18 2016 +0000

    SLING-6289 : Improve java resource change listener to use globs
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/scripting/java@1769867 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  4 --
 .../sling/scripting/java/impl/CompilerOptions.java | 21 ++-----
 .../java/impl/JavaScriptEngineFactory.java         | 69 +++++++++++++---------
 .../scripting/java/impl/JavaServletConfig.java     | 16 +----
 .../OSGI-INF/metatype/metatype.properties          | 45 --------------
 5 files changed, 48 insertions(+), 107 deletions(-)

diff --git a/pom.xml b/pom.xml
index 3e9cf7b..e3c1398 100644
--- a/pom.xml
+++ b/pom.xml
@@ -45,10 +45,6 @@
     <plugins>
       <plugin>
         <groupId>org.apache.felix</groupId>
-        <artifactId>maven-scr-plugin</artifactId>
-      </plugin>
-      <plugin>
-        <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
         <extensions>true</extensions>
         <configuration>
diff --git a/src/main/java/org/apache/sling/scripting/java/impl/CompilerOptions.java b/src/main/java/org/apache/sling/scripting/java/impl/CompilerOptions.java
index 795d86d..86d8776 100644
--- a/src/main/java/org/apache/sling/scripting/java/impl/CompilerOptions.java
+++ b/src/main/java/org/apache/sling/scripting/java/impl/CompilerOptions.java
@@ -16,10 +16,7 @@
  */
 package org.apache.sling.scripting.java.impl;
 
-import java.util.Map;
-
 import org.apache.sling.commons.compiler.Options;
-import org.apache.sling.commons.osgi.PropertiesUtil;
 
 public class CompilerOptions extends Options {
 
@@ -31,31 +28,23 @@ public class CompilerOptions extends Options {
      * Create an compiler options object using data available from
      * the component configuration.
      */
-    public static CompilerOptions createOptions(final Map<String, Object> props) {
+    public static CompilerOptions createOptions(final JavaScriptEngineFactory.Config config) {
         final String currentVersion = System.getProperty("java.specification.version");
         final CompilerOptions opts = new CompilerOptions();
 
-        final Boolean classDebugInfo = PropertiesUtil.toBoolean(
-                props.get(JavaScriptEngineFactory.PROPERTY_CLASSDEBUGINFO), true);
-        opts.put(Options.KEY_GENERATE_DEBUG_INFO, classDebugInfo);
+        opts.put(Options.KEY_GENERATE_DEBUG_INFO, config.java_classdebuginfo());
 
-        final String sourceVM = PropertiesUtil.toString(
-                props.get(JavaScriptEngineFactory.PROPERTY_COMPILER_SOURCE_V_M), null);
-        opts.put(Options.KEY_SOURCE_VERSION, sourceVM != null && sourceVM.trim().length() > 0 ? sourceVM.trim() : JavaScriptEngineFactory.VERSION_AUTO);
+        opts.put(Options.KEY_SOURCE_VERSION, config.java_compilerSourceVM());
         if ( JavaScriptEngineFactory.VERSION_AUTO.equalsIgnoreCase((String)opts.get(Options.KEY_SOURCE_VERSION)) ) {
             opts.put(Options.KEY_SOURCE_VERSION, currentVersion);
         }
 
-        final String targetVM = PropertiesUtil.toString(
-                props.get(JavaScriptEngineFactory.PROPERTY_COMPILER_TARGET_V_M), null);
-        opts.put(Options.KEY_TARGET_VERSION, targetVM != null && targetVM.trim().length() > 0 ? targetVM.trim() : JavaScriptEngineFactory.VERSION_AUTO);
+        opts.put(Options.KEY_TARGET_VERSION, config.java_compilerTargetVM());
         if ( JavaScriptEngineFactory.VERSION_AUTO.equalsIgnoreCase((String)opts.get(Options.KEY_TARGET_VERSION)) ) {
             opts.put(Options.KEY_TARGET_VERSION, currentVersion);
         }
 
-        final String encoding = PropertiesUtil.toString(
-                props.get(JavaScriptEngineFactory.PROPERTY_ENCODING), null);
-        opts.encoding = encoding != null && encoding.length() > 0 ? encoding : "UTF-8";
+        opts.encoding = config.java_javaEncoding();
 
         opts.put(Options.KEY_IGNORE_WARNINGS, true);
 
diff --git a/src/main/java/org/apache/sling/scripting/java/impl/JavaScriptEngineFactory.java b/src/main/java/org/apache/sling/scripting/java/impl/JavaScriptEngineFactory.java
index 6849cec..9bbc3e0 100644
--- a/src/main/java/org/apache/sling/scripting/java/impl/JavaScriptEngineFactory.java
+++ b/src/main/java/org/apache/sling/scripting/java/impl/JavaScriptEngineFactory.java
@@ -20,7 +20,6 @@ import static org.apache.sling.api.scripting.SlingBindings.SLING;
 
 import java.io.IOException;
 import java.io.Reader;
-import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
 
@@ -32,13 +31,6 @@ import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Properties;
-import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingIOException;
@@ -55,6 +47,14 @@ import org.apache.sling.api.scripting.SlingScriptHelper;
 import org.apache.sling.commons.compiler.JavaCompiler;
 import org.apache.sling.scripting.api.AbstractScriptEngineFactory;
 import org.apache.sling.scripting.api.AbstractSlingScriptEngine;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -62,31 +62,44 @@ import org.slf4j.LoggerFactory;
  * The Java engine
  *
  */
-@Component(metatype=true, label="%javahandler.name", description="%javahandler.description")
-@Service(value={javax.script.ScriptEngineFactory.class, ResourceChangeListener.class})
-@Properties({
-    @Property(name="service.vendor", value="The Apache Software Foundation"),
-    @Property(name="service.description", value="Java Servlet Script Handler"),
-    @Property(name=JavaScriptEngineFactory.PROPERTY_COMPILER_SOURCE_V_M, value=JavaScriptEngineFactory.VERSION_AUTO),
-    @Property(name=JavaScriptEngineFactory.PROPERTY_COMPILER_TARGET_V_M, value=JavaScriptEngineFactory.VERSION_AUTO),
-    @Property(name=JavaScriptEngineFactory.PROPERTY_CLASSDEBUGINFO, boolValue=true),
-    @Property(name=JavaScriptEngineFactory.PROPERTY_ENCODING, value="UTF-8"),
-    @Property(name = ResourceChangeListener.CHANGES, value = {"CHANGED", "REMOVED"}, propertyPrivate = true),
-    @Property(name = ResourceChangeListener.PATHS, value = {"."}, propertyPrivate = true)
-})
+@Component(service={javax.script.ScriptEngineFactory.class, ResourceChangeListener.class},
+           property={
+                   Constants.SERVICE_VENDOR + "=The Apache Software Foundation",
+                   Constants.SERVICE_DESCRIPTION + "=" + JavaScriptEngineFactory.DESCRIPTION,
+                   ResourceChangeListener.CHANGES + "=CHANGED",
+                   ResourceChangeListener.CHANGES + "=REMOVED",
+                   ResourceChangeListener.PATHS + "=glob:**/*.java"
+           })
+@Designate(ocd = JavaScriptEngineFactory.Config.class)
 public class JavaScriptEngineFactory
     extends AbstractScriptEngineFactory
     implements ResourceChangeListener, ExternalResourceChangeListener {
 
-    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+    public static final String DESCRIPTION = "Java Servlet Script Handler";
+
+    @ObjectClassDefinition(name = "Apache Sling Java Script Handler",
+           description = "The Java Script Handler supports development of Java Servlets to render response content. ")
+
+    public @interface Config {
 
-    public static final String PROPERTY_COMPILER_SOURCE_V_M = "java.compilerSourceVM";
+        @AttributeDefinition(name = "Generate Debug Info", description = "Should the class file be compiled with " +
+                   "debugging information? true or false, default true.")
+        boolean java_classdebuginfo() default true;
 
-    public static final String PROPERTY_COMPILER_TARGET_V_M = "java.compilerTargetVM";
+        @AttributeDefinition(name = "Source Encoding", description = "")
+        String java_javaEncoding() default "UTF-8";
 
-    public static final String PROPERTY_CLASSDEBUGINFO = "java.classdebuginfo";
+        @AttributeDefinition(name = "Source VM", description = "Java Specification to be used to read " +
+                 "the source files. If left empty or the value \"auto\" is specified, the " +
+                 "current vm version will be used.")
+        String java_compilerSourceVM() default JavaScriptEngineFactory.VERSION_AUTO;
 
-    public static final String PROPERTY_ENCODING = "java.javaEncoding";
+        @AttributeDefinition(name = "Target VM", description = "Target Java version for compilation. If left " +
+                   "empty or the value \"auto\" is specified, the current vm version will be used.")
+        String java_compilerTargetVM() default JavaScriptEngineFactory.VERSION_AUTO;
+    }
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
     public static final String VERSION_AUTO = "auto";
 
@@ -153,13 +166,13 @@ public class JavaScriptEngineFactory
      * @param config Configuration properties
      */
     @Activate
-    protected void activate(final Map<String, Object> config) {
-        final CompilerOptions opts = CompilerOptions.createOptions(new Hashtable<>(config));
+    protected void activate(final Config config, final Map<String, Object> props) {
+        final CompilerOptions opts = CompilerOptions.createOptions(config);
         this.ioProvider = new SlingIOProvider(this.javaCompiler, opts);
         this.javaServletContext = new JavaServletContext(ioProvider,
             slingServletContext);
 
-        this.servletConfig = new JavaServletConfig(javaServletContext, config);
+        this.servletConfig = new JavaServletConfig(javaServletContext, props);
 
         logger.info("Activating Apache Sling Script Engine for Java with options {}", opts);
     }
diff --git a/src/main/java/org/apache/sling/scripting/java/impl/JavaServletConfig.java b/src/main/java/org/apache/sling/scripting/java/impl/JavaServletConfig.java
index 10ca0c4..6a3a172 100644
--- a/src/main/java/org/apache/sling/scripting/java/impl/JavaServletConfig.java
+++ b/src/main/java/org/apache/sling/scripting/java/impl/JavaServletConfig.java
@@ -26,8 +26,6 @@ import java.util.Map;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 
-import org.osgi.framework.Constants;
-
 /**
  * The <code>JavaServletConfig</code>
  * is passed to the compiled servlets.
@@ -37,21 +35,11 @@ public class JavaServletConfig implements ServletConfig {
     /** The servlet context. */
     private final ServletContext servletContext;
 
-    /** The name of the servlet. */
-    private final String servletName;
-
     private final Map<String, String> initParams;
 
-    public JavaServletConfig(ServletContext servletContext, Map<String, Object> config) {
+    public JavaServletConfig(ServletContext servletContext, final Map<String, Object> config) {
         this.servletContext = servletContext;
 
-        // set the servlet name
-        if (config.get(Constants.SERVICE_DESCRIPTION) == null) {
-            servletName = "Java Script Handler";
-        } else{
-            servletName = config.get(Constants.SERVICE_DESCRIPTION).toString();
-        }
-
         // copy the "java." properties
         initParams = new HashMap<String, String>();
         for (final Map.Entry<String, Object> entry : config.entrySet()) {
@@ -95,6 +83,6 @@ public class JavaServletConfig implements ServletConfig {
      */
     @Override
     public String getServletName() {
-        return servletName;
+        return JavaScriptEngineFactory.DESCRIPTION;
     }
 }
\ No newline at end of file
diff --git a/src/main/resources/OSGI-INF/metatype/metatype.properties b/src/main/resources/OSGI-INF/metatype/metatype.properties
deleted file mode 100644
index b2d6623..0000000
--- a/src/main/resources/OSGI-INF/metatype/metatype.properties
+++ /dev/null
@@ -1,45 +0,0 @@
-#
-#  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.
-#
-
-#
-# This file contains localization strings for configuration labels and
-# descriptions as used in the metatype.xml descriptor generated by the
-# the Sling SCR plugin
-
-javahandler.name = Apache Sling Java Script Handler
-javahandler.description The Java Script Handler supports development of Java Servlets \
- to render response content.  
-
-java.classdebuginfo.name = Generate Debug Info
-java.classdebuginfo.description = Should the class file be compiled with \
- debugging information? 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.
-
-java.compilerSourceVM.name = Source VM
-java.compilerSourceVM.description = Java Specification to be used to read \
- the source files. If left empty or the value "auto" is specified, the \
- current vm version will be used.
-
-java.compilerTargetVM.name = Target VM
-java.compilerTargetVM.description = Target Java version for compilation. If left \
- empty or the value "auto" is specified, the current vm version will be used.
- 
\ No newline at end of file

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