Java Code Examples for javax.script.ScriptContext#setErrorWriter()

The following examples show how to use javax.script.ScriptContext#setErrorWriter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSR223Tasklet.java    From org.openntf.domino with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
	Database database = Factory.getSession(SessionType.CURRENT).getDatabase(databasePath_);

	ScriptEngineManager manager = new ScriptEngineManager();
	ScriptEngine engine = manager.getEngineByExtension(scriptExt_);

	engine.put("database", database);
	engine.put("session", database.getAncestorSession());

	ScriptContext context = engine.getContext();
	context.setWriter(new PrintWriter(System.out));
	context.setErrorWriter(new PrintWriter(System.err));

	try {
		engine.eval(script_);
	} catch (ScriptException e) {
		throw new RuntimeException(e);
	}
}
 
Example 2
Source File: JSJavaScriptEngine.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void start(boolean console) {
  ScriptContext context = engine.getContext();
  OutputStream out = getOutputStream();
  if (out != null) {
    context.setWriter(new PrintWriter(out));
  }
  OutputStream err = getErrorStream();
  if (err != null) {
    context.setErrorWriter(new PrintWriter(err));
  }
  // load "sa.js" initialization file
  loadInitFile();
  // load "~/jsdb.js" (if found) to perform user specific
  // initialization steps, if any.
  loadUserInitFile();

  JSJavaFactory fac = getJSJavaFactory();
  JSJavaVM jvm = (fac != null)? fac.newJSJavaVM() : null;
  // call "main" function from "sa.js" -- main expects
  // 'this' object and jvm object
  call("main", new Object[] { this, jvm });

  // if asked, start read-eval-print console
  if (console) {
    processSource(null);
  }
}
 
Example 3
Source File: JSJavaScriptEngine.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected void start(boolean console) {
  ScriptContext context = engine.getContext();
  OutputStream out = getOutputStream();
  if (out != null) {
    context.setWriter(new PrintWriter(out));
  }
  OutputStream err = getErrorStream();
  if (err != null) {
    context.setErrorWriter(new PrintWriter(err));
  }
  // load "sa.js" initialization file
  loadInitFile();
  // load "~/jsdb.js" (if found) to perform user specific
  // initialization steps, if any.
  loadUserInitFile();

  JSJavaFactory fac = getJSJavaFactory();
  JSJavaVM jvm = (fac != null)? fac.newJSJavaVM() : null;
  // call "main" function from "sa.js" -- main expects
  // 'this' object and jvm object
  call("main", new Object[] { this, jvm });

  // if asked, start read-eval-print console
  if (console) {
    processSource(null);
  }
}
 
Example 4
Source File: JSJavaScriptEngine.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected void start(boolean console) {
  ScriptContext context = engine.getContext();
  OutputStream out = getOutputStream();
  if (out != null) {
    context.setWriter(new PrintWriter(out));
  }
  OutputStream err = getErrorStream();
  if (err != null) {
    context.setErrorWriter(new PrintWriter(err));
  }
  // load "sa.js" initialization file
  loadInitFile();
  // load "~/jsdb.js" (if found) to perform user specific
  // initialization steps, if any.
  loadUserInitFile();

  JSJavaFactory fac = getJSJavaFactory();
  JSJavaVM jvm = (fac != null)? fac.newJSJavaVM() : null;
  // call "main" function from "sa.js" -- main expects
  // 'this' object and jvm object
  call("main", new Object[] { this, jvm });

  // if asked, start read-eval-print console
  if (console) {
    processSource(null);
  }
}
 
