001 package com.flat502.rox.demo;
002
003 import java.net.InetAddress;
004 import java.util.Date;
005
006 import com.flat502.rox.marshal.FieldNameCodec;
007 import com.flat502.rox.marshal.MethodCallUnmarshallerAid;
008 import com.flat502.rox.marshal.RpcCall;
009 import com.flat502.rox.server.*;
010 import com.flat502.rox.utils.Utils;
011
012 /**
013 * A demo server illustrating the {@link com.flat502.rox.server.ProxyingRequestHandler}
014 * class.
015 */
016 public class RMIAsyncServerDemo extends MethodCallUnmarshallerAid implements AsynchronousRequestHandler,
017 RMIServerInterface {
018 private static final String NAME_PATTERN = "^example\\.(.*)";
019 private XmlRpcMethodProxy proxy;
020
021 public RMIAsyncServerDemo() {
022 // Create a proxy for the object that we actually want
023 // to handle XML-RPC methods. This does the mapping
024 // between XML-RPC metho calls and Java method calls.
025 this.proxy = new XmlRpcMethodProxy(NAME_PATTERN, this);
026 }
027
028 public void handleRequest(RpcCall call, RpcCallContext context, ResponseChannel rspChannel) throws Exception {
029 // A deferred method call can be handed off to an application thread ...
030 DeferredMethodCall defCall = new DeferredMethodCall(this.proxy, call, rspChannel);
031
032 Object[] params = call.getParameters();
033 System.out.println("Method [" + call.getName() + "] called with "
034 + params.length + " parameters");
035 for (int i = 0; i < params.length; i++) {
036 System.out.println(" Param " + (i + 1) + " [" + params[i] + "]");
037 }
038
039 // ... and executed like this.
040 defCall.invoke();
041 }
042
043 public Class getType(String methodName, int index) {
044 return this.proxy.getType(methodName, index);
045 }
046
047 public FieldNameCodec getFieldNameCodec(String methodName) {
048 // The default codec is fine.
049 return null;
050 }
051
052 /**
053 * Sums an array of integers;
054 * @param list
055 * The list of integers
056 * @return
057 * The sum of the input list of values.
058 */
059 public int sum(int[] list) {
060 System.out.print("sum(" + Utils.toString(list) + ") invoked ... ");
061 int total = 0;
062 for (int i = 0; i < list.length; i++) {
063 total += list[i];
064 }
065 System.out.println("returning " + total);
066 return total;
067 }
068
069 /**
070 * Fetch a version string.
071 * @param verbose
072 * A flag indicating whether or not the returned
073 * version info is verbose.
074 * @return
075 * A version string
076 */
077 public String getVersionInfo(boolean verbose) {
078 System.out.print("getVersionInfo(" + verbose + ") invoked ... ");
079 String version = "1.0";
080 if (verbose) {
081 version = "Version " + version;
082 }
083 System.out.println("returning [" + version + "]");
084 return version;
085 }
086
087 /**
088 * Get the current date and time.
089 * @return
090 * A new {@link Date} instance set to
091 * the current date and time.
092 */
093 public Date getDate() {
094 System.out.print("getDate() invoked ... ");
095 Date today = new Date();
096 System.out.println("returning " + today);
097 return today;
098 }
099
100 /**
101 * Start an instance of this demo server.
102 * <p>
103 * The following XML-RPC methods are supported by
104 * this server:
105 * <ul>
106 * <li>{@link RMIAsyncServerDemo#sum(int[]) rmi.sum}</li>
107 * <li>{@link RMIAsyncServerDemo#getDate() rmi.getDate}</li>
108 * <li>{@link RMIAsyncServerDemo#getVersionInfo(boolean) rmi.getVersion}</li>
109 * </ul>
110 * @param args
111 * A list of parameters indicating
112 * the <code>host/address</code> and
113 * <code>port</code> to bind to. These default to
114 * <code>localhost</code> and <code>8080</code> if
115 * not specified.
116 */
117 public static void main(String[] args) {
118 try {
119 String host = "localhost";
120 int port = 8080;
121
122 if (args != null && args.length > 0) {
123 host = args[0];
124 if (args.length > 1) {
125 port = Integer.parseInt(args[1]);
126 }
127 }
128 System.out.println("Starting server on " + host + ":" + port);
129
130 XmlRpcServer server = new XmlRpcServer(InetAddress.getByName(host), port);
131 RMIAsyncServerDemo handler = new RMIAsyncServerDemo();
132 server.registerHandler(null, NAME_PATTERN, handler, handler);
133 server.start();
134 } catch (Exception e) {
135 e.printStackTrace();
136 }
137 }
138 }
|