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
26 private static final Class<?>[] parameters = new Class[] { URL.class };
27
28 public static void addFile( String s ) throws IOException
29 {
30 File f = new File( s );
31 addFile( f );
32 }
33
34 public static void addFile( File f ) throws IOException
35 {
36 addURL( f.toURI().toURL() );
37 }
38
39 public static void addURL( URL u ) throws IOException
40 {
41
42 try
43 {
44 ClassLoader classLoader = SoapUI.class.getClassLoader();
45
46 if( !( classLoader instanceof URLClassLoader ) )
47 {
48 SoapUI.log.error( "SoapUI classloader is not an URLClassLoader, failed to add external library" );
49 SoapUI.log.info( "classloader type is : "+ classLoader.getClass().toString() );
50 return;
51 }
52
53 URLClassLoader sysloader = ( URLClassLoader )classLoader;
54 Class<URLClassLoader> sysclass = URLClassLoader.class;
55 Method method = sysclass.getDeclaredMethod( "addURL", parameters );
56 method.setAccessible( true );
57 method.invoke( sysloader, new Object[] { u } );
58
59 SoapUI.log.info( "Added [" + u.toString() + "] to classpath" );
60
61 }
62 catch( Throwable t )
63 {
64 SoapUI.logError( t );
65 throw new IOException( "Error, could not add URL to system classloader" );
66 }
67
68 }
69
70 }