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 */
018package org.apache.hadoop.hbase.ipc;
019
020import java.io.IOException;
021import java.net.InetSocketAddress;
022import java.util.List;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.Server;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import org.apache.hadoop.hbase.ipc.RpcServer.BlockingServiceAndInterface;
030import org.apache.hbase.thirdparty.com.google.protobuf.Descriptors.ServiceDescriptor;
031import org.apache.hadoop.hbase.util.ReflectionUtils;
032
033@InterfaceAudience.Private
034public class RpcServerFactory {
035
036  public static final Logger LOG = LoggerFactory.getLogger(RpcServerFactory.class);
037
038  public static final String CUSTOM_RPC_SERVER_IMPL_CONF_KEY = "hbase.rpc.server.impl";
039
040  /**
041   * Private Constructor
042   */
043  private RpcServerFactory() {
044  }
045
046  public static RpcServer createRpcServer(final Server server, final String name,
047      final List<BlockingServiceAndInterface> services, final InetSocketAddress bindAddress,
048      Configuration conf, RpcScheduler scheduler) throws IOException {
049    return createRpcServer(server, name, services, bindAddress, conf, scheduler, true);
050  }
051
052  public static RpcServer createRpcServer(final Server server, final String name,
053      final List<BlockingServiceAndInterface> services,
054      final InetSocketAddress bindAddress, Configuration conf,
055      RpcScheduler scheduler, boolean reservoirEnabled) throws IOException {
056    String rpcServerClass = conf.get(CUSTOM_RPC_SERVER_IMPL_CONF_KEY,
057        NettyRpcServer.class.getName());
058    StringBuilder servicesList = new StringBuilder();
059    for (BlockingServiceAndInterface s: services) {
060      ServiceDescriptor sd = s.getBlockingService().getDescriptorForType();
061      if (sd == null) continue; // Can be null for certain tests like TestTokenAuthentication
062      if (servicesList.length() > 0) servicesList.append(", ");
063      servicesList.append(sd.getFullName());
064    }
065    LOG.info("Creating " + rpcServerClass + " hosting " + servicesList);
066    return ReflectionUtils.instantiateWithCustomCtor(rpcServerClass,
067        new Class[] { Server.class, String.class, List.class,
068          InetSocketAddress.class, Configuration.class, RpcScheduler.class, boolean.class },
069        new Object[] { server, name, services, bindAddress, conf, scheduler, reservoirEnabled });
070  }
071}