View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   package it.imolinfo.jbi4corba.webservice.generator;
9   
10  import java.net.URL;
11  import java.net.URLClassLoader;
12  import java.net.URLStreamHandlerFactory;
13  
14  /**
15   * URLClassLoader that look first in the child URL.
16   * @author Marco Piraccini
17   */
18  public class ChildFirstClassLoader extends URLClassLoader {
19  
20  
21      public ChildFirstClassLoader() {
22          this(null, null, null);
23      }
24  
25  
26      public ChildFirstClassLoader(final ClassLoader parent) {
27          this(new URL[0], parent, null);
28      }
29  
30      public ChildFirstClassLoader(final URL[] urls) {
31          this(urls, null, null);
32      }
33  
34      public ChildFirstClassLoader(final URL[] urls, final ClassLoader parent) {
35          this(urls, parent, null);
36      }
37      public ChildFirstClassLoader(final URL[] urls, final ClassLoader parent, final URLStreamHandlerFactory factory) {
38          super(urls, parent, factory);
39      }
40      public static final URLClassLoader newInstance(final URL[] urls) {
41          return new ChildFirstClassLoader(urls);
42      }      
43      public static final URLClassLoader newInstance(final URL[] urls, final ClassLoader parent) {
44          return new ChildFirstClassLoader(urls, parent);
45      } 
46  
47      public final Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
48          // First check if it's already loaded
49          Class clazz = findLoadedClass(name);
50          
51          if (clazz == null) {
52              try {
53                  // Child first!!!!
54                  clazz = findClass(name);
55              } catch (ClassNotFoundException cnfe) {
56                  ClassLoader parent = getParent();
57                  
58                  if (parent != null) {
59                      // Ask to parent
60                      clazz = parent.loadClass(name);
61                  } else {
62                      // Propagate exception
63                      throw cnfe;
64                  }
65              }
66          }
67  
68          if (resolve) {
69              resolveClass(clazz);
70          }
71  
72          return clazz;
73      }         
74  
75  }
76