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;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.Server;
025import org.apache.hadoop.hbase.ipc.RpcServer.BlockingServiceAndInterface;
026import org.apache.hadoop.hbase.util.ReflectionUtils;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import org.apache.hbase.thirdparty.com.google.protobuf.Descriptors.ServiceDescriptor;
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, final InetSocketAddress bindAddress,
054    Configuration conf, RpcScheduler scheduler, boolean reservoirEnabled) throws IOException {
055    String rpcServerClass =
056      conf.get(CUSTOM_RPC_SERVER_IMPL_CONF_KEY, NettyRpcServer.class.getName());
057    StringBuilder servicesList = new StringBuilder();
058    for (BlockingServiceAndInterface s : services) {
059      ServiceDescriptor sd = s.getBlockingService().getDescriptorForType();
060      if (sd == null) continue; // Can be null for certain tests like TestTokenAuthentication
061      if (servicesList.length() > 0) servicesList.append(", ");
062      servicesList.append(sd.getFullName());
063    }
064    LOG.info("Creating " + rpcServerClass + " hosting " + servicesList);
065    return ReflectionUtils.instantiateWithCustomCtor(rpcServerClass,
066      new Class[] { Server.class, String.class, List.class, InetSocketAddress.class,
067        Configuration.class, RpcScheduler.class, boolean.class },
068      new Object[] { server, name, services, bindAddress, conf, scheduler, reservoirEnabled });
069  }
070}