// Path: hadoop-mini-clusters-common/src/main/java/com/github/sakserv/minicluster/util/FileUtils.java
// public final class FileUtils {
// 
//     // Logger
//     private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(FileUtils.class);
// 
//     public static void deleteFolder(String directory, boolean quietly) {
//         try {
//             Path directoryPath = Paths.get(directory).toAbsolutePath();
//             if (!quietly) {
//                 LOG.info("FILEUTILS: Deleting contents of directory: {}",
//                         directoryPath.toAbsolutePath().toString());
//             }
//             Files.walkFileTree(directoryPath, new SimpleFileVisitor<Path>() {
//                 @Override
//                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
//                         throws IOException {
//                     Files.delete(file);
//                     if (!quietly) {
//                         LOG.info("Removing file: {}", file.toAbsolutePath().toString());
//                     }
//                     return FileVisitResult.CONTINUE;
//                 }
// 
//                 @Override
//                 public FileVisitResult postVisitDirectory(Path dir, IOException exc)
//                         throws IOException {
//                     Files.delete(dir);
//                     if (!quietly) {
//                         LOG.info("Removing directory: {}", dir.toAbsolutePath().toString());
//                     }
//                     return FileVisitResult.CONTINUE;
//                 }
//             });
//         } catch (IOException e) {
//             LOG.error("FILEUTILS: Unable to remove {}", directory);
//         }
//     }
// 
//     public static void deleteFolder(String directory) {
//         deleteFolder(directory, false);
//     }
// 
//     @Override
//     public String toString() {
//         return "FileUtils";
//     }
// }
// 
// Path: hadoop-mini-clusters-common/src/main/java/com/github/sakserv/minicluster/util/WindowsLibsUtils.java
// public class WindowsLibsUtils {
// 
//     // Logger
//     private static final Logger LOG = LoggerFactory.getLogger(WindowsLibsUtils.class);
// 
//     public static void setHadoopHome() {
// 
//         // Set hadoop.home.dir to point to the windows lib dir
//         if (System.getProperty("os.name").startsWith("Windows")) {
// 
//             String windowsLibDir = getHadoopHome();
// 
//             LOG.info("WINDOWS: Setting hadoop.home.dir: {}", windowsLibDir);
//             System.setProperty("hadoop.home.dir", windowsLibDir);
//             System.load(new File(windowsLibDir + Path.SEPARATOR + "lib" + Path.SEPARATOR + "hadoop.dll").getAbsolutePath());
//             System.load(new File(windowsLibDir + Path.SEPARATOR + "lib" + Path.SEPARATOR + "hdfs.dll").getAbsolutePath());
// 
//         }
//     }
// 
//     public static String getHadoopHome() {
// 
//         if(System.getProperty("HADOOP_HOME") != null) {
//             LOG.info("HADOOP_HOME: " + System.getProperty("HADOOP_HOME"));
//             return System.getProperty("HADOOP_HOME");
//         } else if (System.getenv("HADOOP_HOME") != null) { //takes the hadoop home from system environment variable
//             LOG.info("HADOOP_HOME: " + System.getenv("HADOOP_HOME"));
//             return System.getenv("HADOOP_HOME");
//         } else {
// 
//             File windowsLibDir = new File("." + Path.SEPARATOR + "windows_libs" +
//                     Path.SEPARATOR + System.getProperty("hdp.release.version"));
// 
//             if (!windowsLibDir.exists()) {
//                 windowsLibDir = new File(".." + Path.SEPARATOR + windowsLibDir);
//                 if (!windowsLibDir.exists()) {
//                     LOG.error("WINDOWS: ERROR: Could not find windows native libs");
//                 }
//             }
//             return windowsLibDir.getAbsolutePath();
//         }
// 
//     }
// 
// }


import java.io.File;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.HiveMetaStore;
import org.apache.hadoop.hive.metastore.txn.TxnDbUtil;
import org.apache.hadoop.hive.thrift.HadoopThriftAuthBridge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.sakserv.minicluster.MiniCluster;
import com.github.sakserv.minicluster.util.FileUtils;
import com.github.sakserv.minicluster.util.WindowsLibsUtils;
    }

    @Override
    public void stop() throws Exception {
        stop(true);
    }

    @Override
    public void stop(boolean cleanUp) throws Exception {
        LOG.info("HIVEMETASTORE: Stopping Hive Metastore on port: {}", hiveMetastorePort);
        t.interrupt();
        if (cleanUp) {
            cleanUp();
        }

    }

    @Override
    public void configure() throws Exception {
        hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS,
                "thrift://" + hiveMetastoreHostname + ":" + hiveMetastorePort);
        hiveConf.setVar(HiveConf.ConfVars.SCRATCHDIR, hiveScratchDir);
        hiveConf.setVar(HiveConf.ConfVars.METASTORECONNECTURLKEY,
                "jdbc:derby:;databaseName=" + hiveMetastoreDerbyDbDir + ";create=true");
        hiveConf.setVar(HiveConf.ConfVars.METASTOREWAREHOUSE, new File(hiveWarehouseDir).getAbsolutePath());
        hiveConf.setBoolVar(HiveConf.ConfVars.HIVE_IN_TEST, true);
        hiveConf.set("datanucleus.schema.autoCreateTables", "true");
        hiveConf.set("hive.metastore.schema.verification", "false");

        // Handle Windows
