View Javadoc

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