The Tuscany Ruby extension allows Ruby scripts to be used as components in SCA composites and as clients that can invoke SCA services.
The following samples demonstrate use of the Ruby extension:
In order to install and use the Tuscany SCA Ruby Extension there are some extra requirements in addition to the Tuscany SCA requirements:
Software | Download Link |
Ruby version 1.8.x |
http://www.ruby-lang.org Please download and follow the installation instructions. You should be able to build the Tuscany SCA Ruby Extension with other versions of Ruby. The extension has been tested with the Ruby 1.8.5 One-Click Installer on Windows and the 1.8.5 source code (compiled locally) on Linux. |
This section will explain the Tuscany Ruby programming model to help you to write your own Ruby components and clients.
The Tuscany Ruby component and client support comes from a Ruby extension library that is built in the <tuscany_sca_install_dir>/extensions/ruby/bin directory on Windows and <tuscany_sca_install_dir>/extensions/ruby/lib on Linux and Mac OS X.
Using the Ruby SCA extension library, a Ruby client can search for an SCA service with:
require("libtuscany_sca_ruby") calculator = SCA::locateService("CalculatorComponent/CalculatorService")
This finds the component and service as defined in the composite and componentType side files and returns a proxy object that can call the SCA service. You can then simply call a business method on "calculator", like this:
result = calculator.add(12.3, 45.6)
Ruby component implementations are standard Ruby scripts, where class-level functions or module-level functions can be invoked by the Tuscany runtime. To use a Ruby component implementation, use the implementation.ruby element in your .composite file. For example:
<implementation.ruby script="CalculatorImpl.rb"/>
To instantiate a class instance and use a class-level function, the Ruby class must have a default constructor (an initialize() method that takes no arguments) and the class attribute must be definde in the implementation.ruby element in your composite, like so:
<implementation.ruby script="CalculatorImpl.rb" class="CalculatorImpl"/>
Tuscany currently supports passing simple types (strings, ints, floats, etc) as well as Service Data Objects into and out of Ruby components. Service Data Objects are represented in Ruby as REXML Document objects (see the Ruby BigBank sample for a demonstration).
You can write a componentType file for your Ruby component, but you don't have to - the Ruby extension introspects Ruby component implementation classes for you and binds public attributes to references and properties.
References can be invoked from Ruby component implementations by the use of public attributes in the component implementation classes, like this:
class CalculatorImpl # Define the public attribute that corresponds to the divideService reference attr_writer :divideService ... def div(arg1, arg2) print "Ruby - CalculatorImpl.div\n" # Invoke the divideService reference @divideService.divide(arg1.to_f, arg2.to_f) end ... end
and in your composite file:
<component name="CalculatorComponent"> <implementation.ruby script="CalculatorImpl.rb"/> <reference name="divideService">DivideComponent/DivideService</reference> </component>
A composite with a property defined for a component like so:
<component name="DivideComponent"> <implementation.ruby script="DivideImpl.rb" class="DivideImpl"/> <property name="round">true</property> </component>
allows a public attribute of a Ruby component implementation class to be assigned the property value, so it can be used like so:
class DivideImpl # Define the public attribute that corresponds to the round property attr_writer :round ... def divide(arg1, arg2) print "Ruby - DivideImpl.divide ", arg1, " / ", arg2, "\n" res = arg1.to_f / arg2.to_f # Use the round property if @round then res = res.round print "DivideImpl.divide rounding\n" end print "DivideImpl.divide ", res, "\n" res end end
First place to look is at the Tuscany FAQ at http://incubator.apache.org/tuscany/faq.html
Any problem with this release can be reported to the Tuscany mailing lists or create a JIRA issue at http://issues.apache.org/jira/browse/Tuscany.