/**
 * @license
 * Copyright 2020 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.script;

import java.io.IOException;
import java.io.PrintStream;

import bsh.EvalError;
import bsh.Interpreter;
import foam.core.X;
import foam.nanos.boot.NSpec;
import foam.nanos.logger.Logger;

public class BeanShellExecutor {
  private NSpec nSpec;
  public static final Object[] OBJECT_HOLDER = new Object[1];
  
  public BeanShellExecutor(NSpec nSpec) {
    this.nSpec = nSpec; 
  }

  public Object execute(X x, PrintStream ps, String serviceScript ) throws IOException  {
    Interpreter shell = new Interpreter();
    try {
      shell.set("x", x);
      Object service = shell.eval(serviceScript);
      return service;
    } catch (EvalError e) {
      Logger logger = (Logger) x.get("logger");
      if ( logger != null ) {
        logger.error("NSpec serviceScript error", serviceScript, e);
      } else {
        System.err.println("NSpec serviceScript error: " + serviceScript);
        e.printStackTrace();
      }
      return null;
    }
  }
}

@@ -889,104 +889,75 @@
 gger
-.error(%22NSpec serviceScript error%22, serviceScript, e);%0A      %7D else %7B%0A        System.err.println
+ = new foam.nanos.logger.StdoutLogger();%0A      %7D%0A      logger.error
 (%22NS
@@ -983,13 +983,10 @@
 rror
-: %22 +
+%22,
  ser
@@ -999,47 +999,13 @@
 ript
-);%0A        e.printStackTrace();%0A      %7D
+, e);
 %0A   

correct language property default