Author: bryanduxbury
Date: Sat Apr 3 23:19:52 2010
New Revision: 930601
URL: http://svn.apache.org/viewvc?rev=930601&view=rev
Log:
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
Added:
incubator/thrift/trunk/lib/java/src/org/apache/thrift/TServiceClient.java
incubator/thrift/trunk/lib/java/src/org/apache/thrift/TServiceClientFactory.java
Modified:
incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc
Modified: incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc
URL:
http://svn.apache.org/viewvc/incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc?rev=930601&r1=930600&r2=930601&view=diff
==============================================================================
--- incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc
(original)
+++ incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc Sat
Apr 3 23:19:52 2010
@@ -2203,9 +2203,25 @@ void t_java_generator::generate_service_
}
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_);
Added: incubator/thrift/trunk/lib/java/src/org/apache/thrift/TServiceClient.java
URL:
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/src/org/apache/thrift/TServiceClient.java?rev=930601&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/java/src/org/apache/thrift/TServiceClient.java
(added)
+++ incubator/thrift/trunk/lib/java/src/org/apache/thrift/TServiceClient.java
Sat Apr 3 23:19:52 2010
@@ -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();
+}
Added:
incubator/thrift/trunk/lib/java/src/org/apache/thrift/TServiceClientFactory.java
URL:
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/src/org/apache/thrift/TServiceClientFactory.java?rev=930601&view=auto
==============================================================================
---
incubator/thrift/trunk/lib/java/src/org/apache/thrift/TServiceClientFactory.java
(added)
+++
incubator/thrift/trunk/lib/java/src/org/apache/thrift/TServiceClientFactory.java
Sat Apr 3 23:19:52 2010
@@ -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);
+}