// Path: hadoop-mini-clusters-common/src/main/java/com/github/sakserv/minicluster/util/FileUtils.java
// public final class FileUtils {
// 
//     // Logger
//     private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(FileUtils.class);
// 
//     public static void deleteFolder(String directory, boolean quietly) {
//         try {
//             Path directoryPath = Paths.get(directory).toAbsolutePath();
//             if (!quietly) {
//                 LOG.info("FILEUTILS: Deleting contents of directory: {}",
//                         directoryPath.toAbsolutePath().toString());
//             }
//             Files.walkFileTree(directoryPath, new SimpleFileVisitor<Path>() {
//                 @Override
//                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
//                         throws IOException {
//                     Files.delete(file);
//                     if (!quietly) {
//                         LOG.info("Removing file: {}", file.toAbsolutePath().toString());
//                     }
//                     return FileVisitResult.CONTINUE;
//                 }
// 
//                 @Override
//                 public FileVisitResult postVisitDirectory(Path dir, IOException exc)
//                         throws IOException {
//                     Files.delete(dir);
//                     if (!quietly) {
//                         LOG.info("Removing directory: {}", dir.toAbsolutePath().toString());
//                     }
//                     return FileVisitResult.CONTINUE;
//                 }
//             });
//         } catch (IOException e) {
//             LOG.error("FILEUTILS: Unable to remove {}", directory);
//         }
//     }
// 
//     public static void deleteFolder(String directory) {
//         deleteFolder(directory, false);
//     }
// 
//     @Override
//     public String toString() {
//         return "FileUtils";
//     }
// }
// 
// Path: hadoop-mini-clusters-common/src/main/java/com/github/sakserv/minicluster/util/WindowsLibsUtils.java
// public class WindowsLibsUtils {
// 
//     // Logger
//     private static final Logger LOG = LoggerFactory.getLogger(WindowsLibsUtils.class);
// 
//     public static void setHadoopHome() {
// 
//         // Set hadoop.home.dir to point to the windows lib dir
//         if (System.getProperty("os.name").startsWith("Windows")) {
// 
//             String windowsLibDir = getHadoopHome();
// 
//             LOG.info("WINDOWS: Setting hadoop.home.dir: {}", windowsLibDir);
//             System.setProperty("hadoop.home.dir", windowsLibDir);
//             System.load(new File(windowsLibDir + Path.SEPARATOR + "lib" + Path.SEPARATOR + "hadoop.dll").getAbsolutePath());
//             System.load(new File(windowsLibDir + Path.SEPARATOR + "lib" + Path.SEPARATOR + "hdfs.dll").getAbsolutePath());
// 
//         }
//     }
// 
//     public static String getHadoopHome() {
// 
//         if(System.getProperty("HADOOP_HOME") != null) {
//             LOG.info("HADOOP_HOME: " + System.getProperty("HADOOP_HOME"));
//             return System.getProperty("HADOOP_HOME");
//         } else if (System.getenv("HADOOP_HOME") != null) { //takes the hadoop home from system environment variable
//             LOG.info("HADOOP_HOME: " + System.getenv("HADOOP_HOME"));
//             return System.getenv("HADOOP_HOME");
//         } else {
// 
//             File windowsLibDir = new File("." + Path.SEPARATOR + "windows_libs" +
//                     Path.SEPARATOR + System.getProperty("hdp.release.version"));
// 
//             if (!windowsLibDir.exists()) {
//                 windowsLibDir = new File(".." + Path.SEPARATOR + windowsLibDir);
//                 if (!windowsLibDir.exists()) {
//                     LOG.error("WINDOWS: ERROR: Could not find windows native libs");
//                 }
//             }
//             return windowsLibDir.getAbsolutePath();
//         }
// 
//     }
// 
// }


// Path: hadoop-mini-clusters-hivemetastore/src/main/java/com/github/sakserv/minicluster/impl/HiveLocalMetaStore.java
import java.io.File;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.HiveMetaStore;
import org.apache.hadoop.hive.metastore.txn.TxnDbUtil;
import org.apache.hadoop.hive.thrift.HadoopThriftAuthBridge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.sakserv.minicluster.MiniCluster;
import com.github.sakserv.minicluster.util.FileUtils;
import com.github.sakserv.minicluster.util.WindowsLibsUtils;

    }

    @Override
    public void stop() throws Exception {
        stop(true);
    }

    @Override
    public void stop(boolean cleanUp) throws Exception {
        LOG.info("HIVEMETASTORE: Stopping Hive Metastore on port: {}", hiveMetastorePort);
        t.interrupt();
        if (cleanUp) {
            cleanUp();
        }

    }

    @Override
    public void configure() throws Exception {
        hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS,
                "thrift://" + hiveMetastoreHostname + ":" + hiveMetastorePort);
        hiveConf.setVar(HiveConf.ConfVars.SCRATCHDIR, hiveScratchDir);
        hiveConf.setVar(HiveConf.ConfVars.METASTORECONNECTURLKEY,
                "jdbc:derby:;databaseName=" + hiveMetastoreDerbyDbDir + ";create=true");
        hiveConf.setVar(HiveConf.ConfVars.METASTOREWAREHOUSE, new File(hiveWarehouseDir).getAbsolutePath());
        hiveConf.setBoolVar(HiveConf.ConfVars.HIVE_IN_TEST, true);
        hiveConf.set("datanucleus.schema.autoCreateTables", "true");
        hiveConf.set("hive.metastore.schema.verification", "false");

        // Handle Windows
        WindowsLibsUtils.setHadoopHome();