01 package com.flat502.rox.demo;
02
03 import java.net.InetAddress;
04 import java.util.Iterator;
05 import java.util.List;
06
07 import com.flat502.rox.marshal.RpcCall;
08 import com.flat502.rox.marshal.RpcResponse;
09 import com.flat502.rox.marshal.xmlrpc.XmlRpcMethodResponse;
10 import com.flat502.rox.server.RpcCallContext;
11 import com.flat502.rox.server.SynchronousRequestHandler;
12 import com.flat502.rox.server.XmlRpcServer;
13
14 /**
15 * A demo synchronous server illustrating the {@link com.flat502.rox.server.SynchronousRequestHandler}
16 * interface using SSL.
17 */
18 public class SSLServerDemo implements SynchronousRequestHandler {
19 public RpcResponse handleRequest(RpcCall call, RpcCallContext context) throws Exception {
20 Object[] params = call.getParameters();
21 System.out.println("Method [" + call.getName() + "] called with "
22 + params.length + " parameters");
23 for (int i = 0; i < params.length; i++) {
24 System.out.println(" Param " + (i + 1) + " [" + params[i] + "]");
25 }
26
27 if (call.getName().equals("example.getDate")) {
28 return new XmlRpcMethodResponse(new TimeInfo());
29 } else if (call.getName().equals("example.sum")) {
30 Integer sum = new Integer(sum((List)call.getParameters()[0]));
31 return new XmlRpcMethodResponse(sum);
32 } else if (call.getName().equals("example.getInfo")) {
33 throw new NoSuchMethodException();
34 }
35 throw new NoSuchMethodException();
36 }
37
38 private int sum(List list) {
39 int total = 0;
40 Iterator i = list.iterator();
41 while (i.hasNext()) {
42 total += ((Integer) i.next()).intValue();
43 }
44 return total;
45 }
46
47 /**
48 * Start an instance of this demo server.
49 * <p>
50 * The following XML-RPC methods are supported by
51 * this server:
52 * <ul>
53 * <li>{@link RMIServerInterface#sum(int[]) rmi.sum}</li>
54 * <li>{@link RMIServerInterface#getDate() rmi.getDate}</li>
55 * <li>{@link RMIServerInterface#getVersionInfo(boolean) rmi.getVersion}</li>
56 * </ul>
57 * @param args
58 * A list of parameters indicating
59 * the <code>host/address</code> and
60 * <code>port</code> to bind to. These default to
61 * <code>localhost</code> and <code>8080</code> if
62 * not specified.
63 */
64 public static void main(String[] args) {
65 try {
66 String host = "localhost";
67 int port = 8080;
68
69 if (args != null && args.length > 0) {
70 host = args[0];
71 if (args.length > 1) {
72 port = Integer.parseInt(args[1]);
73 }
74 }
75 System.out.println("Starting server on " + host + ":" + port);
76
77 XmlRpcServer server = new XmlRpcServer(InetAddress.getByName(host), port, true);
78 server.registerHandler(null, "^example\\.", new SSLServerDemo());
79 server.start();
80 } catch (Exception e) {
81 e.printStackTrace();
82 }
83 }
84 }
|