Tuscany Hello World JSON-RPC Sample

Overview

The Tuscany hello world JSON-RPC sample shows using the Tuscany SCA runtime in a Tomcat environment being accessed from a JavaScript program running in the client browser that implements the classic introductory hello world example.

Setup

Set up your server as described in the Tomcat Setup.  If you are not configuring with the acceptance test the server, you will need to add sample-helloworldjsonrpc-incubating-M1.war to your tomcat's webapp directory.

Running

Once you've started your web server, use your browse to view the following URL: http://localhost:8080/sample-helloworldjsonrpc/
You should see a form you can supply your name and a button.  Once you click the button the JavaScript program locates the SCA component to execute the greeting service.
Note, unlike the J2SE or JSP clients the Tuscany runtime is automatically initialized and running.

Results

The page should be updated with the greeting displayed below the form.

Code Overview

The source files are physically organized as shown below:
+---main
¦ +---java
¦ ¦ sca.module +---org
¦ +---apache
¦ +---tuscany
¦ +---samples
¦ +---helloworldjsonrpc ¦ HelloWorldService.java ¦ HelloWorldServiceComponentImpl.java
+---webapp ¦ helloworld.js ¦ HelloWorldJSONRPC.html +---WEB-INF
¦ web.xml


 

sca.module Defines the SCA module, entryPoint and component. Defines an entryPoint with an JSON-RPC binding, and the HelloWorldService component and the Java class that implements that component
HelloWorldService.java Defines the Java interface implemented by the component.
HelloWorldServiceComponentImpl.java Implements the SCA component. Uses the SCA service annotation tag on the class to show what SCA interface is being implemented.
helloworld.js Handles invoking the HelloWorld SCA service. Gets the value entered in the text field on the web page, invokes the SCA component and puts the result in the output field on the web page.
The interesting part of the script is  SCA.HelloWorldService.getGreetings(who)  . In the SCA JSON-RPC environment all entryPoints with an JSON-RPC binding are bound to the automatically defined script variable  SCA. .
HelloWorldJSONRPC.html The HTML page for the sample. Creates a text box for the user to enter their name, a button to click to get the greeting, and a text field to show the response. The helloworld.js script function getGreeting is used to invoke the SCA service when the button is clicked. To initialize the SCA environment the sca.js script must be defined as the first script in the HTML with the following line:
<script type="text/javascript" src="SCA/scripts/sca.js">
web.xml Standard J2EE web application's web.xml