Search in sources :

Example 1 with RhinoHostObjectProvider

use of org.apache.sling.scripting.javascript.RhinoHostObjectProvider in project sling by apache.

the class RhinoJavaScriptEngineFactory method getRootScope.

@SuppressWarnings("unchecked")
private Scriptable getRootScope() {
    if (rootScope == null) {
        final Context rhinoContext = Context.enter();
        try {
            rhinoContext.setOptimizationLevel(optimizationLevel);
            Scriptable tmpScope = rhinoContext.initStandardObjects(new ImporterTopLevel(rhinoContext), false);
            // default classes
            addHostObjects(tmpScope, (Class<? extends ScriptableObject>[]) HOSTOBJECT_CLASSES);
            // provided classes
            for (RhinoHostObjectProvider provider : hostObjectProvider) {
                addHostObjects(tmpScope, provider.getHostObjectClasses());
                addImportedClasses(rhinoContext, tmpScope, provider.getImportedClasses());
                addImportedPackages(rhinoContext, tmpScope, provider.getImportedPackages());
            }
            // only assign the root scope when complete set up
            rootScope = tmpScope;
        } finally {
            // ensure the context is exited after setting up the
            // the new root scope
            Context.exit();
        }
    }
    return rootScope;
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) Context(org.mozilla.javascript.Context) BundleContext(org.osgi.framework.BundleContext) ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoHostObjectProvider(org.apache.sling.scripting.javascript.RhinoHostObjectProvider) NativeJavaClass(org.mozilla.javascript.NativeJavaClass) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

RhinoHostObjectProvider (org.apache.sling.scripting.javascript.RhinoHostObjectProvider)1 Context (org.mozilla.javascript.Context)1 ImporterTopLevel (org.mozilla.javascript.ImporterTopLevel)1 NativeJavaClass (org.mozilla.javascript.NativeJavaClass)1 Scriptable (org.mozilla.javascript.Scriptable)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1 BundleContext (org.osgi.framework.BundleContext)1 ComponentContext (org.osgi.service.component.ComponentContext)1