1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 package org.apache.hadoop.hbase.ipc;
21
22 import com.google.protobuf.Descriptors;
23 import com.google.protobuf.Message;
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
26 import org.apache.hadoop.hbase.util.ByteStringer;
27
28 /**
29 * Utilities for handling coprocessor service calls.
30 */
31 @InterfaceAudience.Private
32 public final class CoprocessorRpcUtils {
33 /**
34 * We assume that all HBase protobuf services share a common package name
35 * (defined in the .proto files).
36 */
37 private static String hbaseServicePackage;
38 static {
39 Descriptors.ServiceDescriptor clientService = ClientProtos.ClientService.getDescriptor();
40 hbaseServicePackage = clientService.getFullName()
41 .substring(0, clientService.getFullName().lastIndexOf(clientService.getName()));
42 }
43
44 private CoprocessorRpcUtils() {
45 // private for utility class
46 }
47
48 /**
49 * Returns the name to use for coprocessor service calls. For core HBase services
50 * (in the hbase.pb protobuf package), this returns the unqualified name in order to provide
51 * backward compatibility across the package name change. For all other services,
52 * the fully-qualified service name is used.
53 */
54 public static String getServiceName(Descriptors.ServiceDescriptor service) {
55 if (service.getFullName().startsWith(hbaseServicePackage)) {
56 return service.getName();
57 }
58 return service.getFullName();
59 }
60
61 /**
62 * Returns a service call instance for the given coprocessor request.
63 */
64 public static ClientProtos.CoprocessorServiceCall buildServiceCall(byte[] row,
65 Descriptors.MethodDescriptor method, Message request) {
66 return ClientProtos.CoprocessorServiceCall.newBuilder()
67 .setRow(ByteStringer.wrap(row))
68 .setServiceName(CoprocessorRpcUtils.getServiceName(method.getService()))
69 .setMethodName(method.getName())
70 .setRequest(request.toByteString()).build();
71 }
72 }