RhinoScriptProcessor.java
4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.logging.Log
* org.apache.commons.logging.LogFactory
* org.mozilla.javascript.Context
* org.mozilla.javascript.Script
* org.mozilla.javascript.Scriptable
* org.mozilla.javascript.ScriptableObject
*/
package com.adobe.aemds.guide.utils.rhino;
import com.adobe.aemds.guide.service.GuideException;
import com.adobe.aemds.guide.utils.rhino.RhinoValueConverter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
public class RhinoScriptProcessor {
private static final Log logger = LogFactory.getLog(RhinoScriptProcessor.class);
private static final Map<String, Script> scriptCache = new ConcurrentHashMap<String, Script>(25);
private static final int RHINO_COMPILER_OPTIMIZATION_LEVEL = 3;
public static void reset() {
scriptCache.clear();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private static Script checkAndAddInCache(String scriptContent, String name) throws Exception {
Script script = scriptCache.get(name);
if (script == null) {
Context cx = Context.enter();
cx.setOptimizationLevel(3);
try {
script = cx.compileString(scriptContent, name, 1, (Object)null);
scriptCache.put(name, script);
}
catch (Exception e) {
logger.error((Object)("Unable to compile script " + name + " with optimization level" + 3), (Throwable)e);
}
finally {
Context.exit();
}
}
return script;
}
public static void compileAndCache(String scriptContent, String name) {
if (scriptContent == null) {
return;
}
try {
RhinoScriptProcessor.checkAndAddInCache(scriptContent, name);
}
catch (Exception ex) {
logger.error((Object)("Failed to compile and cache the script '" + name + "': " + ex.getMessage()), (Throwable)ex);
}
}
public static Scriptable execute(String scriptContent, String name, Scriptable scope) {
if (scriptContent == null) {
return scope;
}
try {
Script script = RhinoScriptProcessor.checkAndAddInCache(scriptContent, name);
RhinoScriptProcessor.executeCompiledScript(script, name, scope);
}
catch (Exception ex) {
logger.error((Object)("Failed to execute script '" + name + "': " + ex.getMessage()), (Throwable)ex);
}
return scope;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static Object interpret(String scriptContent, String name, Scriptable givenScope) {
Object result = null;
Context cx = Context.enter();
cx.setOptimizationLevel(-1);
try {
result = cx.evaluateString(givenScope, scriptContent, name, 1, (Object)null);
}
catch (Exception ex) {
logger.error((Object)("Failed to execute script '" + name + "': " + ex.getMessage()), (Throwable)ex);
}
finally {
Context.exit();
}
return RhinoValueConverter.convertValueForJava(result, givenScope);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private static Object executeCompiledScript(Script script, String name, Scriptable givenScope) throws GuideException {
Context cx = Context.enter();
cx.setOptimizationLevel(-1);
Object result = null;
try {
result = script.exec(cx, givenScope);
}
catch (Exception ex) {
logger.error((Object)("Unable to execute compiled script '" + name + "': " + ex.getMessage()), (Throwable)ex);
}
finally {
Context.exit();
}
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static Scriptable getRhinoScope() {
Context cx = null;
ScriptableObject scope = null;
cx = Context.enter();
try {
scope = cx.initStandardObjects(null, false);
}
finally {
Context.exit();
}
return scope;
}
}