/*
 * This java source file is placed into the public domain.
 *
 * The orginal author is Ceki Gulcu, QOS.ch
 *
 * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN
 * THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
 * ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE
 * USE, MODIFICATION, OR REDISTRIBUTION OF THIS SOFTWARE.
 */
package ch.qos.test;

import box.Box;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.net.URL;
import java.net.URLClassLoader;


/**
 * As long as the thread context class loader is not set explicitly,
 * that is if it defaults to the system class loader, then LogFactory
 * will ignore log4j.jar accessible to the child class loader.
 *
 * Usage:
 *
 *   java -cp classes;boxAPI.jar;lib/commons-logging.jar ch.qos.ParentFirstTestJCL0
 *
 * @author <a href="http://www.qos.ch/log4j/">Ceki G&uuml;lc&uuml;</a>
 */
public class ParentFirstTestJCL0 {
  
  public static void main(String[] args) throws Exception {
  
    URLClassLoader childClassLoader =
      new URLClassLoader(
        new URL[] {
          new URL("file:box1.jar"), 
          new URL("file:lib/commons-logging.jar"),
          new URL("file:lib/log4j.jar")
        });

    // since the thread context class loader defaults to the system
    // class loader, log4j.jar will not be seen
    // log4j will not be used for classes loaded by the system
    // class loader (parent class loader), as shown below
    Log log = LogFactory.getLog("org.wombat");
    log.info("a message");

    // log4j will not be used even in classes loaded by the
    // child class loader
    Class boxClass = childClassLoader.loadClass("box.BoxImplWithJCL");
    Box box = (Box) boxClass.newInstance();
    box.doOp();

    // SUMMARY: JCL only considers the thread context class loader 
    // when checking the existence of a logging implementation
  }
}
