thrift/compiler/generate/templates/java/ServiceClient.mustache (199 lines of code) (raw):

{{! Copyright (c) Facebook, Inc. and its affiliates. Licensed 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. }} {{> AutoGenerated}} package {{service:javaPackage}}; import com.facebook.nifty.client.RequestChannel; import com.facebook.swift.codec.*; import com.facebook.swift.service.*; import com.facebook.swift.service.metadata.*; import com.facebook.thrift.client.*; import com.facebook.thrift.util.FutureUtil; import java.io.*; import java.lang.reflect.Method; import java.util.*; import org.apache.thrift.ProtocolId; import reactor.core.publisher.Mono; @SwiftGenerated public class {{service:javaCapitalName}}ClientImpl extends {{^service:extends}}AbstractThriftClient{{/service:extends}}{{#service:extends}}{{service:javaPackage}}.{{service:javaCapitalName}}ClientImpl{{/service:extends}} implements {{service:javaCapitalName}} { // Method Handlers {{#service:singleRequestFunctions}} private ThriftMethodHandler {{function:javaName}}MethodHandler; {{/service:singleRequestFunctions}} {{#service:interactions}} // Interaction Handlers {{#service:singleRequestFunctions}} private ThriftMethodHandler {{function:javaName}}IMethodHandler; {{/service:singleRequestFunctions}} {{/service:interactions}} // Method Exceptions {{#service:singleRequestFunctions}} private static final Class[] {{function:javaName}}Exceptions = new Class[] { {{#function:exceptions}}{{#field:type}}{{> BoxedType}}.class{{/field:type}}, {{/function:exceptions}}org.apache.thrift.TException.class}; {{/service:singleRequestFunctions}} {{#service:interactions}} // Interaction Exceptions {{#service:singleRequestFunctions}} private static final Class[] {{function:javaName}}IExceptions = new Class[] { {{#function:exceptions}}{{#field:type}}{{> BoxedType}}.class{{/field:type}}, {{/function:exceptions}}org.apache.thrift.TException.class}; {{/service:singleRequestFunctions}} {{/service:interactions}} public {{service:javaCapitalName}}ClientImpl( RequestChannel channel, Map<Method, ThriftMethodHandler> methods, Map<String, String> headers, Map<String, String> persistentHeaders, List<? extends ThriftClientEventHandler> eventHandlers) { {{^service:extends}}super(channel, headers, persistentHeaders, eventHandlers);{{/service:extends}}{{#service:extends}}super(channel, methods, headers, persistentHeaders, eventHandlers);{{/service:extends}} Map<String, ThriftMethodHandler> methodHandlerMap = new HashMap<>(); methods.forEach( (key, value) -> { methodHandlerMap.put(key.getName(), value); }); // Set method handlers {{#service:singleRequestFunctions}} {{function:javaName}}MethodHandler = methodHandlerMap.get("{{function:javaName}}"); {{/service:singleRequestFunctions}} {{#service:interactions}} // Set interaction handlers {{#service:singleRequestFunctions}} {{function:javaName}}IMethodHandler = methodHandlerMap.get("{{function:javaName}}"); {{/service:singleRequestFunctions}} {{/service:interactions}} } public {{service:javaCapitalName}}ClientImpl( Map<String, String> headers, Map<String, String> persistentHeaders, Mono<? extends RpcClient> rpcClient, ThriftServiceMetadata serviceMetadata, ThriftCodecManager codecManager, ProtocolId protocolId, Map<Method, ThriftMethodHandler> methods) { {{^service:extends}}super(headers, persistentHeaders, rpcClient, serviceMetadata, codecManager, protocolId);{{/service:extends}}{{#service:extends}}super(headers, persistentHeaders, rpcClient, serviceMetadata, codecManager, protocolId, methods);{{/service:extends}} Map<String, ThriftMethodHandler> methodHandlerMap = new HashMap<>(); methods.forEach( (key, value) -> { methodHandlerMap.put(key.getName(), value); }); // Set method handlers {{#service:singleRequestFunctions}} {{function:javaName}}MethodHandler = methodHandlerMap.get("{{function:javaName}}"); {{/service:singleRequestFunctions}} {{#service:interactions}} // Set interaction handlers {{#service:singleRequestFunctions}} {{function:javaName}}IMethodHandler = methodHandlerMap.get("{{function:javaName}}"); {{/service:singleRequestFunctions}} {{/service:interactions}} } @java.lang.Override public void close() { super.close(); } {{#service:singleRequestFunctions}} @java.lang.Override public {{#function:return_type}}{{> Type}}{{/function:return_type}} {{function:javaName}}({{#function:args}} {{#field:type}}{{> Type}}{{/field:type}} {{field:javaName}}{{^last?}},{{/last?}}{{/function:args}}){{! Exceptions }} throws {{#function:exceptions}}{{#field:type}}{{> BoxedType}}{{/field:type}}, {{/function:exceptions}}org.apache.thrift.TException { {{#function:return_type}}{{^type:void?}}return {{/type:void?}}{{/function:return_type}}{{function:javaName}}Wrapper({{#function:args}}{{field:javaName}}, {{/function:args}}RpcOptions.EMPTY).getData(); } @java.lang.Override public {{#function:return_type}}{{> Type}}{{/function:return_type}} {{function:javaName}}({{#function:args}} {{#field:type}}{{> Type}}{{/field:type}} {{field:javaName}},{{/function:args}} RpcOptions rpcOptions){{! Exceptions }} throws {{#function:exceptions}}{{#field:type}}{{> BoxedType}}{{/field:type}}, {{/function:exceptions}}org.apache.thrift.TException { {{#function:return_type}}{{^type:void?}}return {{/type:void?}}{{/function:return_type}}{{function:javaName}}Wrapper({{#function:args}}{{field:javaName}}, {{/function:args}}rpcOptions).getData(); } @java.lang.Override public ResponseWrapper<{{#function:return_type}}{{> BoxedType}}{{/function:return_type}}> {{function:javaName}}Wrapper({{#function:args}} {{#field:type}}{{> Type}}{{/field:type}} {{field:javaName}},{{/function:args}} RpcOptions rpcOptions){{! Exceptions }} throws {{#function:exceptions}}{{#field:type}}{{> BoxedType}}{{/field:type}}, {{/function:exceptions}}org.apache.thrift.TException { try { return FutureUtil.get(executeWrapperWithOptions({{function:javaName}}MethodHandler, {{function:javaName}}Exceptions, rpcOptions{{#function:args}}, {{field:javaName}}{{/function:args}})); } catch (Throwable t) { if (t instanceof org.apache.thrift.TException) { throw (org.apache.thrift.TException) t; } {{#function:exceptions}} if (t instanceof {{#field:type}}{{> BoxedType}}{{/field:type}}) { throw ({{#field:type}}{{> BoxedType}}{{/field:type}}) t; } {{/function:exceptions}} throw new org.apache.thrift.TException(t); } } {{/service:singleRequestFunctions}} {{#service:interactions}} public class {{service:name}}Impl implements {{service:name}} { private final long interactionId; {{service:name}}Impl(long interactionId) { this.interactionId = interactionId; } {{#service:singleRequestFunctions}} public {{#function:return_type}}{{> Type}}{{/function:return_type}} {{function:javaName}}({{#function:args}} final {{#field:type}}{{> Type}}{{/field:type}} {{field:javaName}}{{^last?}},{{/last?}}{{/function:args}}) throws {{#function:exceptions}}{{#field:type}}{{> BoxedType}}{{/field:type}}, {{/function:exceptions}}org.apache.thrift.TException { {{#function:return_type}}{{^type:void?}}return {{/type:void?}}{{/function:return_type}}{{function:javaName}}Wrapper({{#function:args}}{{field:javaName}}, {{/function:args}}RpcOptions.EMPTY).getData(); } public {{#function:return_type}}{{> Type}}{{/function:return_type}} {{function:javaName}}({{#function:args}} final {{#field:type}}{{> Type}}{{/field:type}} {{field:javaName}},{{/function:args}} RpcOptions rpcOptions){{! Exceptions }} throws {{#function:exceptions}}{{#field:type}}{{> BoxedType}}{{/field:type}}, {{/function:exceptions}}org.apache.thrift.TException { {{#function:return_type}}{{^type:void?}}return {{/type:void?}}{{/function:return_type}}{{function:javaName}}Wrapper({{#function:args}}{{field:javaName}}, {{/function:args}}rpcOptions).getData(); } public ResponseWrapper<{{#function:return_type}}{{> BoxedType}}{{/function:return_type}}> {{function:javaName}}Wrapper({{#function:args}} {{#field:type}}{{> Type}}{{/field:type}} {{field:javaName}},{{/function:args}} RpcOptions _rpcOptions){{! Exceptions }} throws {{#function:exceptions}}{{#field:type}}{{> BoxedType}}{{/field:type}}, {{/function:exceptions}}org.apache.thrift.TException { try { RpcOptions rpcOptions = updateRpcOptions(_rpcOptions); return FutureUtil.get(executeWrapperWithOptions({{function:javaName}}IMethodHandler, {{function:javaName}}IExceptions, rpcOptions{{#function:args}}, {{field:javaName}}{{/function:args}})); } catch (Throwable t) { if (t instanceof org.apache.thrift.TException) { throw (org.apache.thrift.TException) t; } {{#function:exceptions}} if (t instanceof {{#field:type}}{{> BoxedType}}{{/field:type}}) { throw ({{#field:type}}{{> BoxedType}}{{/field:type}}) t; } {{/function:exceptions}} throw new org.apache.thrift.TException(t); } } {{/service:singleRequestFunctions}} @java.lang.Override public void close() { activeInteractions.remove(interactionId); } private RpcOptions updateRpcOptions(RpcOptions _rpcOptions) { RpcOptions.Builder builder = new RpcOptions.Builder(_rpcOptions); if (activeInteractions.contains(interactionId)) { builder.setInteractionId(interactionId); } else { builder.setCreateInteractionId(interactionId).setInteractionId(0L); activeInteractions.add(interactionId); } return builder.build(); } } public {{service:name}} create{{service:name}}() { return new {{service:name}}Impl(interactionCounter.incrementAndGet()); } {{^last?}} {{/last?}} {{/service:interactions}} }