Thrift: Change Java/C++ server generation

Summary: Servers shouldn't implement iface directly, instead should contain an iface object


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664785 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/java/src/TestServer.java b/test/java/src/TestServer.java
index 4e415ae..7418cbb 100644
--- a/test/java/src/TestServer.java
+++ b/test/java/src/TestServer.java
@@ -16,207 +16,212 @@
 import java.util.HashMap;
 import java.util.HashSet;
 
-public class TestServer extends ThriftTest.Server {
-  public TestServer(TProtocol prot) {
-    super(prot);
-  }
-  
-  public void testVoid() {
-    System.out.print("testVoid()\n");
-  }
+public class TestServer {
 
-  public String testString(String thing) {
-    System.out.print("testString(\"" + thing + "\")\n");
-    return thing;
-  }
+  public static class TestHandler implements ThriftTest.Iface {
 
-  public byte testByte(byte thing) {
-    System.out.print("testByte(" + thing + ")\n");
-    return thing;
-  }
+    public TestHandler() {}
+  
+    public void testVoid() {
+      System.out.print("testVoid()\n");
+    }
 
-  public int testI32(int thing) {
-    System.out.print("testI32(" + thing + ")\n");
-    return thing;
-  }
+    public String testString(String thing) {
+      System.out.print("testString(\"" + thing + "\")\n");
+      return thing;
+    }
 
-  public long testI64(long thing) {
-    System.out.print("testI64(" + thing + ")\n");
-    return thing;
-  }
+    public byte testByte(byte thing) {
+      System.out.print("testByte(" + thing + ")\n");
+      return thing;
+    }
+
+    public int testI32(int thing) {
+      System.out.print("testI32(" + thing + ")\n");
+      return thing;
+    }
+
+    public long testI64(long thing) {
+      System.out.print("testI64(" + thing + ")\n");
+      return thing;
+    }
   
-  public Xtruct testStruct(Xtruct thing) {
-    System.out.print("testStruct({" +
-                     "\"" + thing.string_thing + "\", " +
-                     thing.byte_thing + ", " +
-                     thing.i32_thing + ", " +
-                     thing.i64_thing + "})\n");
-    return thing;
-  }
+    public Xtruct testStruct(Xtruct thing) {
+      System.out.print("testStruct({" +
+                       "\"" + thing.string_thing + "\", " +
+                       thing.byte_thing + ", " +
+                       thing.i32_thing + ", " +
+                       thing.i64_thing + "})\n");
+      return thing;
+    }
   
-  public Xtruct2 testNest(Xtruct2 nest) {
-    Xtruct thing = nest.struct_thing;
-    System.out.print("testNest({" +
-                     nest.byte_thing + ", {" +
-                     "\"" + thing.string_thing + "\", " +
-                     thing.byte_thing + ", " +
-                     thing.i32_thing + ", " +
-                     thing.i64_thing + "}, " +
-                     nest.i32_thing + "})\n");
-    return nest;
-  }
+    public Xtruct2 testNest(Xtruct2 nest) {
+      Xtruct thing = nest.struct_thing;
+      System.out.print("testNest({" +
+                       nest.byte_thing + ", {" +
+                       "\"" + thing.string_thing + "\", " +
+                       thing.byte_thing + ", " +
+                       thing.i32_thing + ", " +
+                       thing.i64_thing + "}, " +
+                       nest.i32_thing + "})\n");
+      return nest;
+    }
   
-  public HashMap<Integer,Integer> testMap(HashMap<Integer,Integer> thing) {
-    System.out.print("testMap({");
-    boolean first = true;
-    for (int key : thing.keySet()) {
-      if (first) {
-        first = false;
-      } else {
-        System.out.print(", ");
+    public HashMap<Integer,Integer> testMap(HashMap<Integer,Integer> thing) {
+      System.out.print("testMap({");
+      boolean first = true;
+      for (int key : thing.keySet()) {
+        if (first) {
+          first = false;
+        } else {
+          System.out.print(", ");
+        }
+        System.out.print(key + " => " + thing.get(key));
       }
-      System.out.print(key + " => " + thing.get(key));
+      System.out.print("})\n");
+      return thing;
     }
-    System.out.print("})\n");
-    return thing;
-  }
 
-  public HashSet<Integer> testSet(HashSet<Integer> thing) {
-    System.out.print("testSet({");
-    boolean first = true;
-    for (int elem : thing) {
-      if (first) {
-        first = false;
-      } else {
-        System.out.print(", ");
+    public HashSet<Integer> testSet(HashSet<Integer> thing) {
+      System.out.print("testSet({");
+      boolean first = true;
+      for (int elem : thing) {
+        if (first) {
+          first = false;
+        } else {
+          System.out.print(", ");
+        }
+        System.out.print(elem);
       }
-      System.out.print(elem);
+      System.out.print("})\n");
+      return thing;
     }
-    System.out.print("})\n");
-    return thing;
-  }
 
-  public ArrayList<Integer> testList(ArrayList<Integer> thing) {
-    System.out.print("testList({");
-    boolean first = true;
-    for (int elem : thing) {
-      if (first) {
-        first = false;
-      } else {
-        System.out.print(", ");
+    public ArrayList<Integer> testList(ArrayList<Integer> thing) {
+      System.out.print("testList({");
+      boolean first = true;
+      for (int elem : thing) {
+        if (first) {
+          first = false;
+        } else {
+          System.out.print(", ");
+        }
+        System.out.print(elem);
       }
-      System.out.print(elem);
-    }
-    System.out.print("})\n");
-    return thing;
-  }
-
-  public int testEnum(int thing) {
-    System.out.print("testEnum(" + thing + ")\n");
-    return thing;
-  }
-
-  public long testTypedef(long thing) {
-    System.out.print("testTypedef(" + thing + ")\n");
-    return thing;
-  }
-
-  public HashMap<Integer,HashMap<Integer,Integer>> testMapMap(int hello) {
-    System.out.print("testMapMap(" + hello + ")\n");
-    HashMap<Integer,HashMap<Integer,Integer>> mapmap =
-      new HashMap<Integer,HashMap<Integer,Integer>>();
-
-    HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
-    HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
-    for (int i = 1; i < 5; i++) {
-      pos.put(i, i);
-      neg.put(-i, -i);
+      System.out.print("})\n");
+      return thing;
     }
 
-    mapmap.put(4, pos);
-    mapmap.put(-4, neg);
+    public int testEnum(int thing) {
+      System.out.print("testEnum(" + thing + ")\n");
+      return thing;
+    }
 
-    return mapmap;
-  }
+    public long testTypedef(long thing) {
+      System.out.print("testTypedef(" + thing + ")\n");
+      return thing;
+    }
 
-  public HashMap<Long, HashMap<Integer,Insanity>> testInsanity(Insanity argument) {
-    System.out.print("testInsanity()\n");
+    public HashMap<Integer,HashMap<Integer,Integer>> testMapMap(int hello) {
+      System.out.print("testMapMap(" + hello + ")\n");
+      HashMap<Integer,HashMap<Integer,Integer>> mapmap =
+        new HashMap<Integer,HashMap<Integer,Integer>>();
+
+      HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
+      HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
+      for (int i = 1; i < 5; i++) {
+        pos.put(i, i);
+        neg.put(-i, -i);
+      }
+
+      mapmap.put(4, pos);
+      mapmap.put(-4, neg);
+
+      return mapmap;
+    }
+
+    public HashMap<Long, HashMap<Integer,Insanity>> testInsanity(Insanity argument) {
+      System.out.print("testInsanity()\n");
     
-    Xtruct hello = new Xtruct();
-    hello.string_thing = "Hello2";
-    hello.byte_thing = 2;
-    hello.i32_thing = 2;
-    hello.i64_thing = 2;
+      Xtruct hello = new Xtruct();
+      hello.string_thing = "Hello2";
+      hello.byte_thing = 2;
+      hello.i32_thing = 2;
+      hello.i64_thing = 2;
 
-    Xtruct goodbye = new Xtruct();
-    goodbye.string_thing = "Goodbye4";
-    goodbye.byte_thing = (byte)4;
-    goodbye.i32_thing = 4;
-    goodbye.i64_thing = (long)4;
+      Xtruct goodbye = new Xtruct();
+      goodbye.string_thing = "Goodbye4";
+      goodbye.byte_thing = (byte)4;
+      goodbye.i32_thing = 4;
+      goodbye.i64_thing = (long)4;
 
-    Insanity crazy = new Insanity();
-    crazy.userMap.put(Numberz.EIGHT, (long)8);
-    crazy.xtructs.add(goodbye);
+      Insanity crazy = new Insanity();
+      crazy.userMap.put(Numberz.EIGHT, (long)8);
+      crazy.xtructs.add(goodbye);
 
-    Insanity looney = new Insanity();
-    crazy.userMap.put(Numberz.FIVE, (long)5);
-    crazy.xtructs.add(hello);
+      Insanity looney = new Insanity();
+      crazy.userMap.put(Numberz.FIVE, (long)5);
+      crazy.xtructs.add(hello);
 
-    HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>();
-    HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();;
+      HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>();
+      HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();;
 
-    first_map.put(Numberz.TWO, crazy);
-    first_map.put(Numberz.THREE, crazy);
+      first_map.put(Numberz.TWO, crazy);
+      first_map.put(Numberz.THREE, crazy);
 
-    second_map.put(Numberz.SIX, looney);
+      second_map.put(Numberz.SIX, looney);
 
-    HashMap<Long,HashMap<Integer,Insanity>> insane =
-      new HashMap<Long, HashMap<Integer,Insanity>>();
-    insane.put((long)1, first_map);
-    insane.put((long)2, second_map);
+      HashMap<Long,HashMap<Integer,Insanity>> insane =
+        new HashMap<Long, HashMap<Integer,Insanity>>();
+      insane.put((long)1, first_map);
+      insane.put((long)2, second_map);
 
-    return insane;
-  }
+      return insane;
+    }
 
-  public Xtruct testMulti(byte arg0, int arg1, long arg2, HashMap<Short,String> arg3, int arg4, long arg5) {
-    System.out.print("testMulti()\n");
+    public Xtruct testMulti(byte arg0, int arg1, long arg2, HashMap<Short,String> arg3, int arg4, long arg5) {
+      System.out.print("testMulti()\n");
     
-    Xtruct hello = new Xtruct();;
-    hello.string_thing = "Hello2";
-    hello.byte_thing = arg0;
-    hello.i32_thing = arg1;
-    hello.i64_thing = arg2;
-    return hello;
-  }
-
-  public void testException(String arg) throws Xception {
-    System.out.print("testException("+arg+")\n");
-    if (arg.equals("Xception")) {
-      Xception x = new Xception();
-      x.errorCode = 1001;
-      x.message = "This is an Xception";
-      throw x;
+      Xtruct hello = new Xtruct();;
+      hello.string_thing = "Hello2";
+      hello.byte_thing = arg0;
+      hello.i32_thing = arg1;
+      hello.i64_thing = arg2;
+      return hello;
     }
-    return;
-  }
 
-  public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 {
-    System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n");
-    if (arg0.equals("Xception")) {
-      Xception x = new Xception();
-      x.errorCode = 1001;
-      x.message = "This is an Xception";
-    } else if (arg0.equals("Xception2")) {
-      Xception2 x = new Xception2();
-      x.errorCode = 2002;
-      x.struct_thing.string_thing = "This is an Xception2";
+    public void testException(String arg) throws Xception {
+      System.out.print("testException("+arg+")\n");
+      if (arg.equals("Xception")) {
+        Xception x = new Xception();
+        x.errorCode = 1001;
+        x.message = "This is an Xception";
+        throw x;
+      }
+      return;
     }
+
+    public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 {
+      System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n");
+      if (arg0.equals("Xception")) {
+        Xception x = new Xception();
+        x.errorCode = 1001;
+        x.message = "This is an Xception";
+        throw x;
+      } else if (arg0.equals("Xception2")) {
+        Xception2 x = new Xception2();
+        x.errorCode = 2002;
+        x.struct_thing = new Xtruct();
+        x.struct_thing.string_thing = "This is an Xception2";
+        throw x;
+      }
      
-    Xtruct result = new Xtruct();
-    result.string_thing = arg1;
-    return result;
-  }
+      Xtruct result = new Xtruct();
+      result.string_thing = arg1;
+      return result;
+    }
 
+  } // class TestHandler
 
   public static void main(String [] args) {
     try {
@@ -226,20 +231,26 @@
       }
       
       // Processor
-      TBinaryProtocol binaryProtocol = new TBinaryProtocol();
-      TestServer testServer = new TestServer(binaryProtocol);
+      TBinaryProtocol binaryProtocol =
+        new TBinaryProtocol();
+      TestHandler testHandler =
+        new TestHandler();
+      ThriftTest.Server testServer =
+        new ThriftTest.Server(testHandler, binaryProtocol);
 
       // Options
-      TServer.Options serverOptions = new TServer.Options();
+      TServer.Options serverOptions =
+        new TServer.Options();
 
       // Transport
-      ServerSocket serverSocket = new ServerSocket(port);
-      TServerSocket tServerSocket = new TServerSocket(serverSocket);
+      ServerSocket serverSocket =
+        new ServerSocket(port);
+      TServerSocket tServerSocket =
+        new TServerSocket(serverSocket);
 
       // Server
-      TSimpleServer simpleServer = new TSimpleServer(testServer,
-                                                     serverOptions,
-                                                     tServerSocket);
+      TSimpleServer simpleServer =
+        new TSimpleServer(testServer, serverOptions, tServerSocket);
 
       // Run it
       System.out.println("Starting the server on port " + port + "...");