Search in sources :

Example 1 with ClassShutter

use of org.mozilla.javascript.ClassShutter in project neo4j by neo4j.

the class GlobalJavascriptInitializer method initialize.

public static synchronized void initialize(Mode requestedMode) {
    if (initializationMode != null) {
        if (initializationMode == requestedMode) {
            return;
        } else {
            throw new RuntimeException("Cannot initialize javascript context twice, " + "system is currently initialized as: '" + initializationMode.name() + "'.");
        }
    }
    initializationMode = requestedMode;
    ContextFactory contextFactory;
    switch(requestedMode) {
        case UNSAFE:
            contextFactory = new ContextFactory() {

                protected Context makeContext() {
                    Context cx = super.makeContext();
                    cx.setLanguageVersion(Context.VERSION_1_7);
                    // TODO: This goes up to 9, do performance tests to determine appropriate level of optimization
                    cx.setOptimizationLevel(4);
                    return cx;
                }
            };
            break;
        default:
            contextFactory = new ContextFactory() {

                protected Context makeContext() {
                    Context cx = super.makeContext();
                    ClassShutter shutter = new WhiteListClassShutter(UserScriptClassWhiteList.getWhiteList());
                    cx.setLanguageVersion(Context.VERSION_1_7);
                    // TODO: This goes up to 9, do performance tests to determine appropriate level of optimization
                    cx.setOptimizationLevel(4);
                    cx.setClassShutter(shutter);
                    cx.setWrapFactory(new WhiteListJavaWrapper(shutter));
                    return cx;
                }
            };
            break;
    }
    ContextFactory.initGlobal(contextFactory);
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) ClassShutter(org.mozilla.javascript.ClassShutter)

Example 2 with ClassShutter

use of org.mozilla.javascript.ClassShutter in project OpenAM by OpenRock.

the class RhinoScriptEngineFactory method getContext.

/**
     * Gets a fresh Rhino {@link org.mozilla.javascript.Context} object by calling
     * {@link org.mozilla.javascript.ContextFactory#enterContext()}. The returned context object will be configured
     * with any specified class shutter and optimisation level settings before being returned. Each call to this
     * method should be accompanied by a call to {@link #releaseContext(org.mozilla.javascript.Context)} when the
     * context is no longer required. It is recommended to use a try-finally pattern for this, like:
     * <pre>
     *     final Context context = factory.getContext();
     *     try {
     *         // Use context
     *     } finally {
     *         factory.releaseContext(context);
     *     }
     * </pre>
     *
     * @return the freshly configured context object.
     */
Context getContext() {
    final Context context = contextFactory.enterContext();
    context.setOptimizationLevel(optimisationLevel);
    final ClassShutter sandbox = classShutter;
    if (sandbox != null) {
        context.setClassShutter(sandbox);
    }
    return context;
}
Also used : Context(org.mozilla.javascript.Context) ClassShutter(org.mozilla.javascript.ClassShutter)

Aggregations

ClassShutter (org.mozilla.javascript.ClassShutter)2 Context (org.mozilla.javascript.Context)2 ContextFactory (org.mozilla.javascript.ContextFactory)1