use of org.mozilla.javascript.Context in project sling by apache.
the class AsyncExtractor method convertCallback.
private static Function convertCallback(final UnaryCallback unaryCallback) {
return new BaseFunction() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Object arg = (args.length == 0) ? Context.getUndefinedValue() : args[0];
unaryCallback.invoke(arg);
return Context.getUndefinedValue();
}
};
}
use of org.mozilla.javascript.Context in project gradle by gradle.
the class RhinoWorkerUtils method parse.
public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
Context context = Context.enter();
if (contextConfig != null) {
contextConfig.execute(context);
}
Scriptable scope = context.initStandardObjects();
try {
Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
try {
context.evaluateReader(scope, reader, source.getName(), 0, null);
} finally {
reader.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
Context.exit();
}
return scope;
}
use of org.mozilla.javascript.Context in project gradle by gradle.
the class RhinoWorkerUtils method childScope.
public static <R> R childScope(Scriptable parentScope, ScopeOperation<R> operation) {
Context context = Context.enter();
try {
operation.initContext(context);
Scriptable childScope = context.newObject(parentScope);
childScope.setParentScope(parentScope);
return operation.action(childScope, context);
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.Context in project cxf by apache.
the class JsSimpleDomParser method jsFunction_parseFromString.
// CHECKSTYLE:OFF
public Object jsFunction_parseFromString(String xml, String mimeType) {
StringReader reader = new StringReader(xml);
InputSource inputSource = new InputSource(reader);
Document document;
try {
document = documentBuilder.parse(inputSource);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
Context context = ContextFactory.getGlobal().enterContext();
try {
JsSimpleDomNode domNode = (JsSimpleDomNode) context.newObject(getParentScope(), "Node");
domNode.initialize(document, null);
return domNode;
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.Context in project cxf by apache.
the class JsXMLHttpRequest method doSend.
private void doSend(byte[] dataToSend, boolean xml) {
// avoid warnings on stuff we arent using yet.
if (storedUser != null || storedPassword != null) {
//
}
// 1 check state
if (readyState != jsGet_OPENED()) {
LOG.severe("send state != OPENED.");
throwError("INVALID_STATE_ERR");
}
// 2 check flag
if (sendFlag) {
LOG.severe("send sendFlag set.");
throwError("INVALID_STATE_ERR");
}
// 3
sendFlag = storedAsync;
// UTF-8 bytes.
if (xml && !requestHeaders.containsKey("Content-Type")) {
requestHeaders.put("Content-Type", "application/xml;charset=utf-8");
}
// 5 talk to the server.
try {
connection = url.openConnection();
} catch (IOException e) {
LOG.log(Level.SEVERE, "send connection failed.", e);
throwError("CONNECTION_FAILED");
}
connection.setDoInput(true);
// Enable tunneling.
connection.setUseCaches(false);
boolean post = false;
httpConnection = null;
if (connection instanceof HttpURLConnection) {
httpConnection = (HttpURLConnection) connection;
try {
httpConnection.setRequestMethod(storedMethod);
if ("POST".equalsIgnoreCase(storedMethod)) {
httpConnection.setDoOutput(true);
post = true;
}
for (Map.Entry<String, String> headerEntry : requestHeaders.entrySet()) {
httpConnection.setRequestProperty(headerEntry.getKey(), headerEntry.getValue());
}
} catch (ProtocolException e) {
LOG.log(Level.SEVERE, "send http protocol exception.", e);
throwError("HTTP_PROTOCOL_ERROR");
}
}
if (post) {
OutputStream outputStream = null;
try {
// implicitly connects?
outputStream = connection.getOutputStream();
if (dataToSend != null) {
outputStream.write(dataToSend);
outputStream.flush();
}
outputStream.close();
} catch (IOException e) {
errorFlag = true;
LOG.log(Level.SEVERE, "send output error.", e);
throwError("NETWORK_ERR");
try {
outputStream.close();
} catch (IOException e1) {
//
}
}
}
// 6
notifyReadyStateChangeListener();
if (storedAsync) {
new Thread() {
public void run() {
try {
Context cx = ContextFactory.getGlobal().enterContext();
communicate(cx);
} finally {
Context.exit();
}
}
}.start();
} else {
communicate(Context.getCurrentContext());
}
}
Aggregations