incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r794564 - in /sling/trunk/contrib/jcr/compiler: pom.xml src/main/java/org/apache/sling/jcr/compiler/impl/JcrJavaCompilerImpl.java
Date Thu, 16 Jul 2009 07:27:34 GMT
Author: cziegeler
Date: Thu Jul 16 07:27:34 2009
New Revision: 794564

URL: http://svn.apache.org/viewvc?rev=794564&view=rev
Log:
Replace use of dynamic import package * with new commons class loader. Fix possible multi
threading problems.

Modified:
    sling/trunk/contrib/jcr/compiler/pom.xml
    sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/impl/JcrJavaCompilerImpl.java

Modified: sling/trunk/contrib/jcr/compiler/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/jcr/compiler/pom.xml?rev=794564&r1=794563&r2=794564&view=diff
==============================================================================
--- sling/trunk/contrib/jcr/compiler/pom.xml (original)
+++ sling/trunk/contrib/jcr/compiler/pom.xml Thu Jul 16 07:27:34 2009
@@ -62,7 +62,6 @@
                         <Private-Package>
                             org.apache.sling.jcr.compiler.impl
                         </Private-Package>
-                        <DynamicImport-Package>*</DynamicImport-Package>
                     </instructions>
                 </configuration>
             </plugin>
@@ -77,8 +76,8 @@
 	    </dependency>
 	    <dependency>
 	        <groupId>org.apache.sling</groupId>
-	        <artifactId>org.apache.sling.jcr.classloader</artifactId>
-	        <version>2.0.2-incubator</version>
+	        <artifactId>org.apache.sling.commons.classloader</artifactId>
+	        <version>0.9.0-SNAPSHOT</version>
 	    </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>

Modified: sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/impl/JcrJavaCompilerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/impl/JcrJavaCompilerImpl.java?rev=794564&r1=794563&r2=794564&view=diff
==============================================================================
--- sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/impl/JcrJavaCompilerImpl.java
(original)
+++ sling/trunk/contrib/jcr/compiler/src/main/java/org/apache/sling/jcr/compiler/impl/JcrJavaCompilerImpl.java
Thu Jul 16 07:27:34 2009
@@ -29,6 +29,7 @@
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
 import org.apache.sling.commons.compiler.ClassWriter;
 import org.apache.sling.commons.compiler.CompileUnit;
 import org.apache.sling.commons.compiler.CompilerEnvironment;
@@ -36,42 +37,36 @@
 import org.apache.sling.commons.compiler.JavaCompiler;
 import org.apache.sling.commons.compiler.Options;
 import org.apache.sling.jcr.api.SlingRepository;
-import org.apache.sling.jcr.classloader.RepositoryClassLoaderProvider;
 import org.apache.sling.jcr.compiler.JcrJavaCompiler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * <code>JcrJavaCompilerImpl</code> ...
