27
27
28
28
import org .apache .commons .logging .Log ;
29
29
import org .apache .commons .logging .LogFactory ;
30
+ import org .springframework .beans .factory .annotation .Value ;
30
31
import org .springframework .context .ApplicationContext ;
31
32
import org .springframework .context .annotation .Bean ;
32
33
import org .springframework .context .annotation .Configuration ;
38
39
public class ShNashornEngineConfiguration {
39
40
private static final Log logger = LogFactory .getLog (ShNashornEngineConfiguration .class );
40
41
42
+ private static final String NASHORN_CLASS = "org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory" ;
43
+ private static final String RHINO_CLASS = "org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory" ;
44
+ private static final String NASHORN_ENGINE = "nashorn" ;
45
+
46
+ @ Value ("${shio.website.javascript.engine:nashorn}" )
47
+ private String defaultEngine ;
48
+
41
49
@ Resource
42
50
private ApplicationContext context ;
43
51
44
52
@ Bean
45
53
public ScriptEngine scriptEngine (ShNashornEngineBindings shBindings ) {
46
54
47
- Class <?> nashornScriptEngineFactory ;
48
55
try {
56
+ ScriptEngine engine = defaultEngine .equals (NASHORN_ENGINE ) ? nashornEngine (shBindings ) : rhinoEngine ();
49
57
50
- nashornScriptEngineFactory = Class .forName ("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory" );
51
-
52
- Method getScriptEngine = nashornScriptEngineFactory .getDeclaredMethod ("getScriptEngine" , String [].class );
53
- ScriptEngineFactory scriptEngineFactory = (ScriptEngineFactory ) nashornScriptEngineFactory
54
- .getDeclaredConstructor ().newInstance ();
55
- ScriptEngine engine = (ScriptEngine ) getScriptEngine .invoke (scriptEngineFactory ,
56
- shBindings .getShWebsiteProperties ().getNashornAsObject ());
57
58
Bindings bindings = engine .createBindings ();
58
59
59
60
bindings .put ("shNavigationComponent" , shBindings .getShNavigationComponent ());
@@ -75,4 +76,27 @@ public ScriptEngine scriptEngine(ShNashornEngineBindings shBindings) {
75
76
return null ;
76
77
}
77
78
79
+ private ScriptEngine rhinoEngine () throws ClassNotFoundException , NoSuchMethodException , InstantiationException ,
80
+ IllegalAccessException , InvocationTargetException {
81
+ ScriptEngine engine ;
82
+ Class <?> rhinoScriptEngineFactory = Class .forName (RHINO_CLASS );
83
+ Method getScriptEngine = rhinoScriptEngineFactory .getDeclaredMethod ("getScriptEngine" );
84
+ ScriptEngineFactory scriptEngineFactory = (ScriptEngineFactory ) rhinoScriptEngineFactory
85
+ .getDeclaredConstructor ().newInstance ();
86
+ engine = (ScriptEngine ) getScriptEngine .invoke (scriptEngineFactory );
87
+ return engine ;
88
+ }
89
+
90
+ private ScriptEngine nashornEngine (ShNashornEngineBindings shBindings ) throws ClassNotFoundException ,
91
+ NoSuchMethodException , InstantiationException , IllegalAccessException , InvocationTargetException {
92
+ ScriptEngine engine ;
93
+ Class <?> nashornScriptEngineFactory = Class .forName (NASHORN_CLASS );
94
+ Method getScriptEngine = nashornScriptEngineFactory .getDeclaredMethod ("getScriptEngine" , String [].class );
95
+ ScriptEngineFactory scriptEngineFactory = (ScriptEngineFactory ) nashornScriptEngineFactory
96
+ .getDeclaredConstructor ().newInstance ();
97
+ engine = (ScriptEngine ) getScriptEngine .invoke (scriptEngineFactory ,
98
+ shBindings .getShWebsiteProperties ().getNashornAsObject ());
99
+ return engine ;
100
+ }
101
+
78
102
}
0 commit comments