package com.collective.celos;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * Check in HDFS for a data dependency.
 */
public class HDFSCheckTrigger implements Trigger {

    private static final Map<String, FileSystem> cachedFSs = new HashMap<>();
    private final ScheduledTimeFormatter formatter = new ScheduledTimeFormatter();

    private static final Logger LOGGER = Logger.getLogger(HDFSCheckTrigger.class);
    private final FileSystem fs;
    private final String rawPathString;
    private final String fsString;

    public HDFSCheckTrigger(String rawPathString, String fsString) throws Exception {
        this.rawPathString = Util.requireNonNull(rawPathString);
        this.fsString = Util.requireNonNull(fsString);

        if (cachedFSs.containsKey(fsString)) {
            this.fs = cachedFSs.get(fsString);
        } else {
            Configuration conf = new Configuration();
            //required due http://stackoverflow.com/questions/17265002/hadoop-no-filesystem-for-scheme-file
            conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
            conf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());

            addFileToConfiguration(conf, "hdfs-site.xml", "/etc/hadoop/conf");
            addFileToConfiguration(conf, "core-site.xml", "/etc/hadoop/conf");

            this.fs = FileSystem.get(new URI(fsString), conf);
            cachedFSs.put(fsString, this.fs);
        }
    }

    private void addFileToConfiguration(Configuration conf, String fileName, String defaultConfDir) throws FileNotFoundException {
        InputStream fileStream = getClass().getClassLoader().getResourceAsStream(fileName);
        if (fileStream == null) {
            fileStream = new FileInputStream(new File(defaultConfDir, fileName));
        }
        if (fileStream != null) {
            conf.addResource(fileStream);
        }
    }

    @Override
    public boolean isDataAvailable(Scheduler scheduler, ScheduledTime now, ScheduledTime t) throws Exception {
        Path path = new Path(formatter.replaceTimeTokens(getRawPathString(), t));
        LOGGER.info("Checking HDFS path: " + path);
        return getFs().exists(path);
    }


    public String getRawPathString() {
        return rawPathString;
    }

    public String getFsString() {
        return fsString;
    }

    public FileSystem getFs() {
        return fs;
    }
}

@@ -143,16 +143,72 @@
 s.Path;%0A
+import org.apache.hadoop.security.UserGroupInformation;%0A
 import o
@@ -1604,36 +1604,16 @@
 ite.xml%22
-, %22/etc/hadoop/conf%22
 );%0A     
@@ -1667,28 +1667,8 @@
 xml%22
-, %22/etc/hadoop/conf%22
 );%0A%0A
@@ -1872,31 +1872,8 @@
 Name
-, String defaultConfDir
 ) th
@@ -1997,134 +1997,8 @@
 e);%0A
-        if (fileStream == null) %7B%0A            fileStream = new FileInputStream(new File(defaultConfDir, fileName));%0A        %7D%0A
     

use only -cp way, so we dont need to call UserGroupInformation.setConfiguration(conf);