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.IOException;
022    import java.util.Collection;
023    import java.util.List;
024    
025    import org.apache.hadoop.ipc.GenericRefreshProtocol;
026    import org.apache.hadoop.ipc.RefreshResponse;
027    import org.apache.hadoop.ipc.proto.GenericRefreshProtocolProtos.GenericRefreshRequestProto;
028    import org.apache.hadoop.ipc.proto.GenericRefreshProtocolProtos.GenericRefreshResponseProto;
029    import org.apache.hadoop.ipc.proto.GenericRefreshProtocolProtos.GenericRefreshResponseCollectionProto;
030    
031    import com.google.protobuf.RpcController;
032    import com.google.protobuf.ServiceException;
033    
034    public class GenericRefreshProtocolServerSideTranslatorPB implements
035        GenericRefreshProtocolPB {
036    
037      private final GenericRefreshProtocol impl;
038    
039      public GenericRefreshProtocolServerSideTranslatorPB(
040          GenericRefreshProtocol impl) {
041        this.impl = impl;
042      }
043    
044      @Override
045      public GenericRefreshResponseCollectionProto refresh(
046          RpcController controller, GenericRefreshRequestProto request)
047          throws ServiceException {
048        try {
049          List<String> argList = request.getArgsList();
050          String[] args = argList.toArray(new String[argList.size()]);
051    
052          if (!request.hasIdentifier()) {
053            throw new ServiceException("Request must contain identifier");
054          }
055    
056          Collection<RefreshResponse> results = impl.refresh(request.getIdentifier(), args);
057    
058          return pack(results);
059        } catch (IOException e) {
060          throw new ServiceException(e);
061        }
062      }
063    
064      // Convert a collection of RefreshResponse objects to a
065      // RefreshResponseCollection proto
066      private GenericRefreshResponseCollectionProto pack(
067        Collection<RefreshResponse> responses) {
068        GenericRefreshResponseCollectionProto.Builder b =
069          GenericRefreshResponseCollectionProto.newBuilder();
070    
071        for (RefreshResponse response : responses) {
072          GenericRefreshResponseProto.Builder respBuilder =
073            GenericRefreshResponseProto.newBuilder();
074          respBuilder.setExitStatus(response.getReturnCode());
075          respBuilder.setUserMessage(response.getMessage());
076          respBuilder.setSenderName(response.getSenderName());
077    
078          // Add to collection
079          b.addResponses(respBuilder);
080        }
081    
082        return b.build();
083      }
084    }