RMISyncServerDemo.java
001 package com.flat502.rox.demo;
002 
003 import java.net.InetAddress;
004 import java.util.Arrays;
005 import java.util.Date;
006 import java.util.Iterator;
007 import java.util.List;
008 
009 import com.flat502.rox.server.XmlRpcServer;
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 RMISyncServerDemo implements RMIServerInterface {
017   /**
018    * Sums an array of integers;
019    @param list
020    *   The list of integers
021    @return
022    *   The sum of the input list of values.
023    */
024   public int sum(int[] list) {
025     System.out.print("sum(" + Utils.toString(list") invoked ... ");
026     int total = 0;
027     for (int i = 0; i < list.length; i++) {
028       total += list[i];
029     }
030     System.out.println("returning " + total);
031     return total;
032   }
033 
034   /**
035    * Fetch a version string.
036    @param verbose
037    *   A flag indicating whether or not the returned
038    *   version info is verbose.
039    @return
040    *   A version string
041    */
042   public String getVersionInfo(boolean verbose) {
043     System.out.print("getVersionInfo(" + verbose + ") invoked ... ");
044     String version = "1.0";
045     if (verbose) {
046       version = "Version " + version;
047     }
048     System.out.println("returning [" + version + "]");
049     return version;
050   }
051 
052   /**
053    * Get the current date and time.
054    @return
055    *   A new {@link Date} instance set to
056    *   the current date and time.
057    */
058   public Date getDate() {
059     System.out.print("getDate() invoked ... ");
060     Date today = new Date();
061     System.out.println("returning " + today);
062     return today;
063   }
064 
065   /**
066    * Start an instance of this demo server.
067    <p>
068    * The following XML-RPC methods are supported by
069    * this server:
070    <ul>
071    <li>{@link RMISyncServerDemo#sum(int[]) rmi.sum}</li>
072    <li>{@link RMISyncServerDemo#getDate() rmi.getDate}</li>
073    <li>{@link RMISyncServerDemo#getVersionInfo(boolean) rmi.getVersion}</li>
074    </ul>
075    @param args
076    *   A list of parameters indicating
077    *   the <code>host/address</code> and
078    *   <code>port</code> to bind to. These default to 
079    *   <code>localhost</code> and <code>8080</code> if
080    *   not specified.
081    */
082   public static void main(String[] args) {
083     try {
084       String host = "localhost";
085       int port = 8080;
086 
087       if (args != null && args.length > 0) {
088         host = args[0];
089         if (args.length > 1) {
090           port = Integer.parseInt(args[1]);
091         }
092       }
093       System.out.println("Starting server on " + host + ":" + port);
094 
095       XmlRpcServer server = new XmlRpcServer(InetAddress.getByName(host), port);
096       server.registerProxyingHandler(null, "^example\\.(.*)"new RMISyncServerDemo());
097       server.start();
098     catch (Exception e) {
099       e.printStackTrace();
100     }
101   }
102 }