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

commit 7a3f0fa5a6b8e8909693191d08d7d34c6e3bdce6
Author: Carsten Ziegeler <cziege...@apache.org>
AuthorDate: Fri Mar 23 14:53:38 2012 +0000

    SLING-2447 : ClassLoaderWriter should provide class loader for loading 
written classes/resources
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/contrib/scripting/java@1304391 
13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  4 ++
 .../java/impl/JavaScriptEngineFactory.java         | 30 ++++++-----
 .../sling/scripting/java/impl/ServletWrapper.java  | 63 +++++++---------------
 3 files changed, 39 insertions(+), 58 deletions(-)

diff --git a/pom.xml b/pom.xml
index 20e9670..b3e9879 100644
--- a/pom.xml
+++ b/pom.xml
@@ -80,6 +80,10 @@
 
     <dependencies>
         <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
             <version>2.0.8</version>
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 3eacfdf..2c0c4cc 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
@@ -31,6 +31,11 @@ import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 
+import org.apache.felix.scr.annotations.Component;
+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.SlingConstants;
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingHttpServletRequest;
@@ -52,16 +57,17 @@ import org.osgi.service.event.EventHandler;
 /**
  * The Java engine
  *
- * @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 interface="javax.script.ScriptEngineFactory"
- *
- * @scr.property nameRef="PROPERTY_COMPILER_SOURCE_V_M" 
valueRef="DEFAULT_VM_VERSION"
- * @scr.property nameRef="PROPERTY_COMPILER_TARGET_V_M" 
valueRef="DEFAULT_VM_VERSION"
- * @scr.property nameRef="PROPERTY_CLASSDEBUGINFO" value="true" type="Boolean"
- * @scr.property nameRef="PROPERTY_ENCODING" value="UTF-8"
  */
