incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1404950 - in /sling/trunk/contrib/commons/fsclassloader: ./ src/main/java/org/apache/sling/commons/fsclassloader/impl/ src/main/resources/
Date Fri, 02 Nov 2012 12:34:05 GMT
Author: cziegeler
Date: Fri Nov  2 12:34:04 2012
New Revision: 1404950

URL: http://svn.apache.org/viewvc?rev=1404950&view=rev
Log:
Implement latest class loader writer

Removed:
    sling/trunk/contrib/commons/fsclassloader/LICENSE
    sling/trunk/contrib/commons/fsclassloader/NOTICE
    sling/trunk/contrib/commons/fsclassloader/src/main/resources/
Modified:
    sling/trunk/contrib/commons/fsclassloader/pom.xml
    sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSClassLoaderProvider.java
    sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSDynamicClassLoader.java

Modified: sling/trunk/contrib/commons/fsclassloader/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/commons/fsclassloader/pom.xml?rev=1404950&r1=1404949&r2=1404950&view=diff
==============================================================================
--- sling/trunk/contrib/commons/fsclassloader/pom.xml (original)
+++ sling/trunk/contrib/commons/fsclassloader/pom.xml Fri Nov  2 12:34:04 2012
@@ -88,7 +88,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.classloader</artifactId>
-            <version>1.2.0</version>
+            <version>1.3.0</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Modified: sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSClassLoaderProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSClassLoaderProvider.java?rev=1404950&r1=1404949&r2=1404950&view=diff
==============================================================================
--- sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSClassLoaderProvider.java
(original)
+++ sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSClassLoaderProvider.java
Fri Nov  2 12:34:04 2012
@@ -27,71 +27,109 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.List;
 
 import org.apache.felix.scr.annotations.Component;
 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.commons.classloader.ClassLoaderWriter;
-import org.apache.sling.commons.classloader.DynamicClassLoaderProvider;
+import org.apache.sling.commons.classloader.DynamicClassLoader;
+import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
 import org.osgi.service.component.ComponentContext;
 
 /**
- * The <code>FSClassLoaderProvider</code> is a dynamic class loader privder
+ * The <code>FSClassLoaderProvider</code> is a dynamic class loader provider
  * which uses the file system to store and read class files from.
  *
  */
 @Component
-@Service
-@Property( name="service.vendor", value="The Apache Software Foundation")
+@Service(value={ClassLoaderWriter.class})
+@Property( name="service.ranking", intValue=100)
 public class FSClassLoaderProvider
