View Javadoc
1   package de.juplo.plugins.hibernate;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.net.URL;
6   import java.net.URLClassLoader;
7   import java.util.Arrays;
8   import java.util.Enumeration;
9   import java.util.LinkedHashSet;
10  import org.apache.maven.plugin.logging.Log;
11  
12  
13  /**
14   *
15   * @author kai
16   */
17  public class MutableClassLoader extends ClassLoader
18  {
19    private URLClassLoader loader;
20    private Log log;
21  
22  
23    public MutableClassLoader(LinkedHashSet<URL> urls, Log log)
24    {
25      if (log.isDebugEnabled())
26        for (URL url : urls)
27          log.debug(url.toString());
28      loader =
29          new URLClassLoader(
30              urls.toArray(new URL[urls.size()]),
31              getClass().getClassLoader()
32              );
33      this.log = log;
34    }
35  
36  
37    public MutableClassLoader add(LinkedHashSet<URL> urls)
38    {
39      LinkedHashSet<URL> old =
40          new LinkedHashSet<URL>(Arrays.asList(loader.getURLs()));
41      old.addAll(urls);
42      if (log.isDebugEnabled())
43        for (URL url : urls)
44          log.debug(url.toString());
45      loader =
46          new URLClassLoader(
47              old.toArray(new URL[urls.size()]),
48              getClass().getClassLoader()
49              );
50      return this;
51    }
52  
53  
54    @Override
55    public void clearAssertionStatus()
56    {
57      loader.clearAssertionStatus();
58    }
59  
60    @Override
61    public void setClassAssertionStatus(String className, boolean enabled)
62    {
63      loader.setClassAssertionStatus(className, enabled);
64    }
65  
66    @Override
67    public void setPackageAssertionStatus(String packageName, boolean enabled)
68    {
69      loader.setPackageAssertionStatus(packageName, enabled);
70    }
71  
72    @Override
73    public void setDefaultAssertionStatus(boolean enabled)
74    {
75      loader.setDefaultAssertionStatus(enabled);
76    }
77  
78    @Override
79    public InputStream getResourceAsStream(String name)
80    {
81      return loader.getResourceAsStream(name);
82    }
83  
84    @Override
85    public Enumeration<URL> getResources(String name) throws IOException
86    {
87      return loader.getResources(name);
88    }
89  
90    @Override
91    public URL getResource(String name)
92    {
93      return loader.getResource(name);
94    }
95  
96    @Override
97    public Class<?> loadClass(String name) throws ClassNotFoundException
98    {
99      return loader.loadClass(name);
100   }
101   
102 }