Example 5
Source File: JSJavaScriptEngine.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected void start(boolean console) {
  ScriptContext context = engine.getContext();
  OutputStream out = getOutputStream();
  if (out != null) {
    context.setWriter(new PrintWriter(out));
  }
  OutputStream err = getErrorStream();
  if (err != null) {
    context.setErrorWriter(new PrintWriter(err));
  }
  // load "sa.js" initialization file
  loadInitFile();
  // load "~/jsdb.js" (if found) to perform user specific
  // initialization steps, if any.
  loadUserInitFile();

  JSJavaFactory fac = getJSJavaFactory();
  JSJavaVM jvm = (fac != null)? fac.newJSJavaVM() : null;
  // call "main" function from "sa.js" -- main expects
  // 'this' object and jvm object
  call("main", new Object[] { this, jvm });

  // if asked, start read-eval-print console
  if (console) {
    processSource(null);
  }
}
 
Example 6
Source File: JSJavaScriptEngine.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected void start(boolean console) {
  ScriptContext context = engine.getContext();
  OutputStream out = getOutputStream();
  if (out != null) {
    context.setWriter(new PrintWriter(out));
  }
  OutputStream err = getErrorStream();
  if (err != null) {
    context.setErrorWriter(new PrintWriter(err));
  }
  // load "sa.js" initialization file
  loadInitFile();
  // load "~/jsdb.js" (if found) to perform user specific
  // initialization steps, if any.
  loadUserInitFile();

  JSJavaFactory fac = getJSJavaFactory();
  JSJavaVM jvm = (fac != null)? fac.newJSJavaVM() : null;
  // call "main" function from "sa.js" -- main expects
  // 'this' object and jvm object
  call("main", new Object[] { this, jvm });

  // if asked, start read-eval-print console
  if (console) {
    processSource(null);
  }
}
 
Example 7
Source File: JSJavaScriptEngine.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected void start(boolean console) {
  ScriptContext context = engine.getContext();
  OutputStream out = getOutputStream();
  if (out != null) {
    context.setWriter(new PrintWriter(out));
  }
  OutputStream err = getErrorStream();
  if (err != null) {
    context.setErrorWriter(new PrintWriter(err));
  }
  // load "sa.js" initialization file
  loadInitFile();
  // load "~/jsdb.js" (if found) to perform user specific
  // initialization steps, if any.
  loadUserInitFile();

  JSJavaFactory fac = getJSJavaFactory();
  JSJavaVM jvm = (fac != null)? fac.newJSJavaVM() : null;
  // call "main" function from "sa.js" -- main expects
  // 'this' object and jvm object
  call("main", new Object[] { this, jvm });

  // if asked, start read-eval-print console
  if (console) {
    processSource(null);
  }
}
 
Example 8
Source File: JSJavaScriptEngine.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected void start(boolean console) {
  ScriptContext context = engine.getContext();
  OutputStream out = getOutputStream();
  if (out != null) {
    context.setWriter(new PrintWriter(out));
  }
  OutputStream err = getErrorStream();
  if (err != null) {
    context.setErrorWriter(new PrintWriter(err));
  }
  // load "sa.js" initialization file
  loadInitFile();
  // load "~/jsdb.js" (if found) to perform user specific
  // initialization steps, if any.
  loadUserInitFile();

  JSJavaFactory fac = getJSJavaFactory();
  JSJavaVM jvm = (fac != null)? fac.newJSJavaVM() : null;
  // call "main" function from "sa.js" -- main expects
  // 'this' object and jvm object
  call("main", new Object[] { this, jvm });

  // if asked, start read-eval-print console
  if (console) {
    processSource(null);
  }
}
 
Example 9
Source File: JSJavaScriptEngine.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void start(boolean console) {
  ScriptContext context = engine.getContext();
  OutputStream out = getOutputStream();
  if (out != null) {
    context.setWriter(new PrintWriter(out));
  }
  OutputStream err = getErrorStream();
  if (err != null) {
    context.setErrorWriter(new PrintWriter(err));
  }
  // load "sa.js" initialization file
  loadInitFile();
  // load "~/jsdb.js" (if found) to perform user specific
  // initialization steps, if any.
  loadUserInitFile();

  JSJavaFactory fac = getJSJavaFactory();
  JSJavaVM jvm = (fac != null)? fac.newJSJavaVM() : null;
  // call "main" function from "sa.js" -- main expects
  // 'this' object and jvm object
  call("main", new Object[] { this, jvm });

  // if asked, start read-eval-print console
  if (console) {
    processSource(null);
  }
}
 
