You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2005/04/15 17:54:06 UTC

svn commit: r161488 - in cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader: ClassLoaderManagerImpl.java NonStaticClassLoaderManager.java RepositoryClassLoader.java

Author: vgritsenko
Date: Fri Apr 15 08:54:05 2005
New Revision: 161488

URL: http://svn.apache.org/viewcvs?view=rev&rev=161488
Log:
Merge NonStaticClassLoaderManager into ClassLoaderManagerImpl, deprecate NonStaticClassLoaderManager.
See also http://marc.theaimsgroup.com/?t=104524385000003&r=1.
Add Collection constructor to RepositoryClassLoader.
Reformat code (4 space indent).
Add dispose() to ClassLoaderManagerImpl.

Modified:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/ClassLoaderManagerImpl.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/NonStaticClassLoaderManager.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/RepositoryClassLoader.java

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/ClassLoaderManagerImpl.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/ClassLoaderManagerImpl.java?view=diff&r1=161487&r2=161488
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/ClassLoaderManagerImpl.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/ClassLoaderManagerImpl.java Fri Apr 15 08:54:05 2005
@@ -1,12 +1,12 @@
 /*
- * Copyright 1999-2004 The Apache Software Foundation.
- * 
+ * Copyright 1999-2005 The Apache Software Foundation.
+ *
  * Licensed 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.
@@ -15,6 +15,7 @@
  */
 package org.apache.cocoon.components.classloader;
 
+import org.apache.avalon.framework.activity.Disposable;
 import org.apache.avalon.framework.thread.ThreadSafe;
 
 import java.io.File;
@@ -22,65 +23,68 @@
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
-import java.util.Vector;
 
 /**
  * A singleton-like implementation of <code>ClassLoaderManager</code>
  *
  * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
- * @version CVS $Id$
+ * @version $Id$
  */
