View Javadoc

1   package org.xmlhammer.gui.util;
2   
3   import java.io.File;
4   import java.net.MalformedURLException;
5   import java.net.URI;
6   import java.net.URL;
7   import java.net.URLClassLoader;
8   
9   public class ExtensionClassLoader extends URLClassLoader {
10  
11      public ExtensionClassLoader(ClassLoader parent) {
12          super(new URL[0], parent);
13      }
14  
15      public void add(URI uri) throws MalformedURLException {
16          
17          for ( URL item : getURLs()) {
18              if (item.sameFile(uri.toURL())) {
19                  return;
20              }
21          }
22  
23          super.addURL(uri.toURL());
24      }
25  
26      public void add(File file) {
27          if(file.exists() && file.isDirectory()) {
28              File jars[] = file.listFiles();
29              
30              for(int i = 0; i < jars.length; i++) {
31                  if(jars[i].getAbsolutePath().toLowerCase().endsWith(".jar")) {
32                      try {
33                          add( jars[i].getAbsoluteFile().toURI());
34                      } catch(MalformedURLException e) {
35                          throw new IllegalArgumentException(e.toString());
36                      }
37                  }
38              }
39          } else if ( file.isFile() && file.getAbsolutePath().toLowerCase().endsWith(".jar")) {
40              try {
41                  add( file.getAbsoluteFile().toURI());
42              } catch(MalformedURLException e) {
43                  throw new IllegalArgumentException(e.toString());
44              }
45          }
46      }
47  }
48