New protocol wrapping transport model for Thrift Java


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664846 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/src/server/TServer.java b/lib/java/src/server/TServer.java
index 3cae00b..5ef96d0 100644
--- a/lib/java/src/server/TServer.java
+++ b/lib/java/src/server/TServer.java
@@ -1,9 +1,10 @@
 package com.facebook.thrift.server;
 
 import com.facebook.thrift.TProcessor;
+import com.facebook.thrift.protocol.TBinaryProtocol;
+import com.facebook.thrift.protocol.TProtocolFactory;
 import com.facebook.thrift.transport.TServerTransport;
 import com.facebook.thrift.transport.TTransportFactory;
-import com.facebook.thrift.transport.TBaseTransportFactory;
 
 /**
  * Generic interface for a Thrift server.
@@ -13,24 +14,11 @@
 public abstract class TServer {
 
   /**
-   * The options class should be subclassed by particular servers which have
-   * specific options needs, while the general options should live here.
-   */
-  public static class Options {
-    public Options() {}
-  }
-
-  /**
    * Core processor
    */
   protected TProcessor processor_;
 
   /**
-   * Server options
-   */
-  protected Options options_;
-
-  /**
    * Server transport
    */
   protected TServerTransport serverTransport_;
@@ -41,6 +29,11 @@
   protected TTransportFactory transportFactory_;
 
   /**
+   * Protocol Factory
+   */
+  protected TProtocolFactory protocolFactory_;
+
+  /**
    * Default constructors.
    */
 
@@ -48,8 +41,8 @@
                     TServerTransport serverTransport) {
     this(processor,
          serverTransport,
-         new TBaseTransportFactory(),
-         new Options());
+         new TTransportFactory(),
+         new TBinaryProtocol.Factory());
   }
 
   protected TServer(TProcessor processor,
@@ -58,31 +51,22 @@
     this(processor,
          serverTransport,
          transportFactory,
-         new Options());
-  }
-
-
-  protected TServer(TProcessor processor,
-                    TServerTransport serverTransport,
-                    Options options) {
-    this(processor,
-         serverTransport,
-         new TBaseTransportFactory(),
-         options);
+         new TBinaryProtocol.Factory());
   }
 
   protected TServer(TProcessor processor,
                     TServerTransport serverTransport,
                     TTransportFactory transportFactory,
-                    Options options) {
+                    TProtocolFactory protocolFactory) {
     processor_ = processor;
     serverTransport_ = serverTransport;
     transportFactory_ = transportFactory;
-    options_ = options;
+    protocolFactory_ = protocolFactory;
   }
   
   /**
    * The run method fires up the server and gets things going.
    */
   public abstract void serve();
+
 }
diff --git a/lib/java/src/server/TSimpleServer.java b/lib/java/src/server/TSimpleServer.java
index 548ca09..7ecd347 100644
--- a/lib/java/src/server/TSimpleServer.java
+++ b/lib/java/src/server/TSimpleServer.java
@@ -2,6 +2,8 @@
 
 import com.facebook.thrift.TException;
 import com.facebook.thrift.TProcessor;
+import com.facebook.thrift.protocol.TProtocol;
+import com.facebook.thrift.protocol.TProtocolFactory;
 import com.facebook.thrift.transport.TServerTransport;
 import com.facebook.thrift.transport.TTransport;
 import com.facebook.thrift.transport.TTransportException;
@@ -28,12 +30,14 @@
 
     while (true) {
       TTransport client = null;
-      TTransport[] io = null;
+      TTransport[] iot = null;
+      TProtocol[] iop = null;
       try {
         client = serverTransport_.accept();
         if (client != null) {
-          io = transportFactory_.getIOTransports(client);
-          while (processor_.process(io[0], io[1]));
+          iot = transportFactory_.getIOTransports(client);
+          iop = protocolFactory_.getIOProtocols(iot[0], iot[1]);
+          while (processor_.process(iop[0], iop[1]));
         }
       } catch (TTransportException ttx) {
         // Client died, just move on
@@ -43,12 +47,12 @@
         x.printStackTrace();
       }
 
-      if (io != null) {
-        if (io[0] != null) {
-          io[0].close();
+      if (iot != null) {
+        if (iot[0] != null) {
+          iot[0].close();
         }
-        if (io[1] != null) {
-          io[1].close();
+        if (iot[1] != null) {
+          iot[1].close();
         }
       }
     }
diff --git a/lib/java/src/server/TThreadPoolServer.java b/lib/java/src/server/TThreadPoolServer.java
index c63d1e1..090859d 100644
--- a/lib/java/src/server/TThreadPoolServer.java
+++ b/lib/java/src/server/TThreadPoolServer.java
@@ -2,11 +2,12 @@
 
 import com.facebook.thrift.TException;
 import com.facebook.thrift.TProcessor;
+import com.facebook.thrift.protocol.TProtocol;
+import com.facebook.thrift.protocol.TProtocolFactory;
 import com.facebook.thrift.transport.TServerTransport;
 import com.facebook.thrift.transport.TTransport;
 import com.facebook.thrift.transport.TTransportException;
 import com.facebook.thrift.transport.TTransportFactory;
-import com.facebook.thrift.transport.TBaseTransportFactory;
 
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.LinkedBlockingQueue;
@@ -26,25 +27,21 @@
   private ExecutorService executorService_;
 
   // Customizable server options
-  public static class Options extends TServer.Options {
+  public static class Options {
     public int minWorkerThreads = 5;
     public int maxWorkerThreads = Integer.MAX_VALUE;
   }
 
   public TThreadPoolServer(TProcessor processor,
                            TServerTransport serverTransport) {
-    this(processor,
-         serverTransport,
-         new TBaseTransportFactory(),
-         new Options());
+    this(processor, serverTransport, new Options());
   }
   
   public TThreadPoolServer(TProcessor processor,
                            TServerTransport serverTransport,
-                           TTransportFactory transportFactory,
                            Options options) {
-    super(processor, serverTransport, transportFactory, options);
-    serverTransport_ = serverTransport;
+    super(processor, serverTransport);
+
     executorService_ = null;
 
     LinkedBlockingQueue<Runnable> executorQueue =
@@ -99,10 +96,12 @@
      * Loops on processing a client forever
      */
     public void run() {
-      TTransport[] io = null;
+      TTransport[] iot = null;
+      TProtocol[] iop = null;
       try {
-        io = transportFactory_.getIOTransports(client_);
-        while (processor_.process(io[0], io[1])) {}
+        iot = transportFactory_.getIOTransports(client_);
+        iop = protocolFactory_.getIOProtocols(iot[0], iot[1]);
+        while (processor_.process(iop[0], iop[1])) {}
       } catch (TTransportException ttx) {
         // Assume the client died and continue silently
       } catch (TException tx) {
@@ -111,12 +110,12 @@
         x.printStackTrace();
       }
 
-      if (io != null) {
-        if (io[0] != null) {
-          io[0].close();
+      if (iot != null) {
+        if (iot[0] != null) {
+          iot[0].close();
         }
-        if (io[1] != null) {
-          io[1].close();
+        if (iot[1] != null) {
+          iot[1].close();
         }
       }
     }