HomeApache Tuscany Docs 2.x > Index > SCA Java Extensions Guide > SCA Java binding.jsonrpc
 Apache Tuscany Docs 2.x > Index > SCA Java Extensions Guide > SCA Java binding.jsonrpc Tuscany Home | User List | Dev List | Issue Tracker  
Table of Contents

<binding.jsonrpc>

Tuscany supports JSON-RPC as a protcol for use with SCA services by using the <binding.jsonrpc> element in your Application composite. This enables remote web browser clients to easily make RPC style calls to server-side SCA components.

User Stories

  • A client application invoke a remote SCA Service using JSON-RPC and the invocation return business objects in JSON format.
    • e.g. Retrieve catalog items
  • A SCA component can define a reference to a remote SCA Service and use JSON-RPC for the invocation.
    • e.g. A catalog aggregator invoke various catalog services using JSON-RPC and aggregate the returned items into a single response
  • A client application invoke a SCA Service using JSON-RPC and the invocation returns a business exception.
    • e.g. Empty catalog throws a Busines Exception. Business Exception should properly propagate and display nicely to client.
  • A client application invoke a SCA Service using JSON-RPC and the invocation returns a runtime exception.
    • e.g. Try to retrieve catalog items from a catalog service that is un-available. Runtime exception properly propagate and display nicely to client.
  • A client application needs to access a service exposed trough JSON-RPC binding that requires authentication.
    • e.g. make sure if nobody can get to the catalog if it's not authenticated

Using the Tuscany JSON-RPC binding

You could use this binding without any configuration, or by providing a specific service URI.
To include it on a SCA service or Reference, choose one of the examples below :

<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903"
           xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
           ...

	   <tuscany:binding.jsonrpc/>
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903"
           xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
           ...

   <binding.jsonrpc uri="http://localhost:8080/store/catalog"/>

Consuming JSON-RPC services on the client application

Any JSON-RPC client may be used to access SCA services which use <binding.jsonrpc>, below we are going to describe different ways you could consume the Tuscany JSON-RPC services in your client application.

Utilizing Tuscany Implementation.widget

When your web client application is defined as an SCA component utilizing Tuscany Widgets, a JavaScript is generated which may be included within an HTML document to properly inject services references to SCA references defined on the same HTML document.

This script is used by simply including the following tag within the HTML page :

<script type="text/javascript" src="html-page-name.js" />

This initializes the proxys for the SCA services which can then be injected into SCA references to make requests to the server-side components. For example, if there was a service named "myService" which had operations "aOnewayRequest" and "anRpcRequest" the scripts in the HTML page could now invoke these operations with the following:

//@Reference
var myService = new Reference("myService");
myService.aOnewayRequest(args);

or

//@Reference
var myService = new JSONRpcClient("myService");
myService.anRpcRequest(args, responseFunction);

Also see Tuscany Widgets for more details.

Utilizing a JavaScript Client Proxy

To simplify the task for web browsers developers Tuscany provides a 'binding-jsonrpc.js" JavaScript client proxy code that can be included in your application.

After copying the script to your application, it can be used bu simply including the following tag within the HTML page :

<script type="text/javascript" src="binding-jsonrpc.js" />

This initializes the proxys for the SCA services which can then be used make requests to the server-side components. Based on the scenario described above, below are the to invoke these operations.

var myService = new JSONRpcClient("Catalog").service;
myService.aOnewayRequest(args);

or

var myService = new JSONRpcClient("Catalog").service;
myService.anRpcRequest(args, responseFunction);

Handling JSON-RPC Response with callbacks

In that example 'responseFunction' is the name of a function which is called to process the response and which gets called asynchronously on another thread when the response is avaialble. RPC requests are done this way instead of the simpler "answer = myService.anRpcRequest(args)" to avoid hanging the browser while the (potentially slow) request is being processed. An example of the responseFunction for the previous example is:

function responseFunction(answer){
  // do something with answer
}

Handling errors

//initialization code
try{
   myService.anRpcRequest(args, responseFunction);
} catch(e) {
   //handle error
   alert(e);
}

function responseFunction(answer, exception){
   //handle exception information
   if(exception){
      alert(exception.message);
      return;
   }
  // do something with answer
}

Using SCA JSON-RPC services with Dojo

Apache Tuscany JSON-RPC services provide built-in support for Dojo RPC. The Dojo toolkit is a popular framework for writing Ajax/Web 2.0 style browser client applications. Tuscany SCA services which use <binding.jsonrpc> will by default support the Simple Method Description (SMD) protocol. SMD is similar to ?wsdl for Web services, entering a service endpoint appended with ?smd will return a SMD descriptor for the service.

Using Tuscany SCA services with Dojo can therefore be as simple as the following:

  var myService = new dojo.rpc.JsonService("myService?smd");

Supported data types

The JSON-RPC binding utilize the Databinding Framework to provide support for the following data transformations :

  • Primitive Type <==> JSON <==> Primitive Type
  • Array of Primitive Type <==> JSON <==> Array of Primitive Type
  • Java bean <==> JSON <==> Java bean
  • List <==> JSON <==> List
  • Map <==> JSON <==> Map
  • Set <==> JSON <==> Set

Some examples:

There are two samples showing using <binding.jsonrpc>, one which uses the Dojo Toolkit on the client, and another which uses the Tuscany scaDomain.js script. The samples are helloworld-dojo and helloworld-jsonrpc.