You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ps...@apache.org on 2004/06/15 00:55:51 UTC

cvs commit: logging-log4j/src/java/org/apache/log4j/chainsaw/plugins PluginClassLoaderFactory.java

psmith      2004/06/14 15:55:51

  Added:       src/java/org/apache/log4j/chainsaw/plugins
                        PluginClassLoaderFactory.java
  Log:
  An initial attempt to work around some classloader issues within Web start.  This classloader
  will be able to reference jars & classes etc in a sub-directory of a users chainsaw home directory.
  
  Hopefully this will allow things like the weblogic.jar to be referenced, but I have not completed formal testing
  at this stage, but it does not have any impact on existing functionality so it seems safe to commit it
  in it's current state. (touching wood).
  
  Revision  Changes    Path
  1.1                  logging-log4j/src/java/org/apache/log4j/chainsaw/plugins/PluginClassLoaderFactory.java
  
  Index: PluginClassLoaderFactory.java
  ===================================================================
  
  package org.apache.log4j.chainsaw.plugins;
  
  import java.io.File;
  import java.io.FilenameFilter;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.log4j.helpers.LogLog;
  
  /**
   * A factory class to create a Classloader that can refenerence jars/classes/resources
   * within a users plugin directory.
   * 
   * Currently a work in progress to see if this allows external jars required by
   * some 3rd party vendors for things like the JMSReceiver.
   *  
   * @author psmith
   *
   * 
   */
  public class PluginClassLoaderFactory {
  
  	/**
  	 * @param urls
  	 */
  	private PluginClassLoaderFactory() {
  	}
      
      /**
       * Creates a Classloader that will be able to access any of the classes found
       * in any .JAR file contained within the specified directory path, PLUS
       * the actual Plugin directory itself, so it acts like the WEB-INF/classes directory,
       * any class file in the directory will be accessible
       * 
       * @param pluginDirectory
       * @throws IllegalArgumentException if the pluginDirectory is null, does not exist, or cannot be read
       * @throws RuntimeException if turning a File into a URL failed, which would be very unexpected
       * @return
       */
      public static final ClassLoader create(File pluginDirectory) {
          if(pluginDirectory == null || !pluginDirectory.exists() || !pluginDirectory.canRead()) {
           throw new IllegalArgumentException("pluginDirectory cannot be null, and it must exist and must be readable");   
          }
          
          String[] strings = pluginDirectory.list(new FilenameFilter() {
  
  			public boolean accept(File dir, String name) {
                  return name.toUpperCase().endsWith(".JAR");
  			}});
          
        
          List list = new ArrayList();
          // add the plugin directory as a resource loading path
          try {
  			list.add(pluginDirectory.toURL());
  		} catch (Exception e) {
  			throw new RuntimeException(e);
  		}
          if (strings !=null) {
  			for (int i = 0; i < strings.length; i++) {
  				String name = strings[i];
  				File file = new File(pluginDirectory, name);
  				try {
  					list.add(file.toURL());
  					LogLog.info("Added " + file.getAbsolutePath()
  							+ " to Plugin class loader list");
  				} catch (Exception e) {
  					LogLog.error("Failed to retrieve the URL for file: "
  							+ file.getAbsolutePath());
  					throw new RuntimeException(e);
  				}
  			}
  		}
          URL[] urls = (URL[]) list.toArray(new URL[list.size()]);
          return new URLClassLoader(urls);
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org