1
2
3
4
5
6
7
8
9
10
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 {
28 private Stack<SoapUIScriptEngine> scriptEngines = new Stack<SoapUIScriptEngine>();
29 private String script;
30 private ModelItem modelItem;
31 private int borrowed;
32 private String id;
33
34 public ScriptEnginePool( ModelItem modelItem )
35 {
36 this.modelItem = modelItem;
37 }
38
39 public ScriptEnginePool( String id )
40 {
41 this.id = id;
42 }
43
44 public void setScript( String script )
45 {
46 this.script = script;
47 }
48
49 public void returnScriptEngine( SoapUIScriptEngine scriptEngine )
50 {
51 synchronized( this )
52 {
53 scriptEngines.push( scriptEngine );
54 borrowed-- ;
55 }
56 }
57
58 public SoapUIScriptEngine getScriptEngine()
59 {
60 synchronized( this )
61 {
62 if( scriptEngines.isEmpty() )
63 {
64 if( modelItem != null )
65 scriptEngines.push( SoapUIScriptEngineRegistry.create( modelItem ) );
66 else
67 scriptEngines.push( SoapUIScriptEngineRegistry.getFactory( id ).createScriptEngine( null ));
68 }
69
70 SoapUIScriptEngine result = scriptEngines.pop();
71 if( script != null )
72 result.setScript( script );
73
74 borrowed++ ;
75
76 return result;
77 }
78 }
79
80 public void release()
81 {
82 int waitcount = 10;
83
84 while( borrowed > 0 && waitcount-- > 0 )
85 {
86 try
87 {
88 System.out.println( "Waiting for " + borrowed + " script engines" );
89 Thread.sleep( 1000 );
90 }
91 catch( InterruptedException e )
92 {
93 SoapUI.logError( e );
94 }
95 }
96
97 for( SoapUIScriptEngine scriptEngine : scriptEngines )
98 {
99 scriptEngine.release();
100 }
101
102 scriptEngines.clear();
103
104 if( borrowed > 0 )
105 System.out.println( "Failed to release " + borrowed + " script engines" );
106 }
107 }