- * 
+ *
  * @scr.component metatype="no"
  * @scr.service interface="org.apache.sling.jcr.compiler.JcrJavaCompiler"
  */
 public class JcrJavaCompilerImpl implements JcrJavaCompiler, CompilerEnvironment {
 
     /** Logger instance */
-    private static final Logger log = LoggerFactory.getLogger(JcrJavaCompilerImpl.class);
-
-    private static final String CLASSLOADER_NAME = "admin";
+    private final Logger log = LoggerFactory.getLogger(JcrJavaCompilerImpl.class);
 
     /** @scr.reference */
     protected JavaCompiler compiler;
-    
+
     /** @scr.reference */
-    protected RepositoryClassLoaderProvider clp;
+    protected DynamicClassLoaderManager dynamicClassLoaderManager;
+
+    /** The class loader used to compile the classes. */
+    protected ClassLoader javaClassLoader;
 
     /** @scr.reference */
-    protected SlingRepository rep;
-    
-    // only accessed locally within scope of compile() method
-    private ClassLoader cl;
+    protected SlingRepository repository;
 
-    // only accessed locally within scope of compile() method
+    /** The session. */
     private Session session;
 
-    public JcrJavaCompilerImpl() {
-    }
-
     //------------------------------------------------------< JcrJavaCompiler >
     /* (non-Javadoc)
      * @see org.apache.sling.jcr.compiler.JcrJavaCompiler#compile(java.lang.String[], java.lang.String,
org.apache.sling.commons.compiler.ErrorHandler, boolean, java.lang.String)
@@ -79,7 +74,7 @@
     public boolean compile(String[] srcFiles, String outputDir,
             ErrorHandler errorHandler, boolean generateDebug, String javaVersion)
             throws Exception {
-        
+
         if (javaVersion == null) {
             javaVersion = System.getProperty("java.specification.version");
         }
@@ -87,61 +82,49 @@
         if (outputDir == null) {
             throw new Exception("outputDir can not be null");
         }
-        
+
         ErrorHandlerWrapper handler = new ErrorHandlerWrapper(errorHandler);
 
-        cl = clp.getClassLoader(CLASSLOADER_NAME);
-        try {
-            session = rep.loginAdministrative(null);
-            
-            ClassWriter classWriter;
-            if (outputDir.startsWith("file://")) {
-                // write class files to local file system;
-                // only subdirectories of the system temp dir 
-                // will be accepted
-                File tempDir = new File(System.getProperty("java.io.tmpdir"));
-                File outDir = new File(outputDir.substring("file://".length()));
-                if (!outDir.isAbsolute()) {
-                    outDir = new File(tempDir, outputDir.substring("file://".length()));
-                } 
-                if (!outDir.getCanonicalPath().startsWith(tempDir.getCanonicalPath())) {
-                    throw new Exception("illegal outputDir (not a temp dir): " + outputDir);
-                }
-                outDir.mkdir();
-                classWriter = new FileClassWriter(outDir);
-            } else {
-                // write class files to the repository  (default)
-                if (!session.itemExists(outputDir)) {
-                    throw new Exception("outputDir does not exist: " + outputDir);
-                }
-                
-                Item item = session.getItem(outputDir);
-                if (item.isNode()) {
-                    Node folder = (Node) item;
-                    if (!folder.isNodeType("nt:folder")) {
-                        throw new Exception("outputDir must be a node of type nt:folder");
-                    }
-                    classWriter = new JcrClassWriter(folder);
-                } else {
+        ClassWriter classWriter;
+        if (outputDir.startsWith("file://")) {
+            // write class files to local file system;
+            // only subdirectories of the system temp dir
+            // will be accepted
+            File tempDir = new File(System.getProperty("java.io.tmpdir"));
+            File outDir = new File(outputDir.substring("file://".length()));
+            if (!outDir.isAbsolute()) {
+                outDir = new File(tempDir, outputDir.substring("file://".length()));
+            }
+            if (!outDir.getCanonicalPath().startsWith(tempDir.getCanonicalPath())) {
+                throw new Exception("illegal outputDir (not a temp dir): " + outputDir);
+            }
+            outDir.mkdir();
+            classWriter = new FileClassWriter(outDir);
+        } else {
+            // write class files to the repository  (default)
+            if (!session.itemExists(outputDir)) {
+                throw new Exception("outputDir does not exist: " + outputDir);
+            }
+
+            Item item = session.getItem(outputDir);
+            if (item.isNode()) {
+                Node folder = (Node) item;
+                if (!folder.isNodeType("nt:folder")) {
                     throw new Exception("outputDir must be a node of type nt:folder");
                 }
+                classWriter = new JcrClassWriter(folder);
+            } else {
+                throw new Exception("outputDir must be a node of type nt:folder");
             }
+        }
 
-            CompileUnit[] units = new CompileUnit[srcFiles.length];
-            for (int i = 0; i < units.length; i++) {
-                units[i] = createCompileUnit(srcFiles[i]);
-            }
-            compiler.compile(units, this, classWriter, handler, 
-                    new Options(javaVersion, generateDebug));
-        } finally {
-            // cleanup
-            clp.ungetClassLoader(cl);
-            if (session != null) {
-                session.logout();
-                session = null;
-            }
+        CompileUnit[] units = new CompileUnit[srcFiles.length];
+        for (int i = 0; i < units.length; i++) {
+            units[i] = createCompileUnit(srcFiles[i]);
         }
-        
+        compiler.compile(units, this, classWriter, handler,
+                new Options(javaVersion, generateDebug));
+
         return handler.getNumErrors() == 0;
     }
 
@@ -150,7 +133,7 @@
      * @see org.apache.sling.commons.compiler.CompilerEnvironment#findClass(java.lang.String)
      */
     public byte[] findClass(String className) throws Exception {
-        InputStream in = cl.getResourceAsStream(className.replace('.', '/') + ".class");
+        InputStream in = this.javaClassLoader.getResourceAsStream(className.replace('.',
'/') + ".class");
         if (in == null) {
             return null;
         }
@@ -174,7 +157,7 @@
      * @see org.apache.sling.commons.compiler.CompilerEnvironment#isPackage(java.lang.String)
      */
     public boolean isPackage(String packageName) {
-        InputStream in = cl.getResourceAsStream(packageName.replace('.', '/') + ".class");
+        InputStream in = this.javaClassLoader.getResourceAsStream(packageName.replace('.',
'/') + ".class");
         if (in != null) {
             try {
                 in.close();
@@ -182,7 +165,7 @@
             }
             return false;
         }
-        // FIXME need a better way to determine whether a name resolves 
+        // FIXME need a better way to determine whether a name resolves
         // to a package
         int pos = packageName.lastIndexOf('.');
         if (pos != -1) {
@@ -228,9 +211,9 @@
         };
     }
 
-    private char[] readTextResource(String resourcePath) 
+    private char[] readTextResource(String resourcePath)
             throws RepositoryException, IOException {
-        String relPropPath = resourcePath.substring(1) 
+        String relPropPath = resourcePath.substring(1)
                 + "/jcr:content/jcr:data";
         InputStream in = session.getRootNode().getProperty(relPropPath).getStream();
         Reader reader = new InputStreamReader(in);
@@ -282,4 +265,64 @@
             handler.onWarning(msg, sourceFile, line, position);
         }
     }
+
+    /**
+     * Bind the class load provider.
+     * @param repositoryClassLoaderProvider the new provider
+     */
+    protected void bindDynamicClassLoaderManager(DynamicClassLoaderManager rclp) {
+        if ( this.javaClassLoader != null ) {
+            this.ungetClassLoader();
+        }
+        this.getClassLoader(rclp);
+    }
+
+    /**
+     * Unbind the class loader provider.
+     * @param repositoryClassLoaderProvider the old provider
+     */
+    protected void unbindDynamicClassLoaderManager(DynamicClassLoaderManager rclp) {
+        if ( this.dynamicClassLoaderManager == rclp ) {
+            this.ungetClassLoader();
+        }
+    }
+
+    /**
+     * Get the class loader
+     */
+    private void getClassLoader(DynamicClassLoaderManager rclp) {
+        this.dynamicClassLoaderManager = rclp;
+        this.javaClassLoader = rclp.getDynamicClassLoader();
+    }
+
+    /**
+     * Unget the class loader
+     */
+    private void ungetClassLoader() {
+        this.dynamicClassLoaderManager = null;
+        this.javaClassLoader = null;
+    }
+
+    protected void bindRepository(final SlingRepository repo) {
+        if ( this.session != null ) {
+            this.session.logout();
+            this.session = null;
+        }
+        try {
+            this.session = repo.loginAdministrative(null);
+        } catch (RepositoryException re) {
+            log.error("Unable to open admin session.", re);
+        }
+        this.repository = repo;
+    }
+
+    protected void unbindRepository(final SlingRepository repo) {
+        if ( this.repository == repo ) {
+            if ( this.session != null ) {
+                this.session.logout();
+                this.session = null;
+            }
+            this.repository = null;
+        }
+    }
 }



Mime
View raw message