jabsorb
Java to JavaScript Object Request Broker

Java Client

Note: This author didn't write this, but analysed the code and this is his understanding of it. Hopefully someone with a better understanding of it will edit this!

Overview

The point behind the Java Client is to have a Client which can make calls to some class and have these calls routed over JSON-RPC to the server. The server should then execute the call and pass the result back, which gets reconverted to a Java object. So to implement this, two main classes are created. Client, which makes the calls, and Server, which executes them. The Client class implements the InvocationHandler, which provides the invoke() method. The invoke() method then (if the method is not a really simple one - equals, hashcode or toString) creates the Jabsorb objects, sends them over a Session and reconstructs the result. So that you don't have to figure it out all the arguments to the invoke(), the code is very nice and allows a Proxy to do this for you.

To do this, a java.lang.reflect.Proxy is created for each class which is to be called on the server. A Proxy is just a blank class but which maintains methods for each of any number of interfaces passed to it. This means that a Interface must be made for each Class which is desired to be called. These methods all call an InvokationHandler's (in this case Client's) invoke method, which works as explained above.

This proxy is created for you by Client's openProxy() method. It just takes a key (which must match that on the server) and the interface that you want to make calls on.

Now, so long as you have a running server going with the Class on which you want to call methods having been registered with the JSONRPCBridge using registerObject() - (Note that the key that it is registered must be the same as the key given to the openProxy() class on the Client), you are fine. If not, then you need to make one! The easiest way of doing this is using Jetty, a very lightweight Servlet container. To do this, you just need to create an instance of Jetty's Server on a port and register the class. To do this, here is an example from our tests:

int port= 8083;
JSONRPCBridge.getGlobalBridge().registerObject("test", new org.jabsorb.test.Test());
//The jetty classes are fully resolved so you can see where they come from.
org.mortbay.jetty.Server server= new Server(port);
org.mortbay.jetty.servlet.Context context= new Context(server, JABSORB_CONTEXT, Context.SESSIONS);
org.mortbay.jetty.servlet.ServletHolder jsonRpcServlet= new ServletHolder(new JSONRPCServlet());
jsonRpcServlet.setInitParameter("auto-session-bridge", "0");
context.addServlet(jsonRpcServlet, "/*");
server.start();