View Javadoc
1   package org.andromda.maven.plugin.configuration;
2   
3   import java.net.URL;
4   import java.net.URLClassLoader;
5   
6   /**
7    * A custom class loader necessary to avoid class loading errors
8    * when running within Maven2.
9    *
10   * @author Chad Brandon
11   */
12  public class ConfigurationClassLoader
13      extends URLClassLoader
14  {
15      /**
16       * @param urls
17       * @param parent
18       */
19      public ConfigurationClassLoader(
20          final URL[] urls,
21          final ClassLoader parent)
22      {
23          super(urls, parent);
24      }
25  
26      /**
27       * @see ClassLoader#loadClass(String)
28       */
29      public Class loadClass(final String name)
30          throws ClassNotFoundException
31      {
32          return this.loadClass(
33              name,
34              false);
35      }
36  
37      /**
38       * @see ClassLoader#loadClass(String, boolean)
39       */
40      protected synchronized Class loadClass(
41          final String name,
42          final boolean resolve)
43          throws ClassNotFoundException
44      {
45          // - first, we check if the class has already been loaded
46          Class loadedClass = this.findLoadedClass(name);
47  
48          // - if we could not find it, try to find it in the parent
49          if (loadedClass == null)
50          {
51              final ClassLoader parent = this.getParent();
52              if (parent != null)
53              {
54                  try
55                  {
56                      loadedClass = parent.loadClass(name);
57                  }
58                  catch (final ClassNotFoundException exception)
59                  {
60                      // - ignore
61                  }
62              }
63              else
64              {
65                  loadedClass = getSystemClassLoader().loadClass(name);
66              }
67          }
68  
69          // - if not loaded from the parent, search this classloader
70          if (loadedClass == null)
71          {
72              loadedClass = findClass(name);
73          }
74  
75          if (resolve)
76          {
77              this.resolveClass(loadedClass);
78          }
79  
80          return loadedClass;
81      }
82  }