From: Bryan Duxbury Date: Sat, 3 Apr 2010 23:19:52 +0000 (+0000) Subject: THRIFT-746. java: Generated services Iface/Client inner classes do not derive from... X-Git-Tag: 0.3.0~42 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=b1f7f7973bed233ec6f21807dfd774f7b600c5ec;p=common%2Fthrift.git 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 35f775b1..5c38c5da 100644 --- a/compiler/cpp/src/generate/t_java_generator.cc +++ b/compiler/cpp/src/generate/t_java_generator.cc @@ -2203,9 +2203,25 @@ void t_java_generator::generate_service_client(t_service* tservice) { } 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 {" << 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 00000000..ee07b782 --- /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 00000000..808d5e60 --- /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 + */ +public interface TServiceClientFactory { + /** + * Get a brand-new T using prot 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); +}