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.scripting;
14:
15: import java.util.Stack;
16:
17: import com.eviware.soapui.SoapUI;
18: import com.eviware.soapui.model.ModelItem;
19:
20: /**
21: * A pool of script engines
22: *
23: * @author ole.matzura
24: */
25:
26: public class ScriptEnginePool {
27: private Stack<SoapUIScriptEngine> scriptEngines = new Stack<SoapUIScriptEngine>();
28: private String script;
29: private ModelItem modelItem;
30: private int borrowed;
31:
32: public ScriptEnginePool(ModelItem modelItem) {
33: this .modelItem = modelItem;
34: }
35:
36: public void setScript(String script) {
37: this .script = script;
38: }
39:
40: public void returnScriptEngine(SoapUIScriptEngine scriptEngine) {
41: synchronized (this ) {
42: scriptEngines.push(scriptEngine);
43: borrowed--;
44: }
45: }
46:
47: public SoapUIScriptEngine getScriptEngine() {
48: synchronized (this ) {
49: if (scriptEngines.isEmpty())
50: scriptEngines.push(SoapUIScriptEngineRegistry
51: .create(SoapUIScriptEngineRegistry.GROOVY_ID,
52: modelItem));
53:
54: SoapUIScriptEngine result = scriptEngines.pop();
55: result.setScript(script);
56: borrowed++;
57:
58: return result;
59: }
60: }
61:
62: public void release() {
63: int waitcount = 10;
64:
65: while (borrowed > 0 && waitcount-- > 0) {
66: try {
67: System.out.println("Waiting for " + borrowed
68: + " script engines");
69: Thread.sleep(1000);
70: } catch (InterruptedException e) {
71: SoapUI.logError(e);
72: }
73: }
74:
75: for (SoapUIScriptEngine scriptEngine : scriptEngines) {
76: scriptEngine.release();
77: }
78:
79: scriptEngines.clear();
80:
81: if (borrowed > 0)
82: System.out.println("Failed to release " + borrowed
83: + " script engines");
84: }
85: }
|