Search in sources :

Example 16 with ContextFactory

use of org.mozilla.javascript.ContextFactory in project druid by druid-io.

the class JavaScriptExtractionFn method compile.

private static Function<Object, String> compile(String function) {
    final ContextFactory contextFactory = ContextFactory.getGlobal();
    final Context context = contextFactory.enterContext();
    context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL);
    final ScriptableObject scope = context.initStandardObjects();
    final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null);
    Context.exit();
    return new Function<Object, String>() {

        public String apply(Object input) {
            // ideally we need a close() function to discard the context once it is not used anymore
            Context cx = Context.getCurrentContext();
            if (cx == null) {
                cx = contextFactory.enterContext();
            }
            final Object res = fn.call(cx, scope, scope, new Object[] { input });
            return res != null ? Context.toString(res) : null;
        }
    };
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) Function(com.google.common.base.Function) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 17 with ContextFactory

use of org.mozilla.javascript.ContextFactory 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 18 with ContextFactory

use of org.mozilla.javascript.ContextFactory in project sling by apache.

the class RhinoJavaScriptEngineFactory method dropRootScope.

private void dropRootScope() {
    // ensure the debugger is closed if the root scope will
    // be replaced to ensure no references to the old scope
    // and context remain
    ContextFactory contextFactory = ContextFactory.getGlobal();
    if (contextFactory instanceof SlingContextFactory) {
        ((SlingContextFactory) contextFactory).exitDebugger();
    }
    // drop the scope
    rootScope = null;
}
Also used : SlingContextFactory(org.apache.sling.scripting.javascript.helper.SlingContextFactory) ContextFactory(org.mozilla.javascript.ContextFactory) SlingContextFactory(org.apache.sling.scripting.javascript.helper.SlingContextFactory)

Example 19 with ContextFactory

use of org.mozilla.javascript.ContextFactory in project sling by apache.

the class SlingContextFactory method dispose.

private void dispose() {
    // ensure the debugger is closed
    exitDebugger();
    // reset the context factory class for future use
    ContextFactory newGlobal = new ContextFactory();
    setField(newGlobal, "hasCustomGlobal", Boolean.FALSE);
    setField(newGlobal, "global", newGlobal);
    setField(newGlobal, "sealed", Boolean.FALSE);
    setField(newGlobal, "listeners", null);
    setField(newGlobal, "disabledListening", Boolean.FALSE);
    setField(newGlobal, "applicationClassLoader", null);
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory)

Example 20 with ContextFactory

use of org.mozilla.javascript.ContextFactory in project jaggery by wso2.

the class XMLHttpRequestHostObject method send.

private void send(Context cx, Object obj) throws ScriptException {
    final HttpMethodBase method;
    if ("GET".equalsIgnoreCase(methodName)) {
        method = new GetMethod(this.url);
    } else if ("HEAD".equalsIgnoreCase(methodName)) {
        method = new HeadMethod(this.url);
    } else if ("POST".equalsIgnoreCase(methodName)) {
        PostMethod post = new PostMethod(this.url);
        if (obj instanceof FormDataHostObject) {
            FormDataHostObject fd = ((FormDataHostObject) obj);
            List<Part> parts = new ArrayList<Part>();
            for (Map.Entry<String, String> entry : fd) {
                parts.add(new StringPart(entry.getKey(), entry.getValue()));
            }
            post.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), post.getParams()));
        } else {
            String content = getRequestContent(obj);
            if (content != null) {
                post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(content.getBytes())));
            }
        }
        method = post;
    } else if ("PUT".equalsIgnoreCase(methodName)) {
        PutMethod put = new PutMethod(this.url);
        String content = getRequestContent(obj);
        if (content != null) {
            put.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(content.getBytes())));
        }
        method = put;
    } else if ("DELETE".equalsIgnoreCase(methodName)) {
        method = new DeleteMethod(this.url);
    } else if ("TRACE".equalsIgnoreCase(methodName)) {
        method = new TraceMethod(this.url);
    } else if ("OPTIONS".equalsIgnoreCase(methodName)) {
        method = new OptionsMethod(this.url);
    } else {
        throw new ScriptException("Unknown HTTP method : " + methodName);
    }
    for (Header header : requestHeaders) {
        method.addRequestHeader(header);
    }
    if (username != null) {
        httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    }
    this.method = method;
    final XMLHttpRequestHostObject xhr = this;
    if (async) {
        updateReadyState(cx, xhr, LOADING);
        final ContextFactory factory = cx.getFactory();
        final ExecutorService es = Executors.newSingleThreadExecutor();
        es.submit(new Callable() {

            public Object call() throws Exception {
                Context ctx = RhinoEngine.enterContext(factory);
                try {
                    executeRequest(ctx, xhr);
                } catch (ScriptException e) {
                    log.error(e.getMessage(), e);
                } finally {
                    es.shutdown();
                    RhinoEngine.exitContext();
                }
                return null;
            }
        });
    } else {
        executeRequest(cx, xhr);
    }
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ArrayList(java.util.ArrayList) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) Callable(java.util.concurrent.Callable) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) HeadMethod(org.apache.commons.httpclient.methods.HeadMethod) OptionsMethod(org.apache.commons.httpclient.methods.OptionsMethod) Context(org.mozilla.javascript.Context) DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) TraceMethod(org.apache.commons.httpclient.methods.TraceMethod) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) IOException(java.io.IOException) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) ContextFactory(org.mozilla.javascript.ContextFactory) Header(org.apache.commons.httpclient.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) Part(org.apache.commons.httpclient.methods.multipart.Part) GetMethod(org.apache.commons.httpclient.methods.GetMethod) ExecutorService(java.util.concurrent.ExecutorService) PutMethod(org.apache.commons.httpclient.methods.PutMethod) ScriptableObject(org.mozilla.javascript.ScriptableObject) Map(java.util.Map)

Aggregations

ContextFactory (org.mozilla.javascript.ContextFactory)25 Context (org.mozilla.javascript.Context)22 ScriptableObject (org.mozilla.javascript.ScriptableObject)15 Function (com.google.common.base.Function)4 ContextAction (org.mozilla.javascript.ContextAction)4 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 Callable (java.util.concurrent.Callable)3 ExecutorService (java.util.concurrent.ExecutorService)3 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)3 Scriptable (org.mozilla.javascript.Scriptable)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 SlingContextFactory (org.apache.sling.scripting.javascript.helper.SlingContextFactory)2 StreamHostObject (org.jaggeryjs.hostobjects.stream.StreamHostObject)2 Function (org.mozilla.javascript.Function)2 NativeObject (org.mozilla.javascript.NativeObject)2 RhinoException (org.mozilla.javascript.RhinoException)2 ImmutableList (com.google.common.collect.ImmutableList)1 ObjectColumnSelector (io.druid.segment.ObjectColumnSelector)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1