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.scripting.groovy;
14  
15  import groovy.lang.Binding;
16  import groovy.lang.GroovyClassLoader;
17  import groovy.lang.GroovyShell;
18  import groovy.lang.Script;
19  
20  import org.codehaus.groovy.control.CompilerConfiguration;
21  
22  import com.eviware.soapui.support.StringUtils;
23  import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
24  
25  /***
26   * A Groovy ScriptEngine 
27   * 
28   * @author ole.matzura
29   */
30  
31  public class SoapUIGroovyScriptEngine implements SoapUIScriptEngine
32  {
33  	private GroovyClassLoader classLoader;
34  	private GroovyShell shell;
35  	private Binding binding;
36  	private Script script;
37  	private String scriptText;
38  
39  	public SoapUIGroovyScriptEngine( ClassLoader parentClassLoader )
40  	{
41  		classLoader = new GroovyClassLoader( parentClassLoader );
42  		binding = new Binding();
43  		CompilerConfiguration config = new CompilerConfiguration();
44  		config.setDebug( true );
45  		config.setVerbose( true );
46  		shell = new GroovyShell( classLoader, binding, config );
47  	}
48  
49  	public synchronized Object run() throws Exception
50  	{
51  		if( StringUtils.isNullOrEmpty( scriptText ))
52  			return null;
53  		
54  		if( script == null )
55  		{
56  			compile();
57  		}
58  		
59  		return script.run();
60  	}
61  
62  	public synchronized void setScript( String scriptText )
63  	{
64  		if( scriptText != null && scriptText.equals( this.scriptText ))
65  			return;
66  		
67  		if( script != null )
68  		{
69  			script.setBinding( null );
70  			script = null;
71  			
72  			if( shell != null )
73  				shell.resetLoadedClasses();
74  			
75  			classLoader.clearCache();			
76  		}
77  		
78  		this.scriptText = scriptText;
79  	}
80  	
81  	public void compile() throws Exception
82  	{
83  		if( script == null )
84  		{
85  			script = shell.parse( scriptText );
86  			script.setBinding( binding );
87  		}
88  	}
89  
90  	public void setVariable( String name, Object value )
91  	{
92  		binding.setVariable( name, value );
93  	}
94  
95  	public void clearVariables()
96  	{
97  		if( binding != null )
98  			binding.getVariables().clear();
99  	}
100 
101 	public void release()
102 	{
103 		script = null;
104 		
105 		if( binding != null )
106 		{
107 			binding.getVariables().clear();
108 			binding = null;
109 		}
110 		
111 		if( shell != null )
112 		{
113 			shell.resetLoadedClasses();
114 			shell = null;
115 		}
116 	}
117 	
118 	protected Binding getBinding()
119 	{
120 		return binding;
121 	}
122 	
123 	protected GroovyClassLoader getClassLoader()
124 	{
125 		return classLoader;
126 	}
127 
128 	protected synchronized void reset()
129 	{
130 		script = null;
131 	}
132 
133 	protected Script getScript()
134 	{
135 		return script;
136 	}
137 
138 	protected String getScriptText()
139 	{
140 		return scriptText;
141 	}
142 
143 	protected GroovyShell getShell()
144 	{
145 		return shell;
146 	}
147 }