01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.bpmscript;
18:
19: import java.io.FileNotFoundException;
20: import java.io.FileReader;
21: import java.io.IOException;
22:
23: import org.mozilla.javascript.Context;
24: import org.mozilla.javascript.Function;
25: import org.mozilla.javascript.JavaScriptException;
26: import org.mozilla.javascript.Script;
27: import org.mozilla.javascript.Scriptable;
28: import org.mozilla.javascript.ScriptableObject;
29:
30: public class JsRunner {
31: public static void main(String[] args)
32: throws FileNotFoundException, IOException {
33: Context cx = Context.enter();
34: try {
35: // create scripting environment (i.e. load everything)
36: Scriptable scope = cx.initStandardObjects();
37: cx.setOptimizationLevel(-1);
38: Object wrappedOut = Context.javaToJS(System.out, scope);
39: ScriptableObject.putProperty(scope, "out", wrappedOut);
40: Script script = cx.compileReader(new FileReader(args[0]),
41: args[0], 0, null);
42: script.exec(cx, scope);
43:
44: // get the main function and execute it
45: Object object = scope.get("main", scope);
46: if (object instanceof Function) {
47: Function function = (Function) object;
48: Object result = null;
49: try {
50: result = function.call(cx, scope, scope, args);
51: System.out.println(result);
52: } catch (JavaScriptException ex) {
53: // if we have an exception and it's a continuation stash it away
54: ex.printStackTrace();
55: }
56: }
57: } finally {
58: Context.exit();
59: }
60: }
61: }
|