4. Scripted Methods

As of version 0.0.4, SwitchRMI includes integration of scripting languages using the Bean Scripting Framework, BSF from IBM. The SwitchRMI distribution includes both bsf.jar and javascript.jar (from the Mozilla project) and some example scripted services. This integration allows you to use scripting languages, like Javascript or Python, to write web-services and expose them using SwitchRMI with any of the supported RMI protocols.

Deployment of Scripted Services

The deployment process is similar to the deployment of Java classes as web-services and follows again a three step process:

  1. Implement your scripted methods:
    		var prefix = "javascript:";
    
    		function jsEcho(string) {
    			return prefix + string;
    		}
    		
  2. Implement a Java interface to your scripted methods or functions. In the current release the Java method name declared in the interface must be the same as the method or function name in the script:
    		public interface JSEcho {
    			public String jsEcho(String value);
    		}
    		
  3. Write a Deployment Descriptor
    		<object name="jsEcho" scope="request">
    			<script source="/com/mjh/switchrmi/script/Echo.js" />
    			<interface class="com.mjh.switchrmi.script.JSEcho" />
    		</object>
    		

    The object element looks almost identical to the descriptor for a Java class. The deployed object has a name, a scope and a list of supported RMI protocols. It contains one, or more, interface declarations listing the interfaces supported by the deployed object and a script element which declares the name of the script file containing the service method implementations.

    The deployment descriptor must be added to the deployment configuration file, inside the service element.

Accessing a Scripted Service from a Java Client

The code to access a scripted service is the same as the code used to access a Java class exposed as a SwitchRMI published service:

		import com.mjh.switchrmi.*;
				
			private RmiClient rmi;
			private JSEcho     proxy;
					
			try {
				rmi = new RmiClient();
				proxy = rmi.connect("http://localhost:8080/rmi/jsEcho.xmlrpc", com.mjh.switchrmi.script.JSEcho.class);
				String string = proxy.jsEcho("This is a String);
			}
			catch (Exception ex) {
			}