THRIFT-378. java: Java servers do not turn internal errors into thrift exceptions

This patch causes Java servers to log internal server errors and return an INTERNAL_ERROR exception instead of just closing the connection.

Author: Jonathan Ellis



git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@785713 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index 419053d..2d1381c 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -279,7 +279,8 @@
     "import java.util.HashMap;\n" +
     "import java.util.Set;\n" +
     "import java.util.HashSet;\n" +
-    "import java.util.Collections;\n\n";
+    "import java.util.Collections;\n" +
+    "import org.apache.log4j.Logger;\n\n";
 }
 
 /**
@@ -1940,6 +1941,8 @@
     "public static class Processor" << extends_processor << " implements TProcessor {" << endl;
   indent_up();
 
+  indent(f_service_) << "private static final Logger LOGGER = Logger.getLogger(Processor.class.getName());" << endl;
+
   indent(f_service_) <<
     "public Processor(Iface iface)" << endl;
   scope_up(f_service_);
@@ -2128,7 +2131,18 @@
         f_service_ << "}";
       }
     }
-    f_service_ << endl;
+    f_service_ << " catch (Throwable th) {" << endl;
+    indent_up();
+    f_service_ <<
+      indent() << "LOGGER.error(\"Internal error processing " << tfunction->get_name() << "\", th);" << endl <<
+      indent() << "TApplicationException x = new TApplicationException(TApplicationException.INTERNAL_ERROR, \"Internal error processing " << tfunction->get_name() << "\");" << endl <<
+      indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl <<
+      indent() << "x.write(oprot);" << endl <<
+      indent() << "oprot.writeMessageEnd();" << endl <<
+      indent() << "oprot.getTransport().flush();" << endl <<
+      indent() << "return;" << endl;
+    indent_down();
+    f_service_ << indent() << "}" << endl;
   }
 
   // Shortcut out here for oneway functions
diff --git a/lib/java/src/org/apache/thrift/TApplicationException.java b/lib/java/src/org/apache/thrift/TApplicationException.java
index a85e370..f840392 100644
--- a/lib/java/src/org/apache/thrift/TApplicationException.java
+++ b/lib/java/src/org/apache/thrift/TApplicationException.java
@@ -43,6 +43,7 @@
   public static final int WRONG_METHOD_NAME = 3;
   public static final int BAD_SEQUENCE_ID = 4;
   public static final int MISSING_RESULT = 5;
+  public static final int INTERNAL_ERROR = 6;
 
   protected int type_ = UNKNOWN;