package com.nflabs.zeppelin.zengine;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Map;

import org.apache.log4j.Logger;

import com.google.common.collect.ImmutableMap;
import com.nflabs.zeppelin.conf.ZeppelinConfiguration;
import com.nflabs.zeppelin.conf.ZeppelinConfiguration.ConfVars;
import com.nflabs.zeppelin.driver.ZeppelinDriver;
import com.nflabs.zeppelin.driver.ZeppelinDriverFactory;
import com.nflabs.zeppelin.util.Util;


/**
 * This class is Zeppelin Execution Environment, or context;
 * it hosts all necessary dependencies to run Zengine:
 * 
 *   - zeppelin configuration
 *   - driver factory
 *   
 *  If anybody needs those to run (i.e ZQL, Q, L etc) - they just ask Zengine for it.
 * 
 * @author Alex
 */
public class Zengine {
	Logger logger = Logger.getLogger(Zengine.class);
    private ZeppelinConfiguration conf;
	private ZeppelinDriverFactory driverFactory;
    
    /**
     * Create Zeppelin environment
     * zeppelin-site.xml will be loaded from classpath
     * 
     * @throws ZException
     */
    public Zengine() throws ZException{
    	this(ZeppelinConfiguration.create(), null);
    }
        
    /**
     * Configures Zengine with dependencies: 
     * 
     * @param conf - configuration from FileSysyem
     * @param driver - driver implementation
     *                 could be NULL, then default from configuration is used
     * @throws ZException
     */
    public Zengine(ZeppelinConfiguration conf, ZeppelinDriverFactory driverFactory) throws ZException{
        this.conf = conf;      
        if (driverFactory == null) {
        	String driverDir = conf.getString(ConfVars.ZEPPELIN_DRIVER_DIR);        	
        	String [] uriList = Util.split(conf.getString(ConfVars.ZEPPELIN_DRIVERS), "\"',", '\\', new String[]{"\"", "'"}, new String[]{"\"", "'"}, new String[]{","}, false);
        	URI [] uris = new URI[uriList.length];
        	for (int i=0; i<uriList.length; i++) {
        		try {
					uris[i] = new URI(uriList[i]);
				} catch (URISyntaxException e) {
					throw new ZException(e);
				}
        	}
        	this.driverFactory = new ZeppelinDriverFactory(driverDir, uris);
        } else {
        	this.driverFactory = driverFactory;
        }
    }

    /**
     * Get zeppelin configuration
     * @return
     */
    public ZeppelinConfiguration getConf(){
        return conf;
    }
    
    /**
     * Get zeppelin driver factory
     * @return
     */
    public ZeppelinDriverFactory getDriverFactory(){
    	return driverFactory;
    }

    public boolean useFifoJobScheduler() {
        return conf.getString(ConfVars.ZEPPELIN_JOB_SCHEDULER).equals("FIFO");
    }

}

@@ -1632,497 +1632,8 @@
 ) %7B%0A
-        %09String driverDir = conf.getString(ConfVars.ZEPPELIN_DRIVER_DIR);        %09%0A        %09String %5B%5D uriList = Util.split(conf.getString(ConfVars.ZEPPELIN_DRIVERS), %22%5C%22',%22, '%5C%5C', new String%5B%5D%7B%22%5C%22%22, %22'%22%7D, new String%5B%5D%7B%22%5C%22%22, %22'%22%7D, new String%5B%5D%7B%22,%22%7D, false);%0A        %09URI %5B%5D uris = new URI%5BuriList.length%5D;%0A        %09for (int i=0; i%3CuriList.length; i++) %7B%0A        %09%09try %7B%0A%09%09%09%09%09uris%5Bi%5D = new URI(uriList%5Bi%5D);%0A%09%09%09%09%7D catch (URISyntaxException e) %7B%0A%09%09%09%09%09throw new ZException(e);%0A%09%09%09%09%7D%0A        %09%7D%0A
     
@@ -1688,23 +1688,12 @@
 ory(
-driverDir, uris
+conf
 );%0A 

Move configuration read from zengine to driverfactory