static { BSFManager.registerScriptingEngine( "jexl", //$NON-NLS-1$ "org.apache.commons.jexl.bsf.JexlEngine", //$NON-NLS-1$ new String[] {"jexl"}); // $NON-NLS-1$ log.info("Registering JMeter version of JavaScript engine as work-round for BSF-22"); BSFManager.registerScriptingEngine( "javascript", //$NON-NLS-1$ "org.apache.jmeter.util.BSFJavaScriptEngine", //$NON-NLS-1$ new String[] {"js"}); // $NON-NLS-1$ }
protected void configureBsf() { String[] bshExtensions = {"bsh"}; BSFManager.registerScriptingEngine( "beanshell", "org.ofbiz.base.util.OfbizBshBsfEngine", bshExtensions); String[] jsExtensions = {"js"}; BSFManager.registerScriptingEngine( "javascript", "org.ofbiz.base.util.OfbizJsBsfEngine", jsExtensions); String[] smExtensions = {"sm"}; BSFManager.registerScriptingEngine( "simplemethod", "org.ofbiz.minilang.SimpleMethodBsfEngine", smExtensions); }
/** Test of eval method, of class JRubyEngine. */ @Test public void testEval() throws Exception { System.out.println("eval"); BSFManager.registerScriptingEngine( "jruby", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"}); BSFManager manager = new BSFManager(); JRubyEngine instance = new JRubyEngine(); instance.initialize(manager, "jruby", null); String file = ""; int line = 0; int col = 0; Object expr = null; Object expResult = null; Object result = instance.eval(file, line, col, expr); assertEquals(expResult, result); expResult = "HELLO WORLD!"; result = instance.eval("<script>", 0, 0, "message=\"Hello \" + \"World!\"\nmessage.upcase"); assertEquals(expResult, result); manager.declareBean("greet", "Heeey", String.class); result = manager.eval("jruby", "<script>", 0, 0, "message=$greet + \" World!\""); expResult = "Heeey World!"; assertEquals(expResult, result); }
/** * Loads the declarations of the script engines declared in a property set * * @param props property set containing the configuration * @throws ConfigurationException if the scripting languages cannot be loaded */ private void loadScriptingLanguages(Properties props) throws ConfigurationException { String strEngineList = loadProperty(props, SCRIPT_ENGINE_LIST_P); for (StringTokenizer engineTokenizer = new StringTokenizer(strEngineList); engineTokenizer.hasMoreTokens(); ) { String engineBaseItem = engineTokenizer.nextToken(); String engineName = props.getProperty(engineBaseItem + ".name"); // $NON-NLS-1$ String engineClass = props.getProperty(engineBaseItem + ".class"); // $NON-NLS-1$ String engineExtProps = props.getProperty(engineBaseItem + ".extensions"); // $NON-NLS-1$ String[] extArray = null; if (engineExtProps != null) { List<String> extList = new ArrayList<String>(); for (StringTokenizer extTokenizer = new StringTokenizer(engineExtProps); extTokenizer.hasMoreTokens(); ) { String extension = extTokenizer.nextToken(); ext = extension; extList.add(extension); } extArray = extList.toArray(new String[0]); } BSFManager.registerScriptingEngine(engineName, engineClass, extArray); System.out.println("Script " + engineName + " loaded"); // $NON-NLS-1$ //$NON-NLS-2$ } defaultEngineName = loadProperty(props, DFLT_SCRIPT_ENGINE_P); }
/** * Registers script engines needed for language support. * * <p>When adding support to this class for a new engine, this method must be modified to support * that language */ private static void registerScriptEngines() { if (!BSFManager.isLanguageRegistered(Language.RUBY.name())) { BSFManager.registerScriptingEngine( Language.RUBY.name(), "org.jruby.javasupport.bsf.JRubyEngine", //$NON-NLS-1$ new String[] {"rb"}); // $NON-NLS-1$ } }
@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()); }
@Test public void testLVar() throws BSFException { BSFManager.registerScriptingEngine( "jruby", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"}); BSFManager bsf = new BSFManager(); bsf.eval("jruby", "(java)", 1, 1, "$x='GVar'"); bsf.eval("jruby", "(java)", 1, 1, "puts \"$x = #{$x}\""); bsf.eval("jruby", "(java)", 1, 1, "x='LVar'"); bsf.eval("jruby", "(java)", 1, 1, "at_exit { puts \"#{x} and #{$x} in an at_exit block\" }"); bsf.eval("jruby", "(java)", 1, 1, "puts \"x = #{x}\""); bsf.terminate(); }
@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); }
/** 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.opennms.netmgt.notifd.NotificationStrategy#send(java.util.List) */ @Override public int send(List<Argument> arguments) { m_arguments = arguments; String fileName = getFileName(); String lang = getLangClass(); String engine = getBsfEngine(); String extensions[] = getFileExtensions(); LogUtils.infof(this, "Loading notification script from file '%s'", fileName); File scriptFile = new File(fileName); BSFManager bsfManager = new BSFManager(); int returnCode = -1; try { if (lang == null) lang = BSFManager.getLangFromFilename(fileName); // Declare some beans that can be used inside the script HashMap<String, String> results = new HashMap<String, String>(); bsfManager.declareBean("results", results, HashMap.class); declareBeans(bsfManager); if (engine != null && lang != null && extensions != null && extensions.length > 0) { BSFManager.registerScriptingEngine(lang, engine, extensions); } if (scriptFile.exists() && scriptFile.canRead()) { String code = IOUtils.getStringFromReader( new InputStreamReader(new FileInputStream(scriptFile), "UTF-8")); // Check foot before firing checkAberrantScriptBehaviors(code); // Execute the script bsfManager.exec(lang, "BSFNotificationStrategy", 0, 0, code); // Check whether the script finished successfully if ("OK".equals(results.get("status"))) { LogUtils.infof( this, "Execution succeeded and successful status passed back for script '%s'", scriptFile); returnCode = 0; } else { LogUtils.warnf( this, "Execution succeeded for script '%s', but script did not indicate successful notification by putting an entry into the 'results' bean with key 'status' and value 'OK'", scriptFile); returnCode = -1; } } else { LogUtils.warnf( this, "Cannot locate or read BSF script file '%s'. Returning failure indication.", fileName); returnCode = -1; } } catch (BSFException e) { LogUtils.warnf( this, e, "Execution of script '%s' failed with BSFException: %s", scriptFile, e.getMessage()); returnCode = -1; } catch (FileNotFoundException e) { LogUtils.warnf(this, "Could not find BSF script file '%s'.", fileName); returnCode = -1; } catch (IOException e) { LogUtils.warnf( this, e, "Execution of script '%s' failed with IOException: %s", scriptFile, e.getMessage()); returnCode = -1; } catch (Throwable e) { // Catch any RuntimeException throws LogUtils.warnf( this, e, "Execution of script '%s' failed with unexpected throwable: %s", scriptFile, e.getMessage()); returnCode = -1; } finally { bsfManager.terminate(); } return returnCode; }
public PNutsTagLibrary() { BSFManager.registerScriptingEngine("pnuts", "pnuts.ext.PnutsBSFEngine", new String[] {"pnut"}); setLanguage("pnuts"); }