View Javadoc

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  package org.apache.hadoop.hbase.ipc;
19  
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.util.ReflectionUtils;
23  
24  import java.net.SocketAddress;
25  
26  /**
27   * Factory to create a {@link org.apache.hadoop.hbase.ipc.RpcClient}
28   */
29  @InterfaceAudience.Private
30  public final class RpcClientFactory {
31  
32    public static final String CUSTOM_RPC_CLIENT_IMPL_CONF_KEY = "hbase.rpc.client.impl";
33  
34    /**
35     * Private Constructor
36     */
37    private RpcClientFactory() {
38    }
39  
40    /**
41     * Creates a new RpcClient by the class defined in the configuration or falls back to
42     * RpcClientImpl
43     * @param conf configuration
44     * @param clusterId the cluster id
45     * @return newly created RpcClient
46     */
47    public static RpcClient createClient(Configuration conf, String clusterId) {
48      return createClient(conf, clusterId, null);
49    }
50  
51    /**
52     * Creates a new RpcClient by the class defined in the configuration or falls back to
53     * RpcClientImpl
54     * @param conf configuration
55     * @param clusterId the cluster id
56     * @param localAddr client socket bind address.
57     * @return newly created RpcClient
58     */
59    public static RpcClient createClient(Configuration conf, String clusterId,
60        SocketAddress localAddr) {
61      String rpcClientClass =
62          conf.get(CUSTOM_RPC_CLIENT_IMPL_CONF_KEY,
63            RpcClientImpl.class.getName());
64      return ReflectionUtils.instantiateWithCustomCtor(
65          rpcClientClass,
66          new Class[] { Configuration.class, String.class, SocketAddress.class },
67          new Object[] { conf, clusterId, localAddr }
68      );
69    }
70  }