Skip to content

Commit 07315b7

Browse files
committed
SLING-12516 - Migrate SlingContext to Rhino Context(ContextFactory) API
* Replaced the deprecated Rhino Context() API with the updated Context(ContextFactory) API. * Eliminated dependency on the Rhino global static singleton context factory.
1 parent fed1756 commit 07315b7

File tree

3 files changed

+13
-64
lines changed

3 files changed

+13
-64
lines changed

src/main/java/org/apache/sling/scripting/javascript/helper/SlingContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.sling.scripting.javascript.helper;
2020

2121
import org.mozilla.javascript.Context;
22+
import org.mozilla.javascript.ContextFactory;
2223
import org.mozilla.javascript.ImporterTopLevel;
2324
import org.mozilla.javascript.ScriptableObject;
2425

@@ -29,6 +30,10 @@
2930
*/
3031
public class SlingContext extends Context {
3132

33+
protected SlingContext( ContextFactory factory ) {
34+
super( factory );
35+
}
36+
3237
@Override
3338
public ScriptableObject initStandardObjects(ScriptableObject scope,
3439
boolean sealed) {

src/main/java/org/apache/sling/scripting/javascript/helper/SlingContextFactory.java

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
*/
1919
package org.apache.sling.scripting.javascript.helper;
2020

21-
import java.lang.reflect.Field;
22-
2321
import org.mozilla.javascript.Context;
2422
import org.mozilla.javascript.ContextFactory;
2523
import org.mozilla.javascript.tools.debugger.ScopeProvider;
@@ -45,47 +43,14 @@ public class SlingContextFactory extends ContextFactory {
4543

4644
private int languageVersion;
4745

48-
// conditionally setup the global ContextFactory to be ours. If
49-
// a global context factory has already been set, we have lost
50-
// and cannot set this one.
51-
public static void setup(ScopeProvider sp, int languageVersion) {
52-
// TODO what do we do in the other case? debugger won't work
53-
if (!hasExplicitGlobal()) {
54-
initGlobal(new SlingContextFactory(sp,
55-
Context.isValidLanguageVersion(languageVersion) ? languageVersion : Context.VERSION_DEFAULT));
56-
}
57-
}
58-
59-
public static void teardown() {
60-
ContextFactory factory = getGlobal();
61-
if (factory instanceof SlingContextFactory) {
62-
((SlingContextFactory) factory).dispose();
63-
}
64-
}
65-
66-
// private as instances of this class are only used by setup()
67-
private SlingContextFactory(ScopeProvider sp, int languageVersion) {
46+
public SlingContextFactory(ScopeProvider sp, int languageVersion) {
6847
scopeProvider = sp;
6948
this.languageVersion = languageVersion;
7049
}
7150

72-
private void dispose() {
73-
// ensure the debugger is closed
74-
exitDebugger();
75-
76-
// reset the context factory class for future use
77-
ContextFactory newGlobal = new ContextFactory();
78-
setField(newGlobal, "hasCustomGlobal", Boolean.FALSE);
79-
setField(newGlobal, "global", newGlobal);
80-
setField(newGlobal, "sealed", Boolean.FALSE);
81-
setField(newGlobal, "listeners", null);
82-
setField(newGlobal, "disabledListening", Boolean.FALSE);
83-
setField(newGlobal, "applicationClassLoader", null);
84-
}
85-
8651
@Override
8752
protected Context makeContext() {
88-
Context context = new SlingContext();
53+
Context context = new SlingContext(this);
8954
context.setLanguageVersion(languageVersion);
9055
return context;
9156
}
@@ -142,19 +107,4 @@ public boolean isDebugging() {
142107
return debuggerActive;
143108
}
144109

145-
private void setField(Object instance, String fieldName, Object value) {
146-
try {
147-
Field field = instance.getClass().getDeclaredField(fieldName);
148-
if (!field.isAccessible()) {
149-
field.setAccessible(true);
150-
}
151-
field.set(instance, value);
152-
} catch (IllegalArgumentException iae) {
153-
// don't care, but it is strange anyhow
154-
} catch (IllegalAccessException iae) {
155-
// don't care, but it is strange anyhow
156-
} catch (NoSuchFieldException nsfe) {
157-
// don't care, but it is strange anyhow
158-
}
159-
}
160110
}

src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactory.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ public class RhinoJavaScriptEngineFactory extends AbstractScriptEngineFactory im
134134

135135
private final Set<RhinoHostObjectProvider> hostObjectProvider = new HashSet<RhinoHostObjectProvider>();
136136

137+
private SlingContextFactory contextFactory;
138+
137139
@Reference
138140
private DynamicClassLoaderManager dynamicClassLoaderManager = null;
139141

@@ -251,10 +253,7 @@ private void dropRootScope() {
251253
// ensure the debugger is closed if the root scope will
252254
// be replaced to ensure no references to the old scope
253255
// and context remain
254-
ContextFactory contextFactory = ContextFactory.getGlobal();
255-
if (contextFactory instanceof SlingContextFactory) {
256-
((SlingContextFactory) contextFactory).exitDebugger();
257-
}
256+
contextFactory.exitDebugger();
258257

259258
// drop the scope
260259
rootScope = null;
@@ -304,18 +303,15 @@ protected void activate(final ComponentContext context, final RhinoJavaScriptEng
304303
wrapFactory = new SlingWrapFactory();
305304

306305
// initialize the Rhino Context Factory
307-
SlingContextFactory.setup(this, RHINO_LANGUAGE_VERSION);
306+
contextFactory = new SlingContextFactory(this, RHINO_LANGUAGE_VERSION);
307+
contextFactory.setDebugging(debugging);
308308

309309
setEngineName(getEngineName() + " (Rhino " + (rhinoVersion != null ? rhinoVersion : "unknown") + ")");
310310

311311
setExtensions(PropertiesUtil.toStringArray(props.get("extensions")));
312312
setMimeTypes(PropertiesUtil.toStringArray(props.get("mimeTypes")));
313313
setNames(PropertiesUtil.toStringArray(props.get("names")));
314314

315-
final ContextFactory contextFactory = ContextFactory.getGlobal();
316-
if (contextFactory instanceof SlingContextFactory) {
317-
((SlingContextFactory) contextFactory).setDebugging(debugging);
318-
}
319315
// set the dynamic class loader as the application class loader
320316
final DynamicClassLoaderManager dclm = this.dynamicClassLoaderManager;
321317
if (dclm != null) {
@@ -337,10 +333,8 @@ protected void deactivate(ComponentContext context) {
337333
// remove the root scope
338334
dropRootScope();
339335

340-
// remove our context factory
341-
SlingContextFactory.teardown();
342-
343336
// remove references
337+
contextFactory = null;
344338
wrapFactory = null;
345339
hostObjectProvider.clear();
346340

0 commit comments

Comments
 (0)