001package edu.pdx.cs410J.reflect;
002
003import java.io.File;
004import java.net.*;
005
006/**
007 * This <code>ClassLoader</code> prints out the name of every class
008 * that it (or its parent) loads.
009 */
010public class LoggingClassLoader extends URLClassLoader {
011
012  /**
013   * Creates a <code>LoggingClassLoader</code> that loads classes from
014   * a given array of URLs.  However, before this class loader
015   * attempts to load the class, it will delegate to its parent class
016   * loader.
017   */
018  public LoggingClassLoader(URL[] urls, ClassLoader parent) {
019    super(urls, parent);
020  }
021
022  /**
023   * Invoked as this class loader is loading a class
024   */
025  public Class loadClass(String className) 
026    throws ClassNotFoundException {
027
028    System.out.println("Loading " + className);
029    return super.loadClass(className);
030  }
031  
032  /**
033   * Main program that uses a <code>LoggingClassLoader</code> to load
034   * the class with the given name from a given location.
035   */
036  public static void main(String[] args) {
037    File file = new File(args[0]);
038    String className = args[1];
039
040    try {
041      URL[] urls = new URL[] { file.toURL() };
042      ClassLoader parent = ClassLoader.getSystemClassLoader();
043      ClassLoader cl = new LoggingClassLoader(urls, parent);
044      cl.loadClass(className);
045
046    } catch (MalformedURLException ex) {
047      String s = "Bad URL: " + ex;
048      System.err.println(s);
049
050    } catch (ClassNotFoundException ex) {
051      String s = "Could not find class " + className;
052      System.err.println(s);
053    }
054  }
055
056}