View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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 groovy.lang.Binding;
16  import groovy.lang.GroovyClassLoader;
17  import groovy.lang.GroovyShell;
18  
19  import java.io.File;
20  
21  import com.eviware.soapui.SoapUI;
22  
23  public class ScriptingSupport
24  {
25  	public static SoapUIGroovyShell createGsroovyShell( Binding binding )
26  	{
27  		// LoaderConfiguration config = new LoaderConfiguration();
28  		//		
29  		// String libraries = SoapUI.getSettings().getString(
30  		// ToolsSettings.SCRIPT_LIBRARIES, null );
31  		// if( libraries != null )
32  		// {
33  		// File libs = new File( libraries );
34  		// File[] list = libs.listFiles();
35  		//			
36  		// for( File lib : list)
37  		// {
38  		// if( lib.getName().toLowerCase().endsWith( ".jar" ))
39  		// {
40  		// config.addFile( lib );
41  		// }
42  		// }
43  		// }
44  
45  		// RootLoader loader = new RootLoader( config.getClassPathUrls(), );
46  		GroovyClassLoader groovyClassLoader = new GroovyClassLoader( SoapUI.class.getClassLoader() );
47  		SoapUIGroovyShell groovyShell = binding == null ? new SoapUIGroovyShell( groovyClassLoader )
48  				: new SoapUIGroovyShell( groovyClassLoader, binding );
49  
50  		return groovyShell;
51  	}
52  
53  	public static class SoapUIGroovyShell extends GroovyShell
54  	{
55  		private final GroovyClassLoader classLoader;
56  
57  		protected SoapUIGroovyShell( GroovyClassLoader classLoader, Binding binding )
58  		{
59  			super( classLoader, binding );
60  
61  			this.classLoader = classLoader;
62  
63  			reloadExternalClasses();
64  		}
65  
66  		protected SoapUIGroovyShell( GroovyClassLoader classLoader )
67  		{
68  			super( classLoader );
69  
70  			this.classLoader = classLoader;
71  
72  			reloadExternalClasses();
73  		}
74  
75  		public void reloadExternalClasses()
76  		{
77  			resetLoadedClasses();
78  			classLoader.clearCache();
79  
80  			try
81  			{
82  				File scripts = new File( new File( "" ).getAbsolutePath() + File.separatorChar + "scripts" );
83  				if( scripts.exists() && scripts.isDirectory() )
84  				{
85  					File[] listFiles = scripts.listFiles();
86  					for( File file : listFiles )
87  					{
88  						if( file.isDirectory() || !file.getName().endsWith( ".groovy" ) )
89  							continue;
90  
91  						System.out.println( "parsing " + file.getAbsolutePath() );
92  						classLoader.parseClass( file );
93  					}
94  				}
95  			}
96  			catch( Exception e )
97  			{
98  				SoapUI.logError( e );
99  			}
100 		}
101 	}
102 }