Removed DOCUMENT ME comments that were generated and applied source code
[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 package org.wamblee.general;
17
18 import java.io.File;
19 import java.io.IOException;
20
21 import java.lang.reflect.Method;
22
23 import java.net.URL;
24 import java.net.URLClassLoader;
25
26 /**
27  * Utility for working with the class loader. Based on the ClassPathHacker
28  * example found on the internet.
29  */
30 public class ClassLoaderUtils {
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     private static final String JAR_SUFFIX = ".jar";
35
36     /**
37      * Adds all jars in the given directory to the class path.
38      * 
39      * @param aDirectory
40      *            Directory.
41      * @throws IOException
42      */
43     public static void addJarsInDirectory(File aDirectory) throws IOException {
44         System.out.println("directory '" + aDirectory + "'");
45
46         for (File aFile : aDirectory.listFiles()) {
47             System.out
48                 .println("Considering '" + aFile.getCanonicalPath() + "'");
49
50             if (aFile.getName().toLowerCase().endsWith(JAR_SUFFIX)) {
51                 System.out.println("Adding '" + aFile.getCanonicalPath() +
52                     "' to classpath.");
53                 addFile(aFile);
54             }
55         }
56     }
57
58     /**
59      * Adds a file to the classpath.
60      * 
61      * @param aFilename
62      *            Filename to add.
63      * @throws IOException
64      */
65     public static void addFile(String aFilename) throws IOException {
66         File f = new File(aFilename);
67         addFile(f);
68     }
69
70     /**
71      * Adds a file to the classpath.
72      * 
73      * @param aFile
74      *            File to add.
75      * @throws IOException
76      */
77     public static void addFile(File aFile) throws IOException {
78         addURL(aFile.toURL());
79     }
80
81     /**
82      * Adds a url to the classpath.
83      * 
84      * @param aUrl
85      *            Url to add.
86      * @throws IOException
87      */
88     public static void addURL(URL aUrl) throws IOException {
89         URLClassLoader sysloader = (URLClassLoader) ClassLoader
90             .getSystemClassLoader();
91         Class sysclass = URLClassLoader.class;
92
93         try {
94             Method method = sysclass.getDeclaredMethod("addURL",
95                 new Class[] { URL.class });
96             method.setAccessible(true);
97             method.invoke(sysloader, new Object[] { aUrl });
98         } catch (Throwable t) {
99             t.printStackTrace();
100             throw new IOException(
101                 "Error, could not add URL to system classloader");
102         }
103     }
104 }