1   package ch.qos.logback.classic.spi;
2   
3   import java.net.URL;
4   import java.net.URLClassLoader;
5   
6   /**
7    * An almost trivial no fuss implementation of a class loader following the
8    * child-first delegation model.
9    * 
10   * @author Ceki Gülcü
11   */
12  public class LocalFirstClassLoader extends URLClassLoader {
13  
14    public LocalFirstClassLoader(URL[] urls) {
15      super(urls);
16    }
17  
18    public LocalFirstClassLoader(URL[] urls, ClassLoader parent) {
19      super(urls, parent);
20    }
21  
22    public void addURL(URL url) {
23      super.addURL(url);
24    }
25  
26    public Class<?> loadClass(String name) throws ClassNotFoundException {
27      return loadClass(name, false);
28    }
29  
30    /**
31     * We override the parent-first behavior established by java.lang.Classloader.
32     * 
33     * The implementation is surprisingly straightforward.
34     */
35    protected Class<?> loadClass(String name, boolean resolve)
36        throws ClassNotFoundException {
37  
38      // First, check if the class has already been loaded
39      Class c = findLoadedClass(name);
40  
41      // if not loaded, search the local (child) resources
42      if (c == null) {
43        try {
44          c = findClass(name);
45        } catch (ClassNotFoundException cnfe) {
46          // ignore
47        }
48      }
49  
50      // if we could not find it, delegate to parent
51      // Note that we don't attempt to catch any ClassNotFoundException
52      if (c == null) {
53        if (getParent() != null) {
54          c = getParent().loadClass(name);
55        } else {
56          c = getSystemClassLoader().loadClass(name);
57        }
58      }
59  
60      if (resolve) {
61        resolveClass(c);
62      }
63  
64      return c;
65    }
66  }