View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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  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
    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  		}//end try catch
    59
    60  	}//end method
    61
    62  	}//end class