<h1>Bean Scripting Framework (BSF) Introduction</h1>

To include BSF into your Java code you first need to register the JRuby engine:

<div class="code">
BSFManager.registerScriptingEngine("ruby", 
                                   "org.jruby.javasupport.bsf.JRubyEngine", 
                                   new String[] { "rb" });
</div>

Now you have to create a new BSFManager:

<div class="code">
BSFManager manager = new BSFManager();
</div>

After you have some Java objects you can declare (export) them as "beans" in BSF:

<div class="code">
manager.declareBean("frame", aFrame, JFrame.class);
</div>

These beans can be accessed from Ruby as global variables (in this case `$frame').
You can also register objects:

<div class="code">
manager.registerBean("frame", aFrame);
</div>

this registered object can be accessed by calling:

<div class="code">
$bsf.lookupBean("frame")
</div>

method in Ruby. Now you can execute Ruby scripts:

<div class="code">
manager.exec("ruby", "(java)", 1, 1, "$frame.setTitle(\"A Frame\")");
</div>

Or evaluate Ruby expressions:

<div class="code">
Object obj = manager.eval("ruby", "(java)", 1, 1, "(1..10).collect { |e| e ** 2 }.first");<br/>
int result = ((Integer)obj).intValue();
</div>

<h2>Example</h2>

There is a simple <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jruby/jruby/src/org/jruby/javasupport/bsf/BSFExample.java?rev=1.2&content-type=text/vnd.viewcvs-markup">example</a> BSF in the JRuby source distribution (src/org/jruby/javasupport/bsf/BSFExample.java). Which is also avaibale at:


(C) 2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
(C) 2005 Thomas E Enebo <enebo@acm.org>
