01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: * See the GNU Lesser General Public License for more details at gnu.org.
11: */
12:
13: package com.eviware.soapui.support;
14:
15: import java.io.File;
16: import java.io.IOException;
17: import java.lang.reflect.Method;
18: import java.net.URL;
19: import java.net.URLClassLoader;
20:
21: import com.eviware.soapui.SoapUI;
22:
23: public class ClasspathHacker {
24:
25: private static final Class[] parameters = new Class[] { URL.class };
26:
27: public static void addFile(String s) throws IOException {
28: File f = new File(s);
29: addFile(f);
30: }//end method
31:
32: public static void addFile(File f) throws IOException {
33: addURL(f.toURL());
34: }//end method
35:
36: public static void addURL(URL u) throws IOException {
37:
38: try {
39: ClassLoader classLoader = SoapUI.class.getClassLoader();
40: if (!(classLoader instanceof URLClassLoader)) {
41: SoapUI.log
42: .error("SoapUI classloader is not an URLClassLoader, failed to add external library");
43: return;
44: }
45:
46: URLClassLoader sysloader = (URLClassLoader) classLoader;
47: Class sysclass = URLClassLoader.class;
48: Method method = sysclass.getDeclaredMethod("addURL",
49: parameters);
50: method.setAccessible(true);
51: method.invoke(sysloader, new Object[] { u });
52:
53: SoapUI.log
54: .info("Added [" + u.toString() + "] to classpath");
55:
56: } catch (Throwable t) {
57: SoapUI.logError(t);
58: throw new IOException(
59: "Error, could not add URL to system classloader");
60: }//end try catch
61:
62: }//end method
63:
64: }//end class
|