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 java.io.File;
16  import java.io.IOException;
17  import java.lang.reflect.Method;
18  import java.net.URL;
19  
20  import com.eviware.soapui.SoapUI;
21  
22  public class ClasspathHacker
23  {
24  	private static void addFile( String s ) throws IOException
25  	{
26  		File f = new File( s );
27  		addFile( f );
28  	}// end method
29  
30  	private static void addFile( File f ) throws IOException
31  	{
32  		addURL( f.toURI().toURL() );
33  	}// end method
34  
35  	private static void addURL( URL u ) throws IOException
36  	{
37  		ClassLoader classLoader = SoapUI.class.getClassLoader();
38  
39  		addUrlToClassLoader( u, classLoader );
40  
41  	}// end method
42  
43  	private static void addUrlToClassLoader( URL u, ClassLoader classLoader ) throws IOException
44  	{
45  		try
46  		{
47  			Method method = classLoader.getClass().getDeclaredMethod( "addURL", new Class[] { java.net.URL.class } );
48  			method.setAccessible( true );
49  			method.invoke( classLoader, new Object[] { u } );
50  
51  			SoapUI.log.info( "Added [" + u.toString() + "] to classpath" );
52  		}
53  		catch( NoSuchMethodException e )
54  		{
55  			try
56  			{
57  				Method method = classLoader.getClass().getSuperclass().getDeclaredMethod( "addURL",
58  						new Class[] { java.net.URL.class } );
59  				method.setAccessible( true );
60  				method.invoke( classLoader, new Object[] { u } );
61  
62  				SoapUI.log.info( "Added [" + u.toString() + "] to classpath" );
63  			}
64  			catch( NoSuchMethodException ex )
65  			{
66  				try
67  				{
68  					Method method = classLoader.getClass().getSuperclass().getSuperclass().getDeclaredMethod( "addURL",
69  							new Class[] { java.net.URL.class } );
70  					method.setAccessible( true );
71  					method.invoke( classLoader, new Object[] { u } );
72  
73  					SoapUI.log.info( "Added [" + u.toString() + "] to classpath" );
74  				}
75  				catch( Throwable t )
76  				{
77  					try
78  					{
79  						if( classLoader.getParent() != null )
80  						{
81  							SoapUI.log.info( "Failed to add jar to " + classLoader.getClass().getName() + ", trying parent" );
82  							addUrlToClassLoader( u, classLoader.getParent() );
83  						}
84  						else
85  							throw new IOException( "Error, could not add URL to classloader "
86  									+ classLoader.getClass().getName() );
87  					}
88  					catch( IOException e3 )
89  					{
90  						SoapUI.logError( t );
91  						throw e3;
92  					}
93  				}// end try catch
94  			}
95  			catch( Throwable t )
96  			{
97  				SoapUI.logError( t );
98  				throw new IOException( "Error, could not add URL to system classloader " + classLoader.getClass().getName() );
99  			}// end try catch
100 		}
101 		catch( Throwable t )
102 		{
103 			SoapUI.logError( t );
104 			throw new IOException( "Error, could not add URL to system classloader " + classLoader.getClass().getName() );
105 		}// end try catch
106 	}
107 
108 }// end class