001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.apache.hadoop.ipc.protocolPB;
020
021 import java.io.Closeable;
022 import java.io.IOException;
023 import java.util.ArrayList;
024 import java.util.Arrays;
025 import java.util.Collection;
026 import java.util.List;
027
028 import org.apache.hadoop.ipc.ProtobufHelper;
029 import org.apache.hadoop.ipc.ProtocolMetaInterface;
030 import org.apache.hadoop.ipc.RPC;
031 import org.apache.hadoop.ipc.RefreshResponse;
032 import org.apache.hadoop.ipc.RpcClientUtil;
033 import org.apache.hadoop.ipc.GenericRefreshProtocol;
034 import org.apache.hadoop.ipc.proto.GenericRefreshProtocolProtos.GenericRefreshRequestProto;
035 import org.apache.hadoop.ipc.proto.GenericRefreshProtocolProtos.GenericRefreshResponseProto;
036 import org.apache.hadoop.ipc.proto.GenericRefreshProtocolProtos.GenericRefreshResponseCollectionProto;
037
038 import com.google.protobuf.RpcController;
039 import com.google.protobuf.ServiceException;
040
041 public class GenericRefreshProtocolClientSideTranslatorPB implements
042 ProtocolMetaInterface, GenericRefreshProtocol, Closeable {
043
044 /** RpcController is not used and hence is set to null. */
045 private final static RpcController NULL_CONTROLLER = null;
046 private final GenericRefreshProtocolPB rpcProxy;
047
048 public GenericRefreshProtocolClientSideTranslatorPB(
049 GenericRefreshProtocolPB rpcProxy) {
050 this.rpcProxy = rpcProxy;
051 }
052
053 @Override
054 public void close() throws IOException {
055 RPC.stopProxy(rpcProxy);
056 }
057
058 @Override
059 public Collection<RefreshResponse> refresh(String identifier, String[] args) throws IOException {
060 List<String> argList = Arrays.asList(args);
061
062 try {
063 GenericRefreshRequestProto request = GenericRefreshRequestProto.newBuilder()
064 .setIdentifier(identifier)
065 .addAllArgs(argList)
066 .build();
067
068 GenericRefreshResponseCollectionProto resp = rpcProxy.refresh(NULL_CONTROLLER, request);
069 return unpack(resp);
070 } catch (ServiceException se) {
071 throw ProtobufHelper.getRemoteException(se);
072 }
073 }
074
075 private Collection<RefreshResponse> unpack(GenericRefreshResponseCollectionProto collection) {
076 List<GenericRefreshResponseProto> responseProtos = collection.getResponsesList();
077 List<RefreshResponse> responses = new ArrayList<RefreshResponse>();
078
079 for (GenericRefreshResponseProto rp : responseProtos) {
080 RefreshResponse response = unpack(rp);
081 responses.add(response);
082 }
083
084 return responses;
085 }
086
087 private RefreshResponse unpack(GenericRefreshResponseProto proto) {
088 // The default values
089 String message = null;
090 String sender = null;
091 int returnCode = -1;
092
093 // ... that can be overridden by data from the protobuf
094 if (proto.hasUserMessage()) {
095 message = proto.getUserMessage();
096 }
097 if (proto.hasExitStatus()) {
098 returnCode = proto.getExitStatus();
099 }
100 if (proto.hasSenderName()) {
101 sender = proto.getSenderName();
102 }
103
104 // ... and put into a RefreshResponse
105 RefreshResponse response = new RefreshResponse(returnCode, message);
106 response.setSenderName(sender);
107
108 return response;
109 }
110
111 @Override
112 public boolean isMethodSupported(String methodName) throws IOException {
113 return RpcClientUtil.isMethodSupported(rpcProxy,
114 GenericRefreshProtocolPB.class,
115 RPC.RpcKind.RPC_PROTOCOL_BUFFER,
116 RPC.getProtocolVersion(GenericRefreshProtocolPB.class),
117 methodName);
118 }
119 }