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;
}
};
}
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);
}
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;
}
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);
}
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);
}
}
Aggregations