incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r991822 - in /sling/trunk/contrib/commons/fsclassloader: ./ src/main/java/org/apache/sling/commons/fsclassloader/impl/ src/main/resources/META-INF/
Date Thu, 02 Sep 2010 06:31:01 GMT
Author: cziegeler
Date: Thu Sep  2 06:31:01 2010
New Revision: 991822

URL: http://svn.apache.org/viewvc?rev=991822&view=rev
Log:
Update implementation to support latest dynamic class loader features.

Added:
    sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSDynamicClassLoader.java
  (with props)
Modified:
    sling/trunk/contrib/commons/fsclassloader/NOTICE
    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/resources/META-INF/NOTICE

Modified: sling/trunk/contrib/commons/fsclassloader/NOTICE
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/commons/fsclassloader/NOTICE?rev=991822&r1=991821&r2=991822&view=diff
==============================================================================
--- sling/trunk/contrib/commons/fsclassloader/NOTICE (original)
+++ sling/trunk/contrib/commons/fsclassloader/NOTICE Thu Sep  2 06:31:01 2010
@@ -1,5 +1,5 @@
 Apache Sling FileSystem ClassLoader
-Copyright 2009 The Apache Software Foundation
+Copyright 2009-2010 The Apache Software Foundation
 
 Apache Sling is based on source code originally developed 
 by Day Software (http://www.day.com/).

Modified: sling/trunk/contrib/commons/fsclassloader/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/commons/fsclassloader/pom.xml?rev=991822&r1=991821&r2=991822&view=diff
==============================================================================
--- sling/trunk/contrib/commons/fsclassloader/pom.xml (original)
+++ sling/trunk/contrib/commons/fsclassloader/pom.xml Thu Sep  2 06:31:01 2010
@@ -88,7 +88,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.classloader</artifactId>
-            <version>1.1.0</version>
+            <version>1.2.0</version>
             <scope>provided</scope>
         </dependency>
     </dependencies>

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=991822&r1=991821&r2=991822&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
Thu Sep  2 06:31:01 2010
@@ -27,7 +27,8 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.sling.commons.classloader.ClassLoaderWriter;
 import org.apache.sling.commons.classloader.DynamicClassLoaderProvider;
@@ -47,12 +48,19 @@ public class FSClassLoaderProvider
     /** File root */
     private File root;
 
+    /** All classloaders */
+    private List<FSDynamicClassLoader> loaders = new ArrayList<FSDynamicClassLoader>();
+
     /**
      * @see org.apache.sling.commons.classloader.DynamicClassLoaderProvider#getClassLoader(ClassLoader)
      */
     public ClassLoader getClassLoader(final ClassLoader parent) {
         try {
-            return new URLClassLoader(new URL[] {this.root.toURL()}, parent);
+            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);
@@ -62,20 +70,38 @@ public class FSClassLoaderProvider
     /**
      * @see org.apache.sling.commons.classloader.DynamicClassLoaderProvider#release(java.lang.ClassLoader)
      */
-    public void release(ClassLoader classLoader) {
-        // nothing to do here
+    public void release(final ClassLoader classLoader) {
+        synchronized ( this.loaders ) {
+            this.loaders.remove(classLoader);
+        }
     }
 
+    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);
+                }
+            }
+        }
+    }
     //---------- SCR Integration ----------------------------------------------
 
     /**
      * @see org.apache.sling.commons.classloader.ClassLoaderWriter#delete(java.lang.String)
      */
