View | Details | Raw Unified | Return to bug 53365
Collapse All | Expand All

(-)src/core/org/apache/jmeter/util/JSR223TestElement.java (-17 / +28 lines)
Lines 43-48 Link Here
43
public abstract class JSR223TestElement extends AbstractTestElement
43
public abstract class JSR223TestElement extends AbstractTestElement
44
    implements Serializable, Cloneable
44
    implements Serializable, Cloneable
45
{
45
{
46
    /**
47
     * Initialization On Demand Holder pattern
48
     */
49
    private static class LazyHolder {
50
        public static final ScriptEngineManager INSTANCE = new ScriptEngineManager();
51
    }
52
 
53
    /**
54
     * @return ScriptEngineManager singleton
55
     */
56
    public static ScriptEngineManager getInstance() {
57
            return LazyHolder.INSTANCE;
58
    }
59
    
46
    private static final long serialVersionUID = 233L;
60
    private static final long serialVersionUID = 233L;
47
61
48
    //++ For TestBean implementations only
62
    //++ For TestBean implementations only
Lines 79-91 Link Here
79
       return o;
93
       return o;
80
    }
94
    }
81
95
82
    protected ScriptEngineManager getManager() {
96
    protected ScriptEngine getScriptEngine() throws ScriptException {
83
        ScriptEngineManager sem = new ScriptEngineManager();
97
        final String lang = getScriptLanguage();
84
        initManager(sem);
98
85
        return sem;
99
        ScriptEngine scriptEngine = getInstance().getEngineByName(lang);
100
        if (scriptEngine == null) {
101
            throw new ScriptException("Cannot find engine named: "+lang);
102
        }
103
104
        initScriptEngine(scriptEngine);
105
        return scriptEngine;
86
    }
106
    }
87
107
88
    protected void initManager(ScriptEngineManager sem) {
108
    protected void initScriptEngine(ScriptEngine sem) {
89
        final String label = getName();
109
        final String label = getName();
90
        final String fileName = getFilename();
110
        final String fileName = getFilename();
91
        final String scriptParameters = getParameters();
111
        final String scriptParameters = getParameters();
Lines 116-130 Link Here
116
    }
136
    }
