/*
 * Copyright (c) 2013 Yahoo! Inc. All Rights Reserved.
 *
 * 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. See accompanying LICENSE file.
 */
package com.yahoo.storm.yarn;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.yaml.snakeyaml.Yaml;

public class TestConfig {
    private static final Log LOG = LogFactory.getLog(TestConfig.class);

    private File yarn_site_xml;
    private File storm_conf_file;
    private String storm_home = "./lib/storm";

    synchronized File createYarnSiteConfig(Configuration yarn_conf) throws IOException {
        yarn_site_xml = new File("./target/conf/yarn-site.xml");
        yarn_site_xml.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(yarn_site_xml);
        OutputStreamWriter writer = new OutputStreamWriter(out);
        yarn_conf.writeXml(writer);
        writer.close();
        out.close();   
        return yarn_site_xml;
    }

    @SuppressWarnings("rawtypes")
    synchronized File createConfigFile(Map storm_conf) throws IOException {
        storm_conf_file = new File("./conf/storm.yaml");
        storm_conf_file.getParentFile().mkdirs();
        Yaml yaml = new Yaml();
        FileOutputStream out = new FileOutputStream(storm_conf_file);
        OutputStreamWriter writer = new OutputStreamWriter(out);
        Util.rmNulls(storm_conf);
        yaml.dump(storm_conf, writer);
        writer.close();
        out.close();   
        return storm_conf_file;
    }

    void cleanup() {
        if (storm_conf_file != null) 
            deleteFolder(storm_conf_file);
        deleteFolder(new File(storm_home));
    }

    synchronized String stormHomePath() throws IOException {
        unzipFile("./lib/storm.zip");
        Runtime.getRuntime().exec( "chmod +x ./lib/storm/bin/storm");
        System.setProperty("storm.home", storm_home);
        return storm_home;
    }

    private void unzipFile(String filePath){
        FileInputStream fis = null;
        ZipInputStream zipIs = null;
        ZipEntry zEntry = null;
        try {
            fis = new FileInputStream(filePath);
            zipIs = new ZipInputStream(new BufferedInputStream(fis));
            while((zEntry = zipIs.getNextEntry()) != null){
                try{
                    byte[] tmp = new byte[4*1024];
                    FileOutputStream fos = null;
                    String opFilePath = "lib/"+zEntry.getName();
                    if (zEntry.isDirectory()) { 
                        LOG.debug("Create a folder "+opFilePath);
                        new File(opFilePath).mkdir();
                    } else {
                        LOG.debug("Extracting file to "+opFilePath);
                        fos = new FileOutputStream(opFilePath);
                        int size = 0;
                        while((size = zipIs.read(tmp)) != -1){
                            fos.write(tmp, 0 , size);
                        }
                        fos.flush();
                        fos.close();
                    }
                } catch(Exception ex){

                }
            }
            zipIs.close();
        } catch (FileNotFoundException e) {
            LOG.warn(e.toString());
        } catch (IOException e) {
            LOG.warn(e.toString());
        }
    }

    private void deleteFolder(File file) {
        if (file.isDirectory()) {
            //directory is empty, then delete it
            if(file.list().length==0){
                file.delete();
            } else {
                //list all the directory contents
                String files[] = file.list();
                for (String temp : files) {
                    //construct the file structure
                    File fileDelete = new File(file, temp);
                    //recursive delete
                    deleteFolder(fileDelete);
                }

                //check the directory again, if empty then delete it
                if(file.list().length==0){
                    file.delete();
                }
            }
        } else {
            //if file, then delete it
            file.delete();
        }
    }
}

@@ -2454,32 +2454,48 @@
 (storm_conf_file
+.getParentFile()
 );%0A        delet

remove temp dir created by test programs