Two Steps to success.
The second step is always the same. Different are the properties.
A minimal example for a XML-RPC-Service with local server instance.
import java.util.Properties; import test.crispy.example.service.Echo; import net.sf.crispy.Property; import net.sf.crispy.impl.MiniServer; import net.sf.crispy.impl.ServiceManager; import net.sf.crispy.impl.XmlRpcExecutor; import net.sf.crispy.impl.xmlrpc.MiniXmlRpcServer; public static void main(String[] args) { MiniServer miniServer = null; try { // Server part miniServer = new MiniXmlRpcServer(); miniServer.addService(Echo.class.getName(), EchoImpl.class.getName()); miniServer.addService(Calculator.class.getName(), CalculatorImpl.class.getName()); miniServer.start(); // Client part Properties prop = new Properties(); prop.put(Property.EXECUTOR_CLASS, XmlRpcExecutor.class.getName()); ServiceManager manager = new ServiceManager(prop); Echo echo = (Echo) manager.createService(Echo.class); System.out.println("Echo: " + echo.echo("Hello Crispy")); } catch (Exception e) { e.printStackTrace(); } finally { miniServer.stop(); } }
=> Details you can see on the User Guide or Developer Guide.
public interface Calculator { public int add(int a, int b); public int subtract(int a, int b); }
public class CalculatorImpl implements Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } }
// configuration, see step one Properties prop = new Properties(); // the properties, dependent on the kind of call ... // create ServiceManager (Factory) with properties from step one ServiceManager serviceManager = new ServiceManager(prop); // create service (interface) Calculator calculator = (Calculator) serviceManager.createService(Calculator.class); // simple Java object calls int result = calculator.add(2, 3); result = calculator.subtract(8, 2);
// Map the interface Calculator to implementation CalculatorImpl prop.put(Calculator.class.getName(),CalculatorImpl.class.getName()); ServiceManager lvServiceManager = new ServiceManager(prop); Echo lvEcho = (Echo) pvServiceManager.createService(Echo.class); System.out.println("Echo: " + lvEcho.echo("Hello Crispy!");
Alternative you can use a pseudo server:
MiniLocalObjectServer server = new MiniLocalObjectServer(); // add service interface and service implemantation server.addService(Echo.class, new EchoImpl()); server.start(); Properties prop = new Properties(); prop.put(Property.STATIC_PROXY_CLASS, StaticLocalObjectProxy.class.getName()); ServiceManager lvServiceManager = new ServiceManager(prop); Echo lvEcho = (Echo) pvServiceManager.createService(Echo.class); System.out.println("Echo: " + lvEcho.echo("Hello Crispy!");
Properties:
Properties prop = new Properties(); prop.put(Property.EXECUTOR_CLASS, HttpExecutor.class.getName()); prop.put(Property.REMOTE_URL_AND_PORT, "http://localhost:8111/crispy/httpserializer");
Create your own HttpServlet:
public class MyHttpServlet extends net.sf.crispy.impl.http.HttpServlet { public void init(ServletConfig config) throws ServletException { addService(Echo.class.getName(), new EchoImpl ()); } }
Or test with the MiniHttpServer:
MiniServer miniServer = new MiniHttpServer(); miniServer.addService(Echo.class.getName(), EchoImpl.class.getName()); miniServer.start();
Properties:
// load an Executor, all calls are delegated to this class (proxy pattern) prop.put(Property.EXECUTOR_CLASS, XmlRpcExecutor.class.getName()); // url and port of server prop.put(Property.REMOTE_URL_AND_PORT, "http://localhost:8080");
Create your own XmlServlet, overwrite the init method and register the servlet in the web.xml:
public class MyXmlRpcHttpServlet extends XmlRpcHttpServlet { ... public void init(ServletConfig config) throws ServletException { addService(MyServiceInterface.class.getName(), new MyServiceImpl()); } ... }
Alternative start the mini server and deploy a service implementation:
MiniXmlRpcServer miniServer = new MiniXmlRpcServer(); miniServer.addService(Echo.class.getName(), EchoImpl.class.getName()); miniServer.addService(Calculator.class.getName(), CalculatorImpl.class.getName()); miniServer.start();
Properties dynamic RMI (a Crispy implementation):
// load an Executor, all calls are delegated to this class (proxy pattern) prop.put(Property.EXECUTOR_CLASS, RmiExecutor.class.getName()); // url and port of server prop.put(Property.REMOTE_URL_AND_PORT, "rmi://localhost:1099");
Properties static RMI, traditional RMI call:
// must extend java.rmi.Remote and throws java.rmi.RemoteException prop.put(Property.STATIC_PROXY_CLASS, StaticRmiProxy.class.getName()); // url and port of server prop.put(Property.REMOTE_URL_AND_PORT, "rmi://localhost:1099"); // register service with lookup name prop.put(RemoteCalculator.class.getName(), RemoteCalculator.LOOKUP_NAME);
Or start the MiniRmiServer:
MiniRmiServer miniServer = new MiniRmiServer(); // by RmiExecutor you can add a service implementation server.addService (Echo.class.getName(), new EchoImpl()); // OR // by StaticRmiProxy you can add a service implementation serever.addService (Echo.class.getName(), new EchoImpl()); miniServer.start();
Properties:
// Map Service to JNDI-Name and Home interface properties and create proxy for EJBs calls prop.put(EjbService.class.getName(), EjbService.JNDI_NAME); // map to the home-interface, with this interface can create the instance prop.put(EjbService.class.getName() + Property.EJB_HOME_INTERFACE, EjbServiceHome.class.getName()); // delegate calls to proxy prop.put(Property.STATIC_PROXY_CLASS, StaticEjbProxy.class.getName());
Properties:
// load an Executor, all calls are delegated to this class (proxy pattern) prop.put(Property.EXECUTOR_CLASS, JaxRpcExecutor.class.getName()); // url and port from server prop.put(Property.REMOTE_URL_AND_PORT, "http://localhost:8080/axis/services");
Start the server:
MiniServer miniServer = new MiniAxisServer(); miniServer.start();
Properties:
// load Static-Proxy for Burlap implementation props.put(Property.STATIC_PROXY_CLASS, StaticBurlapProxy.class.getName()); // url and port from server prop.put(Property.REMOTE_URL_AND_PORT, "http://localhost:8091/crispy/burlap"); // load Static-Proxy for Hessian implementation props.put(Property.STATIC_PROXY_CLASS, StaticHessianProxy.class.getName()); // url and port from server prop.put(Property.REMOTE_URL_AND_PORT, "http://localhost:8091/crispy/hessian");
Create your own Burlap/Hessian-Servlet, overwrite the init method and register the servlet in the web.xml:
public class MyBurlapServlet extends BurlapServlet { public void init(ServletConfig config) throws ServletException { super.addCache(Echo.class.getName(), new BurlapSkeleton(new EchoImpl())); } } public class MyHessianServlet extends HessianServlet { public void init(ServletConfig config) throws ServletException { super.addCache(Echo.class.getName(), new HessianSkeleton(new EchoImpl(), Echo.class)); } }
Or start the MiniCauchoServer:
MiniServer miniServer = new MiniCauchoServer(); miniServer.addService(Echo.class.getName(), EchoImpl.class.getName()); miniServer.addService(Calculator.class.getName(), CalculatorImpl.class.getName()); miniServer.start();
Properties:
// url and port from server prop.put(Property.REMOTE_URL_AND_PORT, "http://localhost:8092/crispy/rest"); prop.put(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
Create your own RestServlet, overwrite the init method and register the servlet in the web.xml:
public class MyRestServlet extends RestServlet { public void init(ServletConfig config) throws ServletException { addService(Echo.class.getName(), new EchoImpl ()); addService("service.echo", new EchoImpl ()); } }
Or start the MiniRestServer:
MiniServer miniServer = new MiniRestServer(); miniServer.addService(Echo.class.getName(), EchoImpl.class.getName()); miniServer.addService(Calculator.class.getName(), CalculatorImpl.class.getName()); miniServer.start();
Properties:
// url and port from server prop.put(Property.REMOTE_URL_AND_PORT, "127.0.0.1:1057"); prop.put(Property.STATIC_PROXY_CLASS, StaticCorbaProxy.class.getName());
MiniCorbaServer:
MiniServer miniServer = new MiniCorbaServer(); miniServer.addService("test.crispy.example.service.corba.Echo", "test.crispy.example.service.CorbaEcho"); miniServer.start();