View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  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 hermes.JAXBHermesLoader;
16  
17  import java.io.File;
18  import java.io.IOException;
19  import java.lang.reflect.Method;
20  import java.net.URL;
21  import java.net.URLClassLoader;
22  
23  import com.eviware.soapui.SoapUI;
24  
25  public class HermesJMSClasspathHacker
26  {
27  
28  	private static final Class<?>[] parameters = new Class[] { URL.class };
29  
30  	public static void addFile( String s ) throws IOException
31  	{
32  		File f = new File( s );
33  		addFile( f );
34  	}// end method
35  
36  	public static void addFile( File f ) throws IOException
37  	{
38  		addURL( f.toURI().toURL() );
39  	}// end method
40  
41  	public static void addURL( URL u ) throws IOException
42  	{
43  
44  		try
45  		{
46  			ClassLoader classLoader = JAXBHermesLoader.class.getClassLoader();
47  			if( !( classLoader instanceof URLClassLoader ) )
48  			{
49  				SoapUI.log.error( "SoapUI classloader is not an URLClassLoader, failed to add external library" );
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  		}// end try catch
67  
68  	}// end method
69  
70  }// end class