-public class ClassLoaderManagerImpl implements ClassLoaderManager, ThreadSafe {
-  /**
-   * The single class loader instance
-   */
-  private static RepositoryClassLoader instance = null;
-
-  protected final static Set fileSet = Collections.synchronizedSet(new HashSet());
-
-  /**
-   * A constructor that ensures only a single class loader instance exists
-   *
-   */
-  public ClassLoaderManagerImpl() {
-    if (instance == null) {
-      this.reinstantiate();
+public class ClassLoaderManagerImpl implements ClassLoaderManager, ThreadSafe, Disposable {
+
+    /**
+     * Set of class directories
+     */
+    protected final Set fileSet = Collections.synchronizedSet(new HashSet());
+
+    /**
+     * The class loader instance
+     */
+    private RepositoryClassLoader instance;
+
+    /**
+     * A constructor that ensures only a single class loader instance exists
+     */
+    public ClassLoaderManagerImpl() {
+        reinstantiate();
+    }
+
+    public void dispose() {
+        this.fileSet.clear();
+        reinstantiate();
+    }
+
+    /**
+     * Add a directory to the proxied class loader
+     *
+     * @param directoryName The repository name
+     * @throws IOException If the directory is invalid
+     */
+    public void addDirectory(File directoryName) throws IOException {
+        if (this.fileSet.add(directoryName)) {
+            this.instance.addDirectory(directoryName);
+        }
     }
-  }
 
-  /**
-   * Add a directory to the proxied class loader
-   *
-   * @param directoryName The repository name
-   * @exception IOException If the directory is invalid
-   */
-  public void addDirectory(File directoryName) throws IOException {
-    if ( ! ClassLoaderManagerImpl.fileSet.contains(directoryName)) {
-        ClassLoaderManagerImpl.fileSet.add(directoryName);
-        ClassLoaderManagerImpl.instance.addDirectory(directoryName);
+    /**
+     * Load a class through the proxied class loader
+     *
+     * @param className The name of the class to be loaded
+     * @return The loaded class
+     * @throws ClassNotFoundException If the class is not found
+     */
+    public Class loadClass(String className) throws ClassNotFoundException {
+        return this.instance.loadClass(className);
     }
-  }
 
-  /**
-   * Load a class through the proxied class loader
-   *
-   * @param className The name of the class to be loaded
-   * @return The loaded class
-   * @exception ClassNotFoundException If the class is not found
-   */
-  public Class loadClass(String className) throws ClassNotFoundException {
-    return ClassLoaderManagerImpl.instance.loadClass(className);
-  }
-
-  /**
-   * Reinstantiate the proxied class loader to allow for class reloading
-   *
-   */
-  public void reinstantiate() {
-    if ( ClassLoaderManagerImpl.fileSet.isEmpty()) {
-      ClassLoaderManagerImpl.instance = new RepositoryClassLoader();
-    } else {
-      ClassLoaderManagerImpl.instance = new RepositoryClassLoader(new Vector(ClassLoaderManagerImpl.fileSet));
+    /**
+     * Reinstantiate the proxied class loader to allow for class reloading
+     */
+    public void reinstantiate() {
+        if (this.fileSet.isEmpty()) {
+            this.instance = new RepositoryClassLoader();
+        } else {
+            this.instance = new RepositoryClassLoader(this.fileSet);
+        }
     }
-  }
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/NonStaticClassLoaderManager.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/NonStaticClassLoaderManager.java?view=diff&r1=161487&r2=161488
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/NonStaticClassLoaderManager.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/NonStaticClassLoaderManager.java Fri Apr 15 08:54:05 2005
@@ -1,12 +1,12 @@
 /*
- * Copyright 1999-2004 The Apache Software Foundation.
- * 
+ * Copyright 1999-2005 The Apache Software Foundation.
+ *
  * Licensed 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.
@@ -15,71 +15,9 @@
  */
 package org.apache.cocoon.components.classloader;
 
-import java.io.File;
-import java.io.IOException;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.Vector;
-
-import org.apache.avalon.framework.thread.ThreadSafe;
-
 /**
- * A singleton-like implementation of <code>ClassLoaderManager</code>
- *
- * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
- * @author <a href="mailto:proyal@apache.org">Peter Royal</a>
- * @version CVS $Id: NonStaticClassLoaderManager.java,v 1.2 2004/03/05 13:02:46 bdelacretaz Exp $
+ * @deprecated Merged into ClassLoaderManagerImpl
+ * @version $Id$
  */
-public class NonStaticClassLoaderManager implements ClassLoaderManager, ThreadSafe
-{
-    /**
-     * The single class loader instance
-     */
-    private RepositoryClassLoader instance = new RepositoryClassLoader();
-
-    private Set fileSet = Collections.synchronizedSet( new HashSet() );
-
-    /**
-     * Add a directory to the proxied class loader
-     *
-     * @param directoryName The repository name
-     * @exception IOException If the directory is invalid
-     */
-    public void addDirectory( File directoryName ) throws IOException
-    {
-        if( !this.fileSet.contains( directoryName ) )
-        {
-            this.fileSet.add( directoryName );
-            this.instance.addDirectory( directoryName );
-        }
-    }
-
-    /**
-     * Load a class through the proxied class loader
-     *
-     * @param className The name of the class to be loaded
-     * @return The loaded class
-     * @exception ClassNotFoundException If the class is not found
-     */
-    public Class loadClass( String className ) throws ClassNotFoundException
-    {
-        return this.instance.loadClass( className );
-    }
-
-    /**
-     * Reinstantiate the proxied class loader to allow for class reloading
-     *
-     */
-    public void reinstantiate()
-    {
-        if( this.fileSet.isEmpty() )
-        {
-            this.instance = new RepositoryClassLoader();
-        }
-        else
-        {
-            this.instance = new RepositoryClassLoader( new Vector( this.fileSet ) );
-        }
-    }
+public class NonStaticClassLoaderManager extends ClassLoaderManagerImpl {
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/RepositoryClassLoader.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/RepositoryClassLoader.java?view=diff&r1=161487&r2=161488
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/RepositoryClassLoader.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/classloader/RepositoryClassLoader.java Fri Apr 15 08:54:05 2005
@@ -1,12 +1,12 @@
 /*
- * Copyright 1999-2004 The Apache Software Foundation.
- * 
+ * Copyright 1999-2005 The Apache Software Foundation.
+ *
  * Licensed 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.
@@ -17,6 +17,7 @@
 
 import org.apache.avalon.framework.logger.LogEnabled;
 import org.apache.avalon.framework.logger.Logger;
+
 import org.apache.cocoon.CascadingIOException;
 import org.apache.cocoon.util.ClassUtils;
 
@@ -25,6 +26,7 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.Vector;
 
@@ -36,112 +38,118 @@
  *
  * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
  * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
- * @version CVS $Id: RepositoryClassLoader.java,v 1.2 2004/03/05 13:02:46 bdelacretaz Exp $
+ * @version $Id$
  */
-public class RepositoryClassLoader extends URLClassLoader implements LogEnabled {
+public class RepositoryClassLoader extends URLClassLoader
+                                   implements LogEnabled {
+
+    /**
+     * The logger
+     */
+    protected Logger log;
+
+    /**
+     * Create an empty new class loader.
+     */
+    public RepositoryClassLoader() {
+        super(new URL[]{}, ClassUtils.getClassLoader());
+    }
+
+    /**
+     * Create an empty new class loader.
+     */
+    public RepositoryClassLoader(URL[] urls) {
+        super(urls, ClassUtils.getClassLoader());
+    }
+
+    /**
+     * Create an empty new class loader.
+     */
+    public RepositoryClassLoader(URL[] urls, ClassLoader parentClassLoader) {
+        super(urls, parentClassLoader);
+    }
+
+    /**
+     * Create a class loader from a list of directories
+     *
+     * @param repositories List of searchable directories
+     */
+    protected RepositoryClassLoader(Vector repositories) {
+        this((Collection) repositories);
+    }
+
+    /**
+     * Create a class loader from a list of directories
+     *
+     * @param repositories List of searchable directories
+     */
+    protected RepositoryClassLoader(Collection repositories) {
+        this();
+        Iterator i = repositories.iterator();
+        while (i.hasNext()) {
+            try {
+                this.addDirectory((File) i.next());
+            } catch (IOException ioe) {
+                log.error("Repository could not be added", ioe);
+            }
+        }
+    }
 
-  /**
-   * The logger
-   */
-  protected Logger log;
-
-  /**
-   * Create an empty new class loader.
-   */
-  public RepositoryClassLoader() {
-    super(new URL[] {}, ClassUtils.getClassLoader());
-  }
-
-  /**
-   * Create an empty new class loader.
-   */
-  public RepositoryClassLoader(URL[] urls) {
-    super(urls, ClassUtils.getClassLoader());
-  }
-
-  /**
-   * Create an empty new class loader.
-   */
-  public RepositoryClassLoader(URL[] urls, ClassLoader parentClassLoader) {
-    super(urls, parentClassLoader);
-  }
-
-  /**
-   * Provide component with a logger.
-   * 
-   * @param logger the logger
-   */
-  public void enableLogging(Logger logger) {
-    if (this.log == null) {
-      this.log = logger;
-    }
-  }
-
-
-  /**
-   * Create a class loader from a list of directories
-   *
-   * @param repositories List of searchable directories
-   */
-  protected RepositoryClassLoader(Vector repositories) {
-      this();
-      Iterator i = repositories.iterator();
-      while (i.hasNext()) {
-          try {
-              this.addDirectory((File) i.next());
-          } catch (IOException ioe) {
-              log.error("Repository could not be added", ioe);
-          }
-      }
-  }
-
-  /**
-   * Add a directory to the list of searchable repositories.
-   * This methods ensures that no directory is specified more than once.
-   *
-   * @param repository The directory path
-   * @exception IOException Non-existent, non-readable or non-directory
-   * repository
-   */
-  public void addDirectory(File repository) throws IOException {
-      try {
-          this.addURL(repository.getCanonicalFile().toURL());
-      } catch (MalformedURLException mue) {
-          log.error("The repository had a bad URL", mue);
-          throw new CascadingIOException("Could not add repository", mue);
-      }
-  }
-
-  /**
-   * Add a directory to the list of searchable repositories.
-   * This methods ensures that no directory is specified more than once.
-   *
-   * @param repository The directory path
-   * @exception IOException Non-existent, non-readable or non-directory
-   * repository
-   */
-  public void addDirectory(String repository) throws IOException {
-      try {
-          File file = new File(repository);
-          this.addURL(file.getCanonicalFile().toURL());
-      } catch (MalformedURLException mue) {
-          log.error("The repository had a bad URL", mue);
-          throw new CascadingIOException("Could not add repository", mue);
-      }
-  }
-
-  /**
-   * Add a url to the list of searchable repositories
-   */
-  public void addURL(URL url) {
-      super.addURL(url);
-  }
-
-  /**
-   * Create a Class from a byte array
-   */
-  public Class defineClass(byte [] b) throws ClassFormatError {
-      return super.defineClass(null, b, 0, b.length);
-  }
+    /**
+     * Provide component with a logger.
+     *
+     * @param logger the logger
+     */
+    public void enableLogging(Logger logger) {
+        if (this.log == null) {
+            this.log = logger;
+        }
+    }
+
+    /**
+     * Add a directory to the list of searchable repositories. This methods ensures that no directory is specified more
+     * than once.
+     *
+     * @param repository The directory path
+     * @throws IOException Non-existent, non-readable or non-directory repository
+     */
+    public void addDirectory(File repository) throws IOException {
+        try {
+            this.addURL(repository.getCanonicalFile().toURL());
+        } catch (MalformedURLException mue) {
+            log.error("The repository had a bad URL", mue);
+            throw new CascadingIOException("Could not add repository", mue);
+        }
+    }
 
+    /**
+     * Add a directory to the list of searchable repositories. This methods ensures that no directory is specified more
+     * than once.
+     *
+     * @param repository The directory path
+     * @throws IOException Non-existent, non-readable or non-directory repository
+     */
+    public void addDirectory(String repository) throws IOException {
+        try {
+            File file = new File(repository);
+            this.addURL(file.getCanonicalFile().toURL());
+        } catch (MalformedURLException mue) {
+            log.error("The repository had a bad URL", mue);
+            throw new CascadingIOException("Could not add repository", mue);
+        }
+    }
+
+    /**
+     * Add a url to the list of searchable repositories
+     */
+    public void addURL(URL url) {
+        super.addURL(url);
+    }
+
+    /**
+     * Create a Class from a byte array
+     */
+    public Class defineClass(byte[] b) throws ClassFormatError {
+        return super.defineClass(null, b, 0, b.length);
+    }
 }