+@Component(metatype=true, label="%javahandler.name", 
description="%javahandler.description")
+@Service(value=javax.script.ScriptEngineFactory.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.DEFAULT_VM_VERSION),
+    @Property(name=JavaScriptEngineFactory.PROPERTY_COMPILER_TARGET_V_M, 
value=JavaScriptEngineFactory.DEFAULT_VM_VERSION),
+    @Property(name=JavaScriptEngineFactory.PROPERTY_CLASSDEBUGINFO, 
boolValue=true),
+    @Property(name=JavaScriptEngineFactory.PROPERTY_ENCODING, value="UTF-8")
+})
 public class JavaScriptEngineFactory
     extends AbstractScriptEngineFactory
     implements EventHandler {
@@ -77,12 +83,10 @@ public class JavaScriptEngineFactory
     /** Default source and target VM version (value is "1.5"). */
     public static final String DEFAULT_VM_VERSION = "1.5";
 
-    /**
-     * @scr.reference
-     */
+    @Reference
     private JavaCompiler javaCompiler;
 
-    /** @scr.reference */
+    @Reference
     private ServletContext slingServletContext;
 
     private SlingIOProvider ioProvider;
diff --git 
a/src/main/java/org/apache/sling/scripting/java/impl/ServletWrapper.java 
b/src/main/java/org/apache/sling/scripting/java/impl/ServletWrapper.java
index 45b7f17..9a430e1 100644
--- a/src/main/java/org/apache/sling/scripting/java/impl/ServletWrapper.java
+++ b/src/main/java/org/apache/sling/scripting/java/impl/ServletWrapper.java
@@ -21,7 +21,6 @@ import java.lang.reflect.Array;
 import java.lang.reflect.Field;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
@@ -68,9 +67,6 @@ public class ServletWrapper {
     /** Flag for handling modifications. */
     private volatile long lastModificationTest = 0L;
 
-    /** The compiled class. */
-    private volatile Class<?> theServletClass;
-
     /**
      * The compiled and instantiated servlet. This field may be null in which 
case a new servlet
      * instance is created per request.
@@ -106,7 +102,6 @@ public class ServletWrapper {
     public void service(HttpServletRequest request,
                          HttpServletResponse response)
     throws Exception {
-        Servlet servlet = null;
         try {
             if ((available > 0L) && (available < Long.MAX_VALUE)) {
                 if (available > System.currentTimeMillis()) {
@@ -136,7 +131,7 @@ public class ServletWrapper {
                 throw compileException;
             }
 
-             servlet = getServlet();
+            final Servlet servlet = this.getServlet();
 
             // invoke the servlet
             if (servlet instanceof SingleThreadModel) {
@@ -149,7 +144,7 @@ public class ServletWrapper {
                 servlet.service(request, response);
             }
 
-        } catch (UnavailableException ex) {
+        } catch (final UnavailableException ex) {
             int unavailableSeconds = ex.getUnavailableSeconds();
             if (unavailableSeconds <= 0) {
                 unavailableSeconds = 60;        // Arbitrary default
@@ -160,10 +155,6 @@ public class ServletWrapper {
                 (HttpServletResponse.SC_SERVICE_UNAVAILABLE,
                  ex.getMessage());
             logger.error("Java servlet {} is unavailable.", this.sourcePath);
-        } finally {
-            if (servlet != null && theServlet == null) {
-                servlet.destroy();
-            }
         }
     }
 
@@ -189,8 +180,8 @@ public class ServletWrapper {
      * Check if the used classloader is still valid
      */
     private boolean checkReload() {
-        if ( theServletClass != null && theServletClass.getClassLoader() 
instanceof DynamicClassLoader ) {
-            return 
!((DynamicClassLoader)theServletClass.getClassLoader()).isLive();
+        if ( theServlet != null && theServlet.getClass().getClassLoader() 
instanceof DynamicClassLoader ) {
+            return 
!((DynamicClassLoader)theServlet.getClass().getClassLoader()).isLive();
         }
         return false;
     }
@@ -205,25 +196,17 @@ public class ServletWrapper {
         if (this.checkReload()) {
             synchronized (this) {
                 if (this.checkReload()) {
+                    logger.debug("Reloading {}", this.sourcePath);
                     this.compile();
                 }
             }
         }
 
-        if (theServlet == null && theServletClass != null) {
-            final Servlet servlet = (Servlet) theServletClass.newInstance();
-            servlet.init(this.config);
-
-            injectFields(servlet);
-
-            return servlet;
-        }
-
         return theServlet;
     }
 
-    private void injectFields(Servlet servlet) {
-        for (Field field : theServletClass.getDeclaredFields()) {
+    private void injectFields(final Servlet servlet) {
+        for (Field field : servlet.getClass().getDeclaredFields()) {
             if (field.isAnnotationPresent(Inject.class)) {
                 field.setAccessible(true);
                 try {
@@ -254,16 +237,16 @@ public class ServletWrapper {
                             logger.warn("Field {} of {} was not an injectable 
collection type.", field.getName(), sourcePath);
                             continue;
                         }
-                        
+
                         Class<?> serviceType = (Class<?>) 
ptype.getActualTypeArguments()[0];
                         Object[] services = 
scriptHelper.getServices(serviceType, null);
                         field.set(servlet, Arrays.asList(services));
                     } else {
                         logger.warn("Field {} of {} was not an injectable 
type.", field.getName(), sourcePath);
                     }
-                } catch (IllegalArgumentException e) {
+                } catch (final IllegalArgumentException e) {
                     logger.error(String.format("Unable to inject into field %s 
of %s.", field.getName(), sourcePath), e);
-                } catch (IllegalAccessException e) {
+                } catch (final IllegalAccessException e) {
                     logger.error(String.format("Unable to inject into field %s 
of %s.", field.getName(), sourcePath), e);
                 } finally {
                     field.setAccessible(false);
@@ -288,19 +271,18 @@ public class ServletWrapper {
                     opts);
 
             final List<CompilerMessage> errors = result.getErrors();
+            this.destroy();
             if ( errors != null && errors.size() > 0 ) {
                 throw CompilerException.create(errors, this.sourcePath);
             }
-            if ( result.didCompile() || this.theServletClass == null ) {
-                destroy();
-                this.theServletClass = 
result.loadCompiledClass(this.className);
-            }
 
-            if ( !hasInjectedFields(this.theServletClass) ) {
-                final Servlet servlet = (Servlet) 
theServletClass.newInstance();
-                servlet.init(this.config);
-                this.theServlet = servlet;
-            }
+            final Servlet servlet = (Servlet) 
result.loadCompiledClass(this.className).newInstance();
+
+            servlet.init(this.config);
+            this.injectFields(servlet);
+
+            this.theServlet = servlet;
+
         } catch (final Exception ex) {
             // store exception for futher access attempts
             this.compileException = ex;
@@ -310,15 +292,6 @@ public class ServletWrapper {
         }
     }
 
-    private static boolean hasInjectedFields(Class<?> clazz) {
-        for (Field field : clazz.getDeclaredFields()) {
-            if (field.isAnnotationPresent(Inject.class)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
     /** Compiler exception .*/
     protected final static class CompilerException extends ServletException {
 

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

Reply via email to