(no commit message)
[utils] / support / general / src / main / java / org / wamblee / classloading / ClassLoaderUtils.java
1 /*
2  * Copyright 2005-2010 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.classloading;
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 adding jars to the system class loader.
28  */
29 public class ClassLoaderUtils {
30     // No logging in this class to keep the required class libraries
31     // limited to the standard java classes. This allows use of the
32     // utilities in an environment with a very limited classpath.
33     private static final String JAR_SUFFIX = ".jar";
34
35     /**
36      * Adds all jars in the given directory to the class path.
37      * 
38      * @param aDirectory
39      *            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
49             if (aFile.getName().toLowerCase().endsWith(JAR_SUFFIX)) {
50                 System.out.println("Adding '" + aFile.getCanonicalPath() +
51                     "' to classpath.");
52                 addFile(aFile);
53             }
54         }
55     }
56
57     /**
58      * Adds a file to the classpath.
59      * 
60      * @param aFilename
61      *            Filename to add.
62      * @throws IOException
63      */
64     public static void addFile(String aFilename) throws IOException {
65         File f = new File(aFilename);
66         addFile(f);
67     }
68
69     /**
70      * Adds a file to the classpath.
71      * 
72      * @param aFile
73      *            File to add.
74      * @throws IOException
75      */
76     public static void addFile(File aFile) throws IOException {
77         addURL(aFile.toURI().toURL());
78     }
79
80     /**
81      * Adds a url to the classpath.
82      * 
83      * @param aUrl
84      *            Url to add.
85      * @throws IOException
86      */
87     public static void addURL(URL aUrl) throws IOException {
88         URLClassLoader sysloader = (URLClassLoader) ClassLoader
89             .getSystemClassLoader();
90         Class sysclass = URLClassLoader.class;
91
92         try {
93             Method method = sysclass.getDeclaredMethod("addURL",
94                 new Class[] { URL.class });
95             method.setAccessible(true);
96             method.invoke(sysloader, new Object[] { aUrl });
97         } catch (Throwable t) {
98             t.printStackTrace();
99             throw new IOException(
100                 "Error, could not add URL to system classloader");
101         }
102     }
103 }