001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.thrift; 020 021import java.io.IOException; 022import java.nio.ByteBuffer; 023 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.HBaseInterfaceAudience; 026import org.apache.hadoop.hbase.client.Admin; 027import org.apache.hadoop.hbase.client.Table; 028import org.apache.hadoop.hbase.security.UserProvider; 029import org.apache.hadoop.hbase.util.Bytes; 030import org.apache.hadoop.hbase.util.ConnectionCache; 031import org.apache.yetus.audience.InterfaceAudience; 032 033/** 034 * abstract class for HBase handler 035 * providing a Connection cache and get table/admin method 036 */ 037@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS) 038public abstract class HBaseServiceHandler { 039 public static final String CLEANUP_INTERVAL = "hbase.thrift.connection.cleanup-interval"; 040 public static final String MAX_IDLETIME = "hbase.thrift.connection.max-idletime"; 041 042 protected Configuration conf; 043 044 protected final ConnectionCache connectionCache; 045 046 public HBaseServiceHandler(final Configuration c, 047 final UserProvider userProvider) throws IOException { 048 this.conf = c; 049 int cleanInterval = conf.getInt(CLEANUP_INTERVAL, 10 * 1000); 050 int maxIdleTime = conf.getInt(MAX_IDLETIME, 10 * 60 * 1000); 051 connectionCache = new ConnectionCache( 052 conf, userProvider, cleanInterval, maxIdleTime); 053 } 054 055 protected ThriftMetrics metrics = null; 056 057 public void initMetrics(ThriftMetrics metrics) { 058 this.metrics = metrics; 059 } 060 061 public void setEffectiveUser(String effectiveUser) { 062 connectionCache.setEffectiveUser(effectiveUser); 063 } 064 065 /** 066 * Obtain HBaseAdmin. Creates the instance if it is not already created. 067 */ 068 protected Admin getAdmin() throws IOException { 069 return connectionCache.getAdmin(); 070 } 071 072 /** 073 * Creates and returns a Table instance from a given table name. 074 * 075 * @param tableName 076 * name of table 077 * @return Table object 078 * @throws IOException if getting the table fails 079 */ 080 protected Table getTable(final byte[] tableName) throws IOException { 081 String table = Bytes.toString(tableName); 082 return connectionCache.getTable(table); 083 } 084 085 protected Table getTable(final ByteBuffer tableName) throws IOException { 086 return getTable(Bytes.getBytes(tableName)); 087 } 088 089 090}