ProcessorFactory for Java

Reviewed By: simon forman


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665124 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/src/server/TServer.java b/lib/java/src/server/TServer.java
index 9dcbff6..d5f95f6 100644
--- a/lib/java/src/server/TServer.java
+++ b/lib/java/src/server/TServer.java
@@ -6,7 +6,7 @@
 
 package com.facebook.thrift.server;
 
-import com.facebook.thrift.TProcessor;
+import com.facebook.thrift.TProcessorFactory;
 import com.facebook.thrift.protocol.TBinaryProtocol;
 import com.facebook.thrift.protocol.TProtocolFactory;
 import com.facebook.thrift.transport.TServerTransport;
@@ -22,7 +22,7 @@
   /**
    * Core processor
    */
-  protected TProcessor processor_;
+  protected TProcessorFactory processorFactory_;
 
   /**
    * Server transport
@@ -53,9 +53,9 @@
    * Default constructors.
    */
 
-  protected TServer(TProcessor processor,
+  protected TServer(TProcessorFactory processorFactory,
                     TServerTransport serverTransport) {
-    this(processor,
+    this(processorFactory,
          serverTransport,         
          new TTransportFactory(),
          new TTransportFactory(),
@@ -63,10 +63,10 @@
          new TBinaryProtocol.Factory());
   }
 
-  protected TServer(TProcessor processor,
+  protected TServer(TProcessorFactory processorFactory,
                     TServerTransport serverTransport,
                     TTransportFactory transportFactory) {
-    this(processor,
+    this(processorFactory,
          serverTransport,
          transportFactory,
          transportFactory,
@@ -74,11 +74,11 @@
          new TBinaryProtocol.Factory());
   }
 
-  protected TServer(TProcessor processor,
+  protected TServer(TProcessorFactory processorFactory,
                     TServerTransport serverTransport,
                     TTransportFactory transportFactory,
                     TProtocolFactory protocolFactory) {
-    this(processor,
+    this(processorFactory,
          serverTransport,
          transportFactory,
          transportFactory,
@@ -86,13 +86,13 @@
          protocolFactory);
   }
 
-  protected TServer(TProcessor processor,
+  protected TServer(TProcessorFactory processorFactory,
                     TServerTransport serverTransport,
                     TTransportFactory inputTransportFactory,
                     TTransportFactory outputTransportFactory,
                     TProtocolFactory inputProtocolFactory,
                     TProtocolFactory outputProtocolFactory) {
-    processor_ = processor;
+    processorFactory_ = processorFactory;
     serverTransport_ = serverTransport;
     inputTransportFactory_ = inputTransportFactory;
     outputTransportFactory_ = outputTransportFactory;
diff --git a/lib/java/src/server/TSimpleServer.java b/lib/java/src/server/TSimpleServer.java
index 902c596..181d8e4 100644
--- a/lib/java/src/server/TSimpleServer.java
+++ b/lib/java/src/server/TSimpleServer.java
@@ -8,6 +8,7 @@
 
 import com.facebook.thrift.TException;
 import com.facebook.thrift.TProcessor;
+import com.facebook.thrift.TProcessorFactory;
 import com.facebook.thrift.protocol.TProtocol;
 import com.facebook.thrift.protocol.TProtocolFactory;
 import com.facebook.thrift.transport.TServerTransport;
@@ -24,14 +25,14 @@
 
   public TSimpleServer(TProcessor processor,
                        TServerTransport serverTransport) {
-    super(processor, serverTransport);
+    super(new TProcessorFactory(processor), serverTransport);
   }
 
   public TSimpleServer(TProcessor processor,
                        TServerTransport serverTransport,
                        TTransportFactory transportFactory,
                        TProtocolFactory protocolFactory) {
-    super(processor, serverTransport, transportFactory, protocolFactory);
+    super(new TProcessorFactory(processor), serverTransport, transportFactory, protocolFactory);
   }
 
   public TSimpleServer(TProcessor processor,
@@ -40,11 +41,35 @@
                        TTransportFactory outputTransportFactory,
                        TProtocolFactory inputProtocolFactory,
                        TProtocolFactory outputProtocolFactory) {                       
-    super(processor, serverTransport, 
+    super(new TProcessorFactory(processor), serverTransport, 
           inputTransportFactory, outputTransportFactory,
           inputProtocolFactory, outputProtocolFactory);
   }
 
+  public TSimpleServer(TProcessorFactory processorFactory,
+          TServerTransport serverTransport) {
+    super(processorFactory, serverTransport);
+  }
+
+  public TSimpleServer(TProcessorFactory processorFactory,
+          TServerTransport serverTransport,
+          TTransportFactory transportFactory,
+          TProtocolFactory protocolFactory) {
+    super(processorFactory, serverTransport, transportFactory, protocolFactory);
+  }
+
+  public TSimpleServer(TProcessorFactory processorFactory,
+          TServerTransport serverTransport,
+          TTransportFactory inputTransportFactory,
+          TTransportFactory outputTransportFactory,
+          TProtocolFactory inputProtocolFactory,
+          TProtocolFactory outputProtocolFactory) {                       
+    super(processorFactory, serverTransport, 
+          inputTransportFactory, outputTransportFactory,
+          inputProtocolFactory, outputProtocolFactory);
+  }
+ 
+  
   public void serve() {
     try {
       serverTransport_.listen();
@@ -55,6 +80,7 @@
 
     while (true) {
       TTransport client = null;
+      TProcessor processor = null;
       TTransport inputTransport = null;
       TTransport outputTransport = null;
       TProtocol inputProtocol = null;
@@ -62,11 +88,12 @@
       try {
         client = serverTransport_.accept();
         if (client != null) {
+          processor = processorFactory_.getProcessor(client);
           inputTransport = inputTransportFactory_.getTransport(client);
           outputTransport = outputTransportFactory_.getTransport(client);
           inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
           outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);
-          while (processor_.process(inputProtocol, outputProtocol)) {}
+          while (processor.process(inputProtocol, outputProtocol)) {}
         }
       } catch (TTransportException ttx) {
         // Client died, just move on
diff --git a/lib/java/src/server/TThreadPoolServer.java b/lib/java/src/server/TThreadPoolServer.java
index 9eaf9b8..22930d5 100644
--- a/lib/java/src/server/TThreadPoolServer.java
+++ b/lib/java/src/server/TThreadPoolServer.java
@@ -8,6 +8,7 @@
 
 import com.facebook.thrift.TException;
 import com.facebook.thrift.TProcessor;
+import com.facebook.thrift.TProcessorFactory;
 import com.facebook.thrift.protocol.TProtocol;
 import com.facebook.thrift.protocol.TProtocolFactory;
 import com.facebook.thrift.protocol.TBinaryProtocol;
@@ -47,6 +48,14 @@
          new Options());
   }
 
+  public TThreadPoolServer(TProcessorFactory processorFactory,
+          TServerTransport serverTransport) {
+    this(processorFactory, serverTransport, 
+         new TTransportFactory(), new TTransportFactory(),
+         new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(),
+         new Options());
+  }
+
   public TThreadPoolServer(TProcessor processor,
                            TServerTransport serverTransport,
                            TTransportFactory transportFactory,
@@ -56,15 +65,39 @@
          protocolFactory, protocolFactory,
          new Options());
   }
+
+  public TThreadPoolServer(TProcessorFactory processorFactory,
+          TServerTransport serverTransport,
+          TTransportFactory transportFactory,
+          TProtocolFactory protocolFactory) {
+    this(processorFactory, serverTransport, 
+         transportFactory, transportFactory,
+         protocolFactory, protocolFactory,
+         new Options());
+  }
+
   
   public TThreadPoolServer(TProcessor processor,
+          TServerTransport serverTransport,
+          TTransportFactory inputTransportFactory,
+          TTransportFactory outputTransportFactory,
+          TProtocolFactory inputProtocolFactory,
+          TProtocolFactory outputProtocolFactory,
+          Options options) {
+    this(new TProcessorFactory(processor), serverTransport,
+         inputTransportFactory, outputTransportFactory,
+         inputProtocolFactory, outputProtocolFactory,
+         options);
+  }
+  
+  public TThreadPoolServer(TProcessorFactory processorFactory,
                            TServerTransport serverTransport,
                            TTransportFactory inputTransportFactory,
                            TTransportFactory outputTransportFactory,
                            TProtocolFactory inputProtocolFactory,
                            TProtocolFactory outputProtocolFactory,
                            Options options) {
-    super(processor, serverTransport, 
+    super(processorFactory, serverTransport, 
           inputTransportFactory, outputTransportFactory,
           inputProtocolFactory, outputProtocolFactory);
 
@@ -122,16 +155,18 @@
      * Loops on processing a client forever
      */
     public void run() {
+      TProcessor processor = null;
       TTransport inputTransport = null;
       TTransport outputTransport = null;
       TProtocol inputProtocol = null;
       TProtocol outputProtocol = null;
       try {
+        processor = processorFactory_.getProcessor(client_);
         inputTransport = inputTransportFactory_.getTransport(client_);
         outputTransport = outputTransportFactory_.getTransport(client_);
         inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
         outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);
-        while (processor_.process(inputProtocol, outputProtocol)) {}
+        while (processor.process(inputProtocol, outputProtocol)) {}
       } catch (TTransportException ttx) {
         // Assume the client died and continue silently
       } catch (TException tx) {