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