01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * 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: public static SoapUIGroovyShell createGsroovyShell(Binding binding) {
25: // LoaderConfiguration config = new LoaderConfiguration();
26: //
27: // String libraries = SoapUI.getSettings().getString( ToolsSettings.SCRIPT_LIBRARIES, null );
28: // if( libraries != null )
29: // {
30: // File libs = new File( libraries );
31: // File[] list = libs.listFiles();
32: //
33: // for( File lib : list)
34: // {
35: // if( lib.getName().toLowerCase().endsWith( ".jar" ))
36: // {
37: // config.addFile( lib );
38: // }
39: // }
40: // }
41:
42: // RootLoader loader = new RootLoader( config.getClassPathUrls(), );
43: GroovyClassLoader groovyClassLoader = new GroovyClassLoader(
44: SoapUI.class.getClassLoader());
45: SoapUIGroovyShell groovyShell = binding == null ? new SoapUIGroovyShell(
46: groovyClassLoader)
47: : new SoapUIGroovyShell(groovyClassLoader, binding);
48:
49: return groovyShell;
50: }
51:
52: public static class SoapUIGroovyShell extends GroovyShell {
53: private final GroovyClassLoader classLoader;
54:
55: protected SoapUIGroovyShell(GroovyClassLoader classLoader,
56: Binding binding) {
57: super (classLoader, binding);
58:
59: this .classLoader = classLoader;
60:
61: reloadExternalClasses();
62: }
63:
64: protected SoapUIGroovyShell(GroovyClassLoader classLoader) {
65: super (classLoader);
66:
67: this .classLoader = classLoader;
68:
69: reloadExternalClasses();
70: }
71:
72: public void reloadExternalClasses() {
73: resetLoadedClasses();
74: classLoader.clearCache();
75:
76: try {
77: File scripts = new File(new File("").getAbsolutePath()
78: + File.separatorChar + "scripts");
79: if (scripts.exists() && scripts.isDirectory()) {
80: File[] listFiles = scripts.listFiles();
81: for (File file : listFiles) {
82: if (file.isDirectory()
83: || !file.getName().endsWith(".groovy"))
84: continue;
85:
86: System.out.println("parsing "
87: + file.getAbsolutePath());
88: classLoader.parseClass(file);
89: }
90: }
91: } catch (Exception e) {
92: SoapUI.logError(e);
93: }
94: }
95: }
96: }
|