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.thrift;
019
020import java.io.IOException;
021import java.nio.ByteBuffer;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseInterfaceAudience;
024import org.apache.hadoop.hbase.client.Admin;
025import org.apache.hadoop.hbase.client.Table;
026import org.apache.hadoop.hbase.security.UserProvider;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.apache.hadoop.hbase.util.ConnectionCache;
029import org.apache.yetus.audience.InterfaceAudience;
030
031/**
032 * abstract class for HBase handler providing a Connection cache and get table/admin method
033 */
034@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
035public abstract class HBaseServiceHandler {
036  public static final String CLEANUP_INTERVAL = "hbase.thrift.connection.cleanup-interval";
037  public static final String MAX_IDLETIME = "hbase.thrift.connection.max-idletime";
038
039  protected Configuration conf;
040
041  protected final ConnectionCache connectionCache;
042
043  public HBaseServiceHandler(final Configuration c, final UserProvider userProvider)
044    throws IOException {
045    this.conf = c;
046    int cleanInterval = conf.getInt(CLEANUP_INTERVAL, 10 * 1000);
047    int maxIdleTime = conf.getInt(MAX_IDLETIME, 10 * 60 * 1000);
048    connectionCache = new ConnectionCache(conf, userProvider, cleanInterval, maxIdleTime);
049  }
050
051  protected ThriftMetrics metrics = null;
052
053  public void initMetrics(ThriftMetrics metrics) {
054    this.metrics = metrics;
055  }
056
057  public void setEffectiveUser(String effectiveUser) {
058    connectionCache.setEffectiveUser(effectiveUser);
059  }
060
061  /**
062   * Obtain HBaseAdmin. Creates the instance if it is not already created.
063   */
064  protected Admin getAdmin() throws IOException {
065    return connectionCache.getAdmin();
066  }
067
068  /**
069   * Creates and returns a Table instance from a given table name. n * name of table
070   * @return Table object
071   * @throws IOException if getting the table fails
072   */
073  protected Table getTable(final byte[] tableName) throws IOException {
074    String table = Bytes.toString(tableName);
075    return connectionCache.getTable(table);
076  }
077
078  protected Table getTable(final ByteBuffer tableName) throws IOException {
079    return getTable(Bytes.getBytes(tableName));
080  }
081}