Exemplo n.º 1
0
  @Test
  public void testVarAccess() {
    Configuration config = Configuration.instance();
    try {
      config.load();
    } catch (ConfigurationException e1) {
      e1.printStackTrace();
      fail();
    }
    BSFManager bsfManager = new BSFManager();
    BSFEngine engine = null;
    try {
      engine = bsfManager.loadScriptingEngine("BeanShell");
    } catch (BSFException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
    assertNotNull(engine);

    try {
      // ObjectRegistry registry = bsfManager.getObjectRegistry();
      // registry.register("mmm", new Integer(3));
      // engine.exec("source", 1, 1, "print( mmm );");
      engine.exec("source", 1, 1, "mmm = 1000;");
      engine.exec("source", 1, 1, "print( mmm );");
    } catch (BSFException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }

    engine.terminate();
  }
Exemplo n.º 2
0
  @Test
  public void testRubyVersion() throws BSFException {
    System.out.println("RubyVersion");
    BSFManager.registerScriptingEngine(
        "jruby", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"});
    BSFManager manager = new BSFManager();
    JRubyEngine instance = (JRubyEngine) manager.loadScriptingEngine("jruby");
    Object result =
        instance.eval("org/jruby/embed/ruby/block-param-scope.rb", 0, 0, PathType.CLASSPATH);
    String expResult = "cat";
    assertEquals(expResult, ((String) result).trim());

    // Ruby 1.9 mode is somehow broken in 1.5.0dev
    BSFManager.registerScriptingEngine(
        "jruby19", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"});
    instance = (JRubyEngine) manager.loadScriptingEngine("jruby19");
    result = instance.eval("org/jruby/embed/ruby/block-param-scope.rb", 0, 0, PathType.CLASSPATH);
    expResult = "bear";
    assertEquals(expResult, ((String) result).trim());
  }
 protected void processFileOrScript(BSFManager mgr) throws BSFException {
   BSFEngine bsfEngine = mgr.loadScriptingEngine(getScriptLanguage());
   final String scriptFile = getFilename();
   if (scriptFile.length() == 0) {
     bsfEngine.exec("[script]", 0, 0, getScript());
   } else { // we have a file, read and process it
     try {
       String script = FileUtils.readFileToString(new File(scriptFile));
       bsfEngine.exec(scriptFile, 0, 0, script);
     } catch (IOException e) {
       log.warn(e.getLocalizedMessage());
       throw new BSFException(BSFException.REASON_IO_ERROR, "Problem reading script file", e);
     }
   }
 }
Exemplo n.º 4
0
  /**
   * Loads the BSF engine.
   *
   * @param engineName name of the engine
   * @see #getEngine()
   */
  private void loadEngine(String engineName) throws ConfigurationException {
    System.out.println("Loading script engine " + engineName);
    try {
      engine = bsfManager.loadScriptingEngine(engineName.trim());
    } catch (BSFException bsfEx) {
      String msg = "Could not find script engine " + engineName; // $NON-NLS-1$
      System.err.println(msg + " " + bsfEx);
      throw new ConfigurationException(msg, bsfEx);
    }

    // import the initial packages
    for (String pkgName : scriptImportPkgList) {
      System.out.println("Loading package " + pkgName);
      importPkgToScriptEngine(pkgName);
    }
    System.out.println("Script engine " + engineName + " loaded");
  }
Exemplo n.º 5
0
  @Test
  public void testPathTyp() throws BSFException {
    System.out.println("PathType");
    BSFManager.registerScriptingEngine(
        "jruby", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"});
    BSFManager manager = new BSFManager();
    JRubyEngine instance = (JRubyEngine) manager.loadScriptingEngine("jruby");
    Object receiver =
        instance.eval("org/jruby/embed/ruby/radioactive_decay.rb", 0, 0, PathType.CLASSPATH);
    String method = "amount_after_years";
    Object[] args = new Object[2];
    args[0] = 10.0;
    args[1] = 1000;

    // Plutonium
    manager.declareBean("h", 24100, Long.class);
    Object result = instance.call(receiver, method, args);
    assertEquals(9.716, (Double) result, 0.001);
  }
Exemplo n.º 6
0
  /** Test of call method, of class JRubyEngine. */
  @Test
  public void testCall() throws Exception {
    System.out.println("call");
    BSFManager.registerScriptingEngine(
        "jruby", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"});
    BSFManager manager = new BSFManager();
    JRubyEngine instance = (JRubyEngine) manager.loadScriptingEngine("jruby");
    instance.initialize(manager, "jruby", null);
    Object recv = null;
    String method = "";
    Object[] args = null;
    Object expResult = null;
    Object result = instance.call(recv, method, args);
    assertEquals(expResult, result);

    String script =
        "# Radioactive decay\n"
            + "def amount_after_years(q0, t)\n"
            + "q0 * Math.exp(1.0 / $h * Math.log(1.0/2.0) * t)\n"
            + "end\n"
            + "def years_to_amount(q0, q)\n"
            + "$h * (Math.log(q) - Math.log(q0)) / Math.log(1.0/2.0)\n"
            + "end";
    recv = manager.eval("jruby", "radioactive_decay", 0, 0, script);
    method = "amount_after_years";
    args = new Object[2];
    args[0] = 10.0;
    args[1] = 1000;

    // Radium
    manager.declareBean("h", 1599, Long.class);
    result = instance.call(recv, method, args);
    assertEquals(6.482, (Double) result, 0.001);

    method = "years_to_amount";
    args[0] = 10.0;
    args[1] = 1.0;
    result = instance.call(recv, method, args);
    assertEquals(5311.8, (Double) result, 0.1);
  }
 /* (non-Javadoc)
  * @see org.marketcetera.strategy.ExecutionEngine#prepare(org.marketcetera.strategy.Strategy, java.lang.String)
  */
 @Override
 public void prepare(Strategy inStrategy, String inProcessedScript) throws StrategyException {
   strategy = inStrategy;
   processedScript = inProcessedScript;
   SLF4JLoggerProxy.debug(
       this,
       "Preparing {}", //$NON-NLS-1$
       inStrategy);
   registerScriptEngines();
   String languageString = inStrategy.getLanguage().name();
   try {
     synchronized (scriptManager) {
       if (scriptEngine == null) {
         String classpath = System.getProperty(Strategy.CLASSPATH_PROPERTYNAME);
         SLF4JLoggerProxy.debug(
             this,
             "Setting classpath to {}", //$NON-NLS-1$
             classpath);
         scriptManager.setClassPath(classpath);
         scriptEngine = scriptManager.loadScriptingEngine(languageString);
         SLF4JLoggerProxy.debug(this, "Initializing engine..."); // $NON-NLS-1$
         scriptEngine.initialize(scriptManager, languageString, new Vector<Object>());
       } else {
         SLF4JLoggerProxy.debug(this, "Reusing intialized engine..."); // $NON-NLS-1$
       }
     }
   } catch (BSFException e) {
     StrategyModule.log(
         LogEventBuilder.error()
             .withMessage(NO_SUPPORT_FOR_LANGUAGE, languageString)
             .withException(e)
             .create(),
         strategy);
     throw new StrategyException(
         e, new I18NBoundMessage1P(NO_SUPPORT_FOR_LANGUAGE, languageString));
   }
 }
Exemplo n.º 8
0
  @Test
  public void testEval() {
    Configuration config = Configuration.instance();
    try {
      config.load();
    } catch (ConfigurationException ex) {
      ex.printStackTrace();
      fail();
    }
    BSFManager bsfManager = new BSFManager();

    Integer i = new Integer(5);
    try {
      bsfManager.declareBean("i", i, Integer.class);
    } catch (BSFException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }

    BSFEngine engine = null;
    try {
      engine = bsfManager.loadScriptingEngine("BeanShell");
    } catch (BSFException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
    assertNotNull(engine);

    Integer j = new Integer(7);
    try {
      bsfManager.declareBean("j", j, Integer.class);
    } catch (BSFException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }

    try {
      engine.exec("source", 1, 1, "a=3;\nprint(a);");
      engine.exec("source", 1, 1, "a=a+1;\nprint(a);");
    } catch (BSFException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }

    try {
      engine.exec("source", 1, 1, "print(\"i=\"+i);");
      engine.exec("source", 1, 1, "j=j*10; print(\"j=\"+j);");
    } catch (BSFException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }

    // BSFDeclaredBean toto = new BSFDeclaredBean();
    // toto.beannew BSFDeclaredBean("i", i, Integer.class);
    ObjectRegistry registry = bsfManager.getObjectRegistry();
    Object jValue = registry.lookup("j");
    if (jValue != null) {
      System.out.println("j-value = " + jValue.toString());
    }
    registry.register("k", new Integer(1000));
    Object kValue = registry.lookup("k");
    assertNotNull(kValue);
    System.out.println("k=" + kValue.toString());
    try {
      engine.exec("source", 1, 1, "print(\"k=\"+k);");
    } catch (BSFException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
    try {
      bsfManager.declareBean("k", kValue, Integer.class);
    } catch (BSFException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
    try {
      engine.exec("source", 1, 1, "print(\"k=\"+k);");
    } catch (BSFException e) {
      e.printStackTrace();
      fail(e.getMessage());
    }

    engine.terminate();
  }