Example 10
Source File: REPLPane.java    From sciview with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
   * Constructs an interpreter UI pane for a SciJava scripting REPL.
   *
   * @param context The SciJava application context to use
   */
  public REPLPane(final Context context) {
    context.inject(this);
    output = new OutputPane(log);
    final JScrollPane outputScroll = new JScrollPane(output);
    outputScroll.setPreferredSize(new Dimension(440, 400));

    repl = new ScriptREPL(context, output.getOutputStream());
    repl.initialize();

    final Writer writer = output.getOutputWriter();
    final ScriptContext ctx = repl.getInterpreter().getEngine().getContext();
    ctx.setErrorWriter(writer);
    ctx.setWriter(writer);

    vars = new VarsPane(context, repl);
    vars.setBorder(new EmptyBorder(0, 0, 8, 0));

    prompt = new REPLEditor(repl, vars, output);
    context.inject(prompt);
    prompt.setREPLLanguage("Python");
    final JScrollPane promptScroll = new JScrollPane(prompt);

    final JPanel bottomPane = new JPanel();
    bottomPane.setLayout(new MigLayout("ins 0", "[grow,fill][pref]", "[grow,fill,align top]"));
    bottomPane.add(promptScroll, "spany 2");

    final JSplitPane outputAndPromptPane =
            new JSplitPane(JSplitPane.VERTICAL_SPLIT, outputScroll, bottomPane);
    outputAndPromptPane.setResizeWeight(1);
//    outputAndPromptPane.setDividerSize(2);

    mainPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, vars,
            outputAndPromptPane);
    mainPane.setDividerSize(1);
    mainPane.setDividerLocation(0);
  }
 
Example 11
Source File: REPLEditor.java    From sciview with BSD 2-Clause "Simplified" License 5 votes vote down vote up
void setREPLLanguage(String language) {
  System.out.println("Resetting language to " + language);
  if(!repl.getInterpreter().getLanguage().getNames().contains(language)) {
    repl.lang(language);
  }
  ScriptLanguage l = scriptService.getLanguageByName(language);
  setLanguage(scriptService.getLanguageByName(language));

  final ScriptContext ctx = repl.getInterpreter().getEngine().getContext();
  ctx.setErrorWriter(outputPane.getOutputWriter());
  ctx.setWriter(outputPane.getOutputWriter());
}
 
Example 12
Source File: ScriptManager.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void configureEngine() {

        ScriptContext context = new SimpleScriptContext();
        context.setWriter(output);
        context.setErrorWriter(output);

        engine.setContext(context);
        engine.put("out", output);
        engine.put("err", output);

        output.println(MessageFormat.format("Script language set to {0}.", engine.getFactory().getLanguageName()));

        final URL url = findInitScript();
        if (url != null) {
            output.println(MessageFormat.format("Loading initialisation script ''{0}''...", url));
            execute0(url, new Observer() {
                @Override
                public void onSuccess(Object value) {
                }

                @Override
                public void onFailure(Throwable throwable) {
                    output.println("Failed to load initialisation script. " +
                            "BEAM-specific language extensions are disabled.");
                    throwable.printStackTrace(output);
                }
            });
            output.println("Initialisation script loaded. BEAM-specific language extensions are enabled.");
        } else {
            output.println("No initialisation script found. " +
                    "BEAM-specific language extensions are disabled.");
        }
    }
 
Example 13
Source File: ConsoleContext.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public void applyTo ( final ScriptContext scriptContext )
{
    scriptContext.setWriter ( new PrintWriter ( new OutputStreamWriter ( this.writerStream ) ) );
    scriptContext.setErrorWriter ( this.errorPrintWriter );
}