WSO2Con 2013 CFP Banner

Deploying a Javascript (E4X) Service in Axis2

Discuss this article on Stack Overflow
By Oshani .
  • 1 May, 2006
  • Level: 
  • Reads: 6611

E4X (ECMAScript for XML) extends the semantics of familiar Javascript operators to manipulate XML data. When it comes to Web services we all speak XML. Therefore, if you could write an E4X service it would provide a versatile and easy way to manipulate the XML in order to implement the business logic. This document will introduce how you can write a service using Javascript and how it can be exposed through Apache Axis2/Java Web service engine.

Oshani .

WSO2 Inc.

Implementation in a Nut-shell

Are you wondering how all this could happen? Then here's what you may want to know. It's highly recommended that you read Axis2 User's Guide before continuing with this tutorial.

To deploy a Javascript (E4X) [1] service, first you need to have JavaScriptReceiver, JavaScriptEngine classes and js.jar [2] library in your classpath. The JavaScriptReceiver processes the incoming message and extracts the following from the MessageContext:

  • Javascript function name (i.e. the operation the client requests)
  • The arguments/payload to be sent to the Javascript service
  • The Javascript source stream

It then calls the JavaScriptEngine which uses the Mozilla Rhino API to process the javascript. The JavaScriptEngine returns an xml string object to the JavaScriptReceiver and the latter encodes the result and sends the outgoing MessageContext.

Writing & Deploying a Javascript Service

So, now if you are interested in getting in to the action by writing and deploying a Javascript service in Axis2, here's what you have to do:

  1. Write the javascript service- The input argument to the service function would be an XML and the output from the function should also be an XML.
  2. Write the META-INF/services.xml- This follows the axis2 convention of deploying a service. However, the MessageReceiver in this case should be "org.sample.javascript.JavaScriptReceiver", and the ServiceClass parameter should include the Javascript source file instead of the service class.
  3. Put js.jar [2] into the "lib" folder.
  4. Package the javascript source file, "META-INF", "lib" and the class files "org.sample.javascript.JavaScriptReceiver" and " org.sample.javascript.JavaScriptEngine" into an Axis2 archive (aar) file.
  5. Put the aar into your servlet container and test your service with a client.

If you prefer to skip the exercises, and go to the answers directly, a working example is available at [3]. Simply drop the JavaScriptService.aar into your webapps/axis2/WEB-INF/services and run the JavaScriptClient!

Appendix

Applies To:

Apache Axis2/Java versions before 1.0