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