Ejemplo 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();
  }
 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);
     }
   }
 }
Ejemplo n.º 3
0
 private void addClassPathToScriptEngine(File jarFile) throws ConfigurationException {
   try {
     StringBuilder cmd = new StringBuilder(addClassPathCmd);
     int tagStartPos = cmd.indexOf(parameterTag);
     int tageEndPos = tagStartPos + parameterTag.length();
     cmd.replace(tagStartPos, tageEndPos, jarFile.getAbsolutePath().replace("\\", "/"));
     // System.out.println("cmd " + cmd.toString());
     engine.eval("add-classpath", 1, 1, cmd.toString()); // $NON-NLS-1$
   } catch (Exception ex) {
     String msg = String.format("Failed to load class path %s", jarFile.getName()); // $NON-NLS-1$
     System.err.println(msg + " " + ex);
     throw new ConfigurationException(msg, ex);
   }
 }
Ejemplo n.º 4
0
 /**
  * Imports a package into the script engine
  *
  * @param pkgName the name of the package
  * @throws ConfigurationException if the package cannot be imported
  */
 private void importPkgToScriptEngine(String pkgName) throws ConfigurationException {
   try {
     StringBuilder cmd = new StringBuilder(importPackageCmd);
     int tagStartPos = cmd.indexOf(parameterTag);
     int tageEndPos = tagStartPos + parameterTag.length();
     cmd.replace(tagStartPos, tageEndPos, pkgName);
     // System.out.println("cmd " + cmd.toString());
     engine.eval("load-packages", 1, 1, cmd.toString()); // $NON-NLS-1$
   } catch (Exception ex) {
     String msg = String.format("Failed to import package %s", pkgName); // $NON-NLS-1$
     System.err.println(msg + " " + ex);
     throw new ConfigurationException(msg, ex);
   }
 }
 /* (non-Javadoc)
  * @see org.marketcetera.strategy.ExecutionEngine#start()
  */
 @Override
 public Object start() throws StrategyException {
   try {
     return scriptEngine.eval(strategy.getLanguage().name(), 0, 0, processedScript);
   } catch (BSFException e) {
     CompilationFailed failed = new CompilationFailed(strategy);
     if (e.getTargetException() instanceof RaiseException) {
       failed.addDiagnostic(Diagnostic.error(RubyExecutor.exceptionAsString(e)));
     }
     StrategyModule.log(
         LogEventBuilder.error()
             .withMessage(COMPILATION_FAILED, String.valueOf(strategy), failed.toString())
             .withException(failed)
             .create(),
         strategy);
     throw failed;
   }
 }
 /* (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));
   }
 }
Ejemplo n.º 7
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();
  }