1
2
3
4
5
6
7
8
9
10
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 }
31
32 public static void addFile(File f) throws IOException {
33 addURL(f.toURI().toURL());
34 }
35
36
37 public static void addURL(URL u) throws IOException {
38
39 try {
40 ClassLoader classLoader = SoapUI.class.getClassLoader();
41 if( !(classLoader instanceof URLClassLoader ))
42 {
43 SoapUI.log.error( "SoapUI classloader is not an URLClassLoader, failed to add external library" );
44 return;
45 }
46
47 URLClassLoader sysloader = (URLClassLoader)classLoader;
48 Class sysclass = URLClassLoader.class;
49 Method method = sysclass.getDeclaredMethod("addURL",parameters);
50 method.setAccessible(true);
51 method.invoke(sysloader,new Object[]{ u });
52
53 SoapUI.log.info( "Added [" + u.toString() + "] to classpath");
54
55 } catch (Throwable t) {
56 SoapUI.logError( t );
57 throw new IOException("Error, could not add URL to system classloader");
58 }
59
60 }
61
62 }