5275e15972c847265bfdad3a7c3398ffa80d1427
[utils] / support / general / src / main / java / org / wamblee / general / ClassLoaderUtils.java
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.general;
18
19 import java.io.IOException;
20 import java.io.File;
21 import java.net.URLClassLoader;
22 import java.net.URL;
23 import java.lang.reflect.Method;
24
25 /**
26  * Utility for working with the class loader. Based on the ClassPathHacker
27  * example found on the internet.   
28  */
29 public class ClassLoaderUtils {
30     
31     // No logging in this class to keep the required class libraries
32     // limited to the standard java classes. This allows use of the
33     // utilities in an environment with a very limited classpath. 
34
35     private static final String JAR_SUFFIX = ".jar";
36
37     /**
38      * Adds all jars in the given directory to the class path. 
39      * @param aDirectory Directory. 
40      * @throws IOException
41      */
42     public static void addJarsInDirectory(File aDirectory) throws IOException {
43         System.out.println("directory '" + aDirectory + "'");
44
45         for (File aFile : aDirectory.listFiles()) {
46             System.out
47                     .println("Considering '" + aFile.getCanonicalPath() + "'");
48             if (aFile.getName().toLowerCase().endsWith(JAR_SUFFIX)) {
49                 System.out.println("Adding '" + aFile.getCanonicalPath()
50                         + "' to classpath.");
51                 addFile(aFile);
52             }
53         }
54     }
55
56     /**
57      * Adds a file to the classpath.
58      * @param aFilename Filename to add. 
59      * @throws IOException
60      */
61     public static void addFile(String aFilename) throws IOException {
62         File f = new File(aFilename);
63         addFile(f);
64     }
65
66     /**
67      * Adds a file to the classpath. 
68      * @param aFile File to add. 
69      * @throws IOException
70      */
71     public static void addFile(File aFile) throws IOException {
72         addURL(aFile.toURL());
73     }
74
75     /**
76      * Adds a url to the classpath.
77      * @param aUrl Url to add. 
78      * @throws IOException
79      */
80     public static void addURL(URL aUrl) throws IOException {
81
82         URLClassLoader sysloader = (URLClassLoader) ClassLoader
83                 .getSystemClassLoader();
84         Class sysclass = URLClassLoader.class;
85
86         try {
87             Method method = sysclass.getDeclaredMethod("addURL", new Class[]{ URL.class } );
88             method.setAccessible(true);
89             method.invoke(sysloader, new Object[] { aUrl });
90         } catch (Throwable t) {
91             t.printStackTrace();
92             throw new IOException(
93                     "Error, could not add URL to system classloader");
94         }
95
96     }
97
98 }