117
137
118
138
119
    protected Object processFileOrScript(ScriptEngineManager sem) throws IOException, ScriptException {
139
    protected Object processFileOrScript(ScriptEngine scriptEngine) throws IOException, ScriptException {
120
140
        File scriptFile = new File(getFilename());        
121
        final String lang = getScriptLanguage();
122
        ScriptEngine scriptEngine = sem.getEngineByName(lang);
123
        if (scriptEngine == null) {
124
            throw new ScriptException("Cannot find engine named: "+lang);
125
        }
126
127
        File scriptFile = new File(getFilename());
128
        if (scriptFile.exists()) {
141
        if (scriptFile.exists()) {
129
            BufferedReader fileReader = null;
142
            BufferedReader fileReader = null;
130
            try {
143
            try {
Lines 136-142 Link Here
136
        } else {
149
        } else {
137
            return scriptEngine.eval(getScript());
150
            return scriptEngine.eval(getScript());
138
        }
151
        }
139
140
    }
152
    }
141
153
142
    /**
154
    /**
Lines 184-188 Link Here
184
    public void setScriptLanguage(String s) {
196
    public void setScriptLanguage(String s) {
185
        scriptLanguage = s;
197
        scriptLanguage = s;
186
    }
198
    }
187
199
}
188
}
(-)src/components/org/apache/jmeter/extractor/JSR223PostProcessor.java (-7 / +5 lines)
Lines 20-26 Link Here
20
20
21
import java.io.IOException;
21
import java.io.IOException;
22
22
23
import javax.script.ScriptEngineManager;
23
import javax.script.ScriptEngine;
24
import javax.script.ScriptException;
24
import javax.script.ScriptException;
25
25
26
import org.apache.jmeter.processor.PostProcessor;
26
import org.apache.jmeter.processor.PostProcessor;
Lines 36-50 Link Here
36
    private static final long serialVersionUID = 232L;
36
    private static final long serialVersionUID = 232L;
37
37
38
    public void process() {
38
    public void process() {
39
40
        try {
39
        try {
41
            ScriptEngineManager sem = getManager();
40
            ScriptEngine scriptEngine = getScriptEngine();
42
            if(sem == null) { return; }
41
            processFileOrScript(scriptEngine);
43
            processFileOrScript(sem);
44
        } catch (ScriptException e) {
42
        } catch (ScriptException e) {
45
            log.warn("Problem in JSR223 script "+e);
43
            log.warn("Problem in JSR223 script "+e, e);
46
        } catch (IOException e) {
44
        } catch (IOException e) {
47
            log.warn("Problem in JSR223 script "+e);
45
            log.warn("Problem in JSR223 script "+e, e);
48
        }
46
        }
49
    }
47
    }
50
}
48
}
(-)src/components/org/apache/jmeter/assertions/JSR223Assertion.java (-13 / +7 lines)
Lines 20-26 Link Here
20
20
21
import java.io.IOException;
21
import java.io.IOException;
22
22
23
import javax.script.ScriptEngineManager;
23
import javax.script.ScriptEngine;
24
import javax.script.ScriptException;
24
import javax.script.ScriptException;
25
25
26
import org.apache.jmeter.samplers.SampleResult;
26
import org.apache.jmeter.samplers.SampleResult;
Lines 38-60 Link Here
38
    public AssertionResult getResult(SampleResult response) {
38
    public AssertionResult getResult(SampleResult response) {
39
        AssertionResult result = new AssertionResult(getName());
39
        AssertionResult result = new AssertionResult(getName());
40
        try {
40
        try {
41
            ScriptEngineManager mgr = getManager();
41
            ScriptEngine scriptEngine = getScriptEngine();
42
            if (mgr == null) {
42
            scriptEngine.put("SampleResult", response);
43
                result.setFailure(true);
43
            scriptEngine.put("AssertionResult", result);
44
                result.setError(true);
44
            processFileOrScript(scriptEngine);
45
                result.setFailureMessage("JSR223 Manager not found");
46
                return result;
47
            }
48
            mgr.put("SampleResult", response);
49
            mgr.put("AssertionResult", result);
50
            processFileOrScript(mgr);
51
            result.setError(false);
45
            result.setError(false);
52
        } catch (IOException e) {
46
        } catch (IOException e) {
53
            log.warn("Problem in JSR223 script "+e);
47
            log.warn("Problem in JSR223 script "+e, e);
54
            result.setError(true);
48
            result.setError(true);
55
            result.setFailureMessage(e.toString());
49
            result.setFailureMessage(e.toString());
56
        } catch (ScriptException e) {
50
        } catch (ScriptException e) {
57
            log.warn("Problem in JSR223 script "+e);
51
            log.warn("Problem in JSR223 script "+e, e);
58
            result.setError(true);
52
            result.setError(true);
59
            result.setFailureMessage(e.toString());
53
            result.setFailureMessage(e.toString());
60
        }
54
        }
(-)src/components/org/apache/jmeter/visualizers/JSR223Listener.java (-8 / +7 lines)
Lines 20-26 Link Here
20
20
21
import java.io.IOException;
21
import java.io.IOException;
22
22
23
import javax.script.ScriptEngineManager;
23
import javax.script.ScriptEngine;
24
import javax.script.ScriptException;
24
import javax.script.ScriptException;
25
25
26
import org.apache.jmeter.samplers.SampleEvent;
26
import org.apache.jmeter.samplers.SampleEvent;
Lines 41-55 Link Here
41
41
42
    public void sampleOccurred(SampleEvent event) {
42
    public void sampleOccurred(SampleEvent event) {
43
        try {
43
        try {
44
            ScriptEngineManager sem = getManager();
44
            ScriptEngine scriptEngine = getScriptEngine();
45
            if (sem == null) { return; }
45
            scriptEngine.put("sampleEvent", event);
46
            sem.put("sampleEvent", event);
46
            scriptEngine.put("sampleResult", event.getResult());
47
            sem.put("sampleResult", event.getResult());
47
            processFileOrScript(scriptEngine);
48
            processFileOrScript(sem);
49
        } catch (ScriptException e) {
48
        } catch (ScriptException e) {
50
            log.warn("Problem in JSR223 script "+e);
49
            log.warn("Problem in JSR223 script "+e, e);
51
        } catch (IOException e) {
50
        } catch (IOException e) {
52
            log.warn("Problem in JSR223 script "+e);
51
            log.warn("Problem in JSR223 script "+e, e);
53
        }
52
        }
54
    }
53
    }
55
54
(-)src/components/org/apache/jmeter/modifiers/JSR223PreProcessor.java (-7 / +5 lines)
Lines 20-26 Link Here
20
20
21
import java.io.IOException;
21
import java.io.IOException;
22
22
23
import javax.script.ScriptEngineManager;
23
import javax.script.ScriptEngine;
24
import javax.script.ScriptException;
24
import javax.script.ScriptException;
25
25
26
import org.apache.jmeter.processor.PreProcessor;
26
import org.apache.jmeter.processor.PreProcessor;
Lines 36-50 Link Here
36
    private static final long serialVersionUID = 232L;
36
    private static final long serialVersionUID = 232L;
37
37
38
    public void process() {
38
    public void process() {
39
40
        try {
39
        try {
41
            ScriptEngineManager sem = getManager();
40
            ScriptEngine scriptEngine = getScriptEngine();
42
            if(sem == null) { return; }
41
            processFileOrScript(scriptEngine);
43
            processFileOrScript(sem);
44
        } catch (ScriptException e) {
42
        } catch (ScriptException e) {
45
            log.warn("Problem in JSR223 script "+e);
43
            log.warn("Problem in JSR223 script "+e, e);
46
        } catch (IOException e) {
44
        } catch (IOException e) {
47
            log.warn("Problem in JSR223 script "+e);
45
            log.warn("Problem in JSR223 script "+e, e);
48
        }
46
        }
49
    }
47
    }
50
}
48
}
(-)src/components/org/apache/jmeter/timers/JSR223Timer.java (-9 / +6 lines)
Lines 20-26 Link Here
20
20
21
import java.io.IOException;
21
import java.io.IOException;
22
22
23
import javax.script.ScriptEngineManager;
23
import javax.script.ScriptEngine;
24
import javax.script.ScriptException;
24
import javax.script.ScriptException;
25
25
26
import org.apache.jmeter.testbeans.TestBean;
26
import org.apache.jmeter.testbeans.TestBean;
Lines 37-58 Link Here
37
    public long delay() {
37
    public long delay() {
38
        long delay = 0;
38
        long delay = 0;
39
        try {
39
        try {
40
            ScriptEngineManager mgr = getManager();
40
            ScriptEngine scriptEngine = getScriptEngine();
41
            if (mgr == null) {
41
            Object o = processFileOrScript(scriptEngine);
42
                return 0; 
43
            }
44
            Object o = processFileOrScript(mgr);
45
            if (o == null) {
42
            if (o == null) {
46
                log.warn("Script did not return a value");
43
                log.warn("Script did not return a value");
47
                return 0;
44
                return 0;
48
            }
45
            }
49
            delay = Long.valueOf(o.toString()).longValue();
46
            delay = Long.valueOf(o.toString()).longValue();
50
        } catch (NumberFormatException e) {
47
        } catch (NumberFormatException e) {
51
            log.warn("Problem in JSR223 script "+e);
48
            log.warn("Problem in JSR223 script "+e, e);
52
        } catch (IOException e) {
49
        } catch (IOException e) {
53
            log.warn("Problem in JSR223 script "+e);
50
            log.warn("Problem in JSR223 script "+e, e);
54
        } catch (ScriptException e) {
51
        } catch (ScriptException e) {
55
            log.warn("Problem in JSR223 script "+e);
52
            log.warn("Problem in JSR223 script "+e, e);
56
        }
53
        }
57
        return delay;
54
        return delay;
58
    }
55
    }
(-)src/protocol/java/org/apache/jmeter/protocol/java/sampler/JSR223Sampler.java (-12 / +6 lines)
Lines 23-29 Link Here
23
import java.util.HashSet;
23
import java.util.HashSet;
24
import java.util.Set;
24
import java.util.Set;
25
25
26
import javax.script.ScriptEngineManager;
26
import javax.script.ScriptEngine;
27
import javax.script.ScriptException;
27
import javax.script.ScriptException;
28
28
29
import org.apache.jmeter.config.ConfigTestElement;
29
import org.apache.jmeter.config.ConfigTestElement;
Lines 58-72 Link Here
58
        result.setDataType(SampleResult.TEXT);
58
        result.setDataType(SampleResult.TEXT);
59
        result.sampleStart();
59
        result.sampleStart();
60
        try {
60
        try {
61
            ScriptEngineManager mgr = getManager();
61
            ScriptEngine scriptEngine = getScriptEngine();
62
            if (mgr == null) {
62
            scriptEngine.put("SampleResult",result);
63
                result.setSuccessful(false);
63
            Object ret = processFileOrScript(scriptEngine);
64
                result.setResponseCode("500"); // $NON-NLS-1$
65
                result.setResponseMessage("Could not instantiate ScriptManager");
66
                return result;
67
            }
68
            mgr.put("SampleResult",result);
69
            Object ret = processFileOrScript(mgr);
70
            result.setSuccessful(true);
64
            result.setSuccessful(true);
71
            result.setResponseCodeOK();
65
            result.setResponseCodeOK();
72
            result.setResponseMessageOK();
66
            result.setResponseMessageOK();
Lines 74-85 Link Here
74
                result.setResponseData(ret.toString(), null);
68
                result.setResponseData(ret.toString(), null);
75
            }
69
            }
76
        } catch (IOException e) {
70
        } catch (IOException e) {
77
            log.warn("Problem in JSR223 script "+e);
71
            log.warn("Problem in JSR223 script "+e, e);
78
            result.setSuccessful(false);
72
            result.setSuccessful(false);
79
            result.setResponseCode("500"); // $NON-NLS-1$
73
            result.setResponseCode("500"); // $NON-NLS-1$
80
            result.setResponseMessage(e.toString());
74
            result.setResponseMessage(e.toString());
81
        } catch (ScriptException e) {
75
        } catch (ScriptException e) {
82
            log.warn("Problem in JSR223 script "+e);
76
            log.warn("Problem in JSR223 script "+e, e);
83
            result.setSuccessful(false);
77
            result.setSuccessful(false);
84
            result.setResponseCode("500"); // $NON-NLS-1$
78
            result.setResponseCode("500"); // $NON-NLS-1$
85
            result.setResponseMessage(e.toString());
79
            result.setResponseMessage(e.toString());

Return to bug 53365