-    public boolean delete(String name) {
+    public boolean delete(final String name) {
         final String path = cleanPath(name);
         final File file = new File(path);
         if ( file.exists() ) {
-            return file.delete();
+            final boolean result = file.delete();
+            if ( result ) {
+                this.checkClassLoaders(file.getAbsolutePath());
+            }
         }
         // file does not exist so we return false
         return false;
@@ -84,7 +110,7 @@ public class FSClassLoaderProvider
     /**
      * @see org.apache.sling.commons.classloader.ClassLoaderWriter#getOutputStream(java.lang.String)
      */
-    public OutputStream getOutputStream(String name) {
+    public OutputStream getOutputStream(final String name) {
         final String path = cleanPath(name);
         final File file = new File(path);
         final File parentDir = file.getParentFile();
@@ -92,6 +118,9 @@ public class FSClassLoaderProvider
             parentDir.mkdirs();
         }
         try {
+            if ( file.exists() ) {
+                this.checkClassLoaders(path);
+            }
             return new FileOutputStream(path);
         } catch (FileNotFoundException e) {
             throw new RuntimeException(e);
@@ -101,11 +130,15 @@ public class FSClassLoaderProvider
     /**
      * @see org.apache.sling.commons.classloader.ClassLoaderWriter#rename(java.lang.String,
java.lang.String)
      */
-    public boolean rename(String oldName, String newName) {
+    public boolean rename(final String oldName, final String newName) {
         final String oldPath = cleanPath(oldName);
         final String newPath = cleanPath(newName);
         final File old = new File(oldPath);
-        return old.renameTo(new File(newPath));
+        final boolean result = old.renameTo(new File(newPath));
+        if ( result ) {
+            this.checkClassLoaders(oldPath);
+        }
+        return result;
     }
 
     /**
@@ -131,7 +164,7 @@ public class FSClassLoaderProvider
     /**
      * @see org.apache.sling.commons.classloader.ClassLoaderWriter#getInputStream(java.lang.String)
      */
-    public InputStream getInputStream(String name)
+    public InputStream getInputStream(final String name)
     throws IOException {
         final String path = cleanPath(name);
         final File file = new File(path);
@@ -141,7 +174,7 @@ public class FSClassLoaderProvider
     /**
      * @see org.apache.sling.commons.classloader.ClassLoaderWriter#getLastModified(java.lang.String)
      */
-    public long getLastModified(String name) {
+    public long getLastModified(final String name) {
         final String path = cleanPath(name);
         final File file = new File(path);
         if ( file.exists() ) {
@@ -157,7 +190,7 @@ public class FSClassLoaderProvider
      * Create the root directory.
      * @param componentContext
      */
-    protected void activate(ComponentContext componentContext) {
+    protected void activate(final ComponentContext componentContext) {
         // get the file root
         this.root = new File(componentContext.getBundleContext().getDataFile(""), "classes");
         this.root.mkdirs();
@@ -168,7 +201,7 @@ public class FSClassLoaderProvider
      * Create the root directory.
      * @param componentContext
      */
-    protected void deactivate(ComponentContext componentContext) {
+    protected void deactivate(final ComponentContext componentContext) {
         this.root = null;
     }
 }

Added: 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=991822&view=auto
==============================================================================
--- sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSDynamicClassLoader.java
(added)
+++ sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSDynamicClassLoader.java
Thu Sep  2 06:31:01 2010
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+package org.apache.sling.commons.fsclassloader.impl;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.sling.commons.classloader.DynamicClassLoader;
+
+public class FSDynamicClassLoader
+    extends URLClassLoader
+    implements DynamicClassLoader {
+
+    private boolean isDirty = false;
+
+    private final Set<String> hit = Collections.synchronizedSet(new HashSet<String>());
+    private final Set<String> miss = Collections.synchronizedSet(new HashSet<String>());
+
+    public FSDynamicClassLoader(final URL[] urls, final ClassLoader parent) {
+        super(urls, parent);
+    }
+
+    /**
+     * @see org.apache.sling.commons.classloader.DynamicClassLoader#isLive()
+     */
+    public boolean isLive() {
+        return !isDirty;
+    }
+
+    /**
+     * @see java.lang.ClassLoader#loadClass(java.lang.String)
+     */
+    public Class<?> loadClass(final String name) throws ClassNotFoundException {
+        try {
+            final Class<?> c = super.loadClass(name);
+            this.hit.add(name);
+            return c;
+        } catch (final ClassNotFoundException cnfe) {
+            this.miss.add(name);
+            throw cnfe;
+        }
+    }
+
+    public void check(final String className) {
+       this.isDirty = hit.contains(className) || miss.contains(className);
+    }
+}

Propchange: sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSDynamicClassLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSDynamicClassLoader.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/contrib/commons/fsclassloader/src/main/java/org/apache/sling/commons/fsclassloader/impl/FSDynamicClassLoader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/contrib/commons/fsclassloader/src/main/resources/META-INF/NOTICE
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/commons/fsclassloader/src/main/resources/META-INF/NOTICE?rev=991822&r1=991821&r2=991822&view=diff
==============================================================================
--- sling/trunk/contrib/commons/fsclassloader/src/main/resources/META-INF/NOTICE (original)
+++ sling/trunk/contrib/commons/fsclassloader/src/main/resources/META-INF/NOTICE Thu Sep 
2 06:31:01 2010
@@ -1,5 +1,5 @@
 Apache Sling FileSystem ClassLoader
-Copyright 2009 The Apache Software Foundation
+Copyright 2009-2010 The Apache Software Foundation
 
 Apache Sling is based on source code originally developed 
 by Day Software (http://www.day.com/).



Mime
View raw message