THRIFT-746. java: Generated services Iface/Client inner classes do not derive from base classes
This patch causes all generated Client classes to inherit from TServiceClient, an interface that provides a way to get the protocols the Client is using. Also, it causes a new TServiceClientFactory implementation to generated for each Service, which provides a generic, reflection-free way to get Clients. These changes make it easier to build generic pools of Client objects.
Patch: Mathias Herberts
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@930601 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 35f775b..5c38c5d 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -2203,9 +2203,25 @@
}
indent(f_service_) <<
- "public static class Client" << extends_client << " implements Iface {" << endl;
+ "public static class Client" << extends_client << " implements TServiceClient, Iface {" << endl;
indent_up();
+ indent(f_service_) << "public static class Factory implements TServiceClientFactory<Client> {" << endl;
+ indent_up();
+ indent(f_service_) << "public Factory() {}" << endl;
+ indent(f_service_) << "public Client getClient(TProtocol prot) {" << endl;
+ indent_up();
+ indent(f_service_) << "return new Client(prot);" << endl;
+ indent_down();
+ indent(f_service_) << "}" << endl;
+ indent(f_service_) << "public Client getClient(TProtocol iprot, TProtocol oprot) {" << endl;
+ indent_up();
+ indent(f_service_) << "return new Client(iprot, oprot);" << endl;
+ indent_down();
+ indent(f_service_) << "}" << endl;
+ indent_down();
+ indent(f_service_) << "}" << endl << endl;
+
indent(f_service_) <<
"public Client(TProtocol prot)" << endl;
scope_up(f_service_);
diff --git a/lib/java/src/org/apache/thrift/TServiceClient.java b/lib/java/src/org/apache/thrift/TServiceClient.java
new file mode 100644
index 0000000..ee07b78
--- /dev/null
+++ b/lib/java/src/org/apache/thrift/TServiceClient.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import org.apache.thrift.protocol.TProtocol;
+
+/**
+ * A TServiceClient is used to communicate with a TService implementation
+ * across protocols and transports.
+ */
+public interface TServiceClient {
+ /**
+ * Get the TProtocol being used as the input (read) protocol.
+ * @return
+ */
+ public TProtocol getInputProtocol();
+ /**
+ * Get the TProtocol being used as the output (write) protocol.
+ * @return
+ */
+ public TProtocol getOutputProtocol();
+}
diff --git a/lib/java/src/org/apache/thrift/TServiceClientFactory.java b/lib/java/src/org/apache/thrift/TServiceClientFactory.java
new file mode 100644
index 0000000..808d5e6
--- /dev/null
+++ b/lib/java/src/org/apache/thrift/TServiceClientFactory.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import org.apache.thrift.protocol.TProtocol;
+
+/**
+ * A TServiceClientFactory provides a general way to get a TServiceClient
+ * connected to a remote TService via a protocol.
+ * @param <T>
+ */
+public interface TServiceClientFactory<T extends TServiceClient> {
+ /**
+ * Get a brand-new T using <i>prot</i> as both the input and output protocol.
+ * @param prot
+ * @return
+ */
+ public T getClient(TProtocol prot);
+
+ /**
+ * Get a brand new T using the specified input and output protocols. The
+ * input and output protocols may be the same instance.
+ * @param iprot
+ * @param oprot
+ * @return
+ */
+ public T getClient(TProtocol iprot, TProtocol oprot);
+}