-    implements DynamicClassLoaderProvider, ClassLoaderWriter {
+    implements ClassLoaderWriter {
 
     /** File root */
     private File root;
 
-    /** All classloaders */
-    private List<FSDynamicClassLoader> loaders = new ArrayList<FSDynamicClassLoader>();
+    /** File root URL */
+    private URL rootURL;
+
+    /** Current class loader */
+    private FSDynamicClassLoader loader;
+
+    @Reference
+    private DynamicClassLoaderManager dynamicClassLoaderManager;
+
+    private ClassLoader dynamicClassLoader;
 
     /**
-     * @see org.apache.sling.commons.classloader.DynamicClassLoaderProvider#getClassLoader(ClassLoader)
+     * Bind the class load provider.
+     *
+     * @param repositoryClassLoaderProvider the new provider
      */
-    public ClassLoader getClassLoader(final ClassLoader parent) {
-        try {
-            final FSDynamicClassLoader cl = new FSDynamicClassLoader(new URL[] {this.root.toURL()},
parent);
-            synchronized ( this.loaders ) {
-                this.loaders.add(cl);
-            }
-            return cl;
-        } catch (MalformedURLException e) {
-            // this should never happen, but who knows
-            throw new RuntimeException(e);
+    protected void bindDynamicClassLoaderManager(final DynamicClassLoaderManager rclp) {
+        if ( this.dynamicClassLoader != null ) {
+            this.ungetClassLoader();
         }
+        this.getClassLoader(rclp);
     }
 
     /**
-     * @see org.apache.sling.commons.classloader.DynamicClassLoaderProvider#release(java.lang.ClassLoader)
+     * Unbind the class loader provider.
+     * @param repositoryClassLoaderProvider the old provider
      */
-    public void release(final ClassLoader classLoader) {
-        synchronized ( this.loaders ) {
-            this.loaders.remove(classLoader);
+    protected void unbindDynamicClassLoaderManager(final DynamicClassLoaderManager rclp)
{
+        if ( this.dynamicClassLoaderManager == rclp ) {
+            this.ungetClassLoader();
         }
     }
 
-    private void checkClassLoaders(final String filePath) {
-        if ( filePath.endsWith(".class") ) {
-            // remove store directory and .class
-            final String path = filePath.substring(this.root.getAbsolutePath().length() +
1, filePath.length() - 6);
-            // convert to a class name
-            final String className = path.replace(File.separatorChar, '.');
-            synchronized ( this.loaders ) {
-                for(final FSDynamicClassLoader cl : this.loaders ) {
-                    cl.check(className);
-                }
+    /**
+     * Get the class loader
+     */
+    private void getClassLoader(final DynamicClassLoaderManager rclp) {
+        this.dynamicClassLoaderManager = rclp;
+        this.dynamicClassLoader = rclp.getDynamicClassLoader();
+    }
+
+    /**
+     * Unget the class loader
+     */
+    private void ungetClassLoader() {
+        this.dynamicClassLoader = null;
+        this.dynamicClassLoaderManager = null;
+    }
+    /**
+     * @see org.apache.sling.commons.classloader.ClassLoaderWriter#getClassLoader()
+     */
+    public ClassLoader getClassLoader() {
+        synchronized ( this ) {
+            // first check parent
+            boolean recreate = loader == null;
+            if ( (this.dynamicClassLoader instanceof DynamicClassLoader) && !((DynamicClassLoader)this.dynamicClassLoader).isLive())
{
+                this.dynamicClassLoader = this.dynamicClassLoaderManager.getDynamicClassLoader();
+                recreate = true;
+            }
+            if ( recreate || !loader.isLive() ) {
+                loader = new FSDynamicClassLoader(new URL[] {this.rootURL}, this.dynamicClassLoader);
             }
+            return this.loader;
         }
     }
+
+    private void checkClassLoader(final String filePath) {
+        synchronized ( this ) {
+            final FSDynamicClassLoader currentLoader = this.loader;
+            if ( currentLoader != null && filePath.endsWith(".class") ) {
+                // remove store directory and .class
+                final String path = filePath.substring(this.root.getAbsolutePath().length()
+ 1, filePath.length() - 6);
+                // convert to a class name
+                final String className = path.replace(File.separatorChar, '.');
+                currentLoader.check(className);
+            }
+        }
+    }
+
     //---------- SCR Integration ----------------------------------------------
 
     /**
@@ -103,7 +141,7 @@ public class FSClassLoaderProvider
         if ( file.exists() ) {
             final boolean result = file.delete();
             if ( result ) {
-                this.checkClassLoaders(file.getAbsolutePath());
+                this.checkClassLoader(file.getAbsolutePath());
             }
         }
         // file does not exist so we return false
@@ -122,7 +160,7 @@ public class FSClassLoaderProvider
         }
         try {
             if ( file.exists() ) {
-                this.checkClassLoaders(path);
+                this.checkClassLoader(path);
             }
             return new FileOutputStream(path);
         } catch (FileNotFoundException e) {
@@ -139,7 +177,8 @@ public class FSClassLoaderProvider
         final File old = new File(oldPath);
         final boolean result = old.renameTo(new File(newPath));
         if ( result ) {
-            this.checkClassLoaders(oldPath);
+            this.checkClassLoader(oldPath);
+            this.checkClassLoader(newPath);
         }
         return result;
     }
@@ -192,11 +231,13 @@ public class FSClassLoaderProvider
      * Activate this component.
      * Create the root directory.
      * @param componentContext
+     * @throws MalformedURLException
      */
-    protected void activate(final ComponentContext componentContext) {
+    protected void activate(final ComponentContext componentContext) throws MalformedURLException
{
         // get the file root
         this.root = new File(componentContext.getBundleContext().getDataFile(""), "classes");
         this.root.mkdirs();
+        this.rootURL = this.root.toURI().toURL();
     }
 
     /**
@@ -206,5 +247,6 @@ public class FSClassLoaderProvider
      */
     protected void deactivate(final ComponentContext componentContext) {
         this.root = null;
+        this.rootURL = null;
     }
 }

Modified: sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSDynamicClassLoader.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSDynamicClassLoader.java?rev=1404950&r1=1404949&r2=1404950&view=diff
==============================================================================
--- sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSDynamicClassLoader.java
(original)
+++ sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSDynamicClassLoader.java
Fri Nov  2 12:34:04 2012
@@ -49,6 +49,7 @@ public class FSDynamicClassLoader
     /**
      * @see java.lang.ClassLoader#loadClass(java.lang.String)
      */
+    @Override
     public Class<?> loadClass(final String name) throws ClassNotFoundException {
         try {
             final Class<?> c = super.loadClass(name);
@@ -61,6 +62,8 @@ public class FSDynamicClassLoader
     }
 
     public void check(final String className) {
-       this.isDirty = hit.contains(className) || miss.contains(className);
+        if ( !this.isDirty ) {
+            this.isDirty = hit.contains(className) || miss.contains(className);
+        }
     }
 }



Mime
View raw message