/*
 * Copyright 2014 NAVER Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.navercorp.pinpoint.common.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.springframework.data.hadoop.hbase.HbaseSystemException;

import java.io.IOException;

/**
 * @author emeroad
 */
public class HBaseAdminTemplate {

    private final HBaseAdmin hBaseAdmin;

    public HBaseAdminTemplate(Configuration configuration) {
        try {
            this.hBaseAdmin = new HBaseAdmin(configuration);
        } catch (MasterNotRunningException e) {
            throw new HbaseSystemException(e);
        } catch (ZooKeeperConnectionException e) {
            throw new HbaseSystemException(e);
        } catch (IOException e) {
            throw new HbaseSystemException(e);
        }
    }

    public boolean createTableIfNotExist(HTableDescriptor htd) {
        try {
            if (!hBaseAdmin.tableExists(htd.getName())) {
                this.hBaseAdmin.createTable(htd);
                return true;
            }
            return false;
        } catch (IOException e) {
            throw new HbaseSystemException(e);
        }
    }

    public boolean tableExists(String tableName) {
        try {
            return hBaseAdmin.tableExists(tableName);
        } catch (IOException e) {
            throw new HbaseSystemException(e);
        }
    }

    public boolean dropTableIfExist(String tableName) {
        try {
            if (hBaseAdmin.tableExists(tableName)) {
                this.hBaseAdmin.disableTable(tableName);
                this.hBaseAdmin.deleteTable(tableName);
                return true;
            }
            return false;
        } catch (IOException e) {
            throw new HbaseSystemException(e);
        }
    }

    public void dropTable(String tableName) {
        try {
            this.hBaseAdmin.disableTable(tableName);
            this.hBaseAdmin.deleteTable(tableName);
        } catch (IOException e) {
            throw new HbaseSystemException(e);
        }
    }

    public void close() {
        try {
            this.hBaseAdmin.close();
        } catch (IOException e) {
            throw new HbaseSystemException(e);
        }
    }
}

@@ -635,16 +635,45 @@
 hbase;%0A%0A
+import java.io.IOException;%0A%0A
 import o
@@ -793,32 +793,61 @@
 ase.
-MasterNotRunningExceptio
+TableName;%0Aimport org.apache.hadoop.hbase.client.Admi
 n;%0Ai
@@ -880,32 +880,21 @@
 ase.
-ZooKeeperConnectionExcep
+client.Connec
 tion
@@ -933,26 +933,33 @@
 .client.
-HBaseAdmin
+ConnectionFactory
 ;%0Aimport
@@ -1024,37 +1024,8 @@
 n;%0A%0A
-import java.io.IOException;%0A%0A
 /**%0A
@@ -1104,25 +1104,15 @@
 nal 
-HBase
 Admin 
-hBaseA
+a
 dmin
@@ -1205,247 +1205,123 @@
     
-this.hBaseAdmin = new HBaseAdmin(configuration);%0A        %7D catch (MasterNotRunningException e) %7B%0A            throw new HbaseSystemException(e);%0A        %7D catch (ZooKeeperConnectionException e) %7B%0A            throw new HbaseSystemExceptio
+Connection connection = ConnectionFactory.createConnection(configuration);%0A            admin = connection.getAdmi
 n(
-e
 );%0A 
@@ -1328,34 +1328,32 @@
        %7D catch (
-IO
 Exception e) %7B%0A 
@@ -1511,22 +1511,17 @@
    if (!
-hBaseA
+a
 dmin.tab
@@ -1536,16 +1536,21 @@
 (htd.get
+Table
 Name()))
@@ -1569,30 +1569,25 @@
        this.
-hBaseA
+a
 dmin.createT
@@ -1848,22 +1848,17 @@
  return 
-hBaseA
+a
 dmin.tab
@@ -1858,32 +1858,50 @@
 min.tableExists(
+TableName.valueOf(
 tableName);%0A    
@@ -1886,32 +1886,33 @@
 lueOf(tableName)
+)
 ;%0A        %7D catc
@@ -2043,32 +2043,85 @@
 ng tableName) %7B%0A
+        TableName tn = TableName.valueOf(tableName);%0A
         try %7B%0A  
@@ -2134,22 +2134,17 @@
     if (
-hBaseA
+a
 dmin.tab
@@ -2153,24 +2153,17 @@
 Exists(t
-ableName
+n
 )) %7B%0A   
@@ -2172,38 +2172,33 @@
            this.
-hBaseA
+a
 dmin.disableTabl
@@ -2196,32 +2196,25 @@
 sableTable(t
-ableName
+n
 );%0A         
@@ -2217,38 +2217,33 @@
            this.
-hBaseA
+a
 dmin.deleteTable
@@ -2240,32 +2240,25 @@
 eleteTable(t
-ableName
+n
 );%0A         
@@ -2453,32 +2453,85 @@
 ng tableName) %7B%0A
+        TableName tn = TableName.valueOf(tableName);%0A
         try %7B%0A  
@@ -2541,30 +2541,25 @@
        this.
-hBaseA
+a
 dmin.disable
@@ -2561,32 +2561,25 @@
 sableTable(t
-ableName
+n
 );%0A         
@@ -2582,30 +2582,25 @@
        this.
-hBaseA
+a
 dmin.deleteT
@@ -2605,24 +2605,17 @@
 eTable(t
-ableName
+n
 );%0A     
@@ -2768,14 +2768,9 @@
 his.
-hBaseA
+a
 dmin

improve using hbase deprecated api