/*
 * Copyright 2012 LinkedIn 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 azkaban.jobtype.hiveutils;

import org.apache.hadoop.hive.cli.CliSessionState;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.ql.session.SessionState;

import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.HIVEHISTORYFILELOC;
import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.SCRATCHDIR;

/**
 * Guice-like module for creating a Hive instance.  Easily turned back into
 * a full Guice module when we have need of it.
 */
class HiveQueryExecutorModule {
  private HiveConf hiveConf = null;
  private CliSessionState ss = null;

  HiveConf provideHiveConf() {
    if(this.hiveConf != null) return this.hiveConf;
    else this.hiveConf = new HiveConf();

    troublesomeConfig(HIVEHISTORYFILELOC, hiveConf);
    troublesomeConfig(SCRATCHDIR, hiveConf);
    
    if (System.getenv("HADOOP_TOKEN_FILE_LOCATION") != null) {
    	System.out.println("Setting hadoop tokens ... ");
		hiveConf.set("mapreduce.job.credentials.binary", System.getenv("HADOOP_TOKEN_FILE_LOCATION"));
		System.setProperty("mapreduce.job.credentials.binary", System.getenv("HADOOP_TOKEN_FILE_LOCATION"));
	}

    return hiveConf;
  }

  private void troublesomeConfig(HiveConf.ConfVars value, HiveConf hc) {
    System.out.println("Troublesome config " + value + " = " + HiveConf.getVar(hc, value));
  }

  CliSessionState provideCliSessionState() {
    if(ss != null) return ss;
    ss = new CliSessionState(provideHiveConf());
    SessionState.start(ss);
    return ss;
  }

  //@Override
  protected void configure() { /** Nothing to do **/ }
}


@@ -1207,16 +1207,17 @@
 %7B%0A    if
+ 
 (this.hi
@@ -1223,32 +1223,40 @@
 iveConf != null)
+ %7B%0A     
  return this.hiv
@@ -1269,14 +1269,24 @@
 %0A   
+ %7D
  else 
+%7B%0A      
 this
@@ -1310,18 +1310,42 @@
 iveConf(
-);
+SessionState.class);%0A    %7D
 %0A%0A    tr
@@ -1508,17 +1508,18 @@
 ) %7B%0A    
-%09
+  
 System.o
@@ -1560,18 +1560,22 @@
 ... %22);%0A
-%09%09
+      
 hiveConf
@@ -1665,10 +1665,14 @@
 ));%0A
-%09%09
+      
 Syst
@@ -1772,9 +1772,12 @@
 ));%0A
-%09
+    
 %7D%0A%0A 
@@ -2022,16 +2022,17 @@
 %7B%0A    if
+ 
 (ss != n
@@ -2035,16 +2035,24 @@
 != null)
+ %7B%0A     
  return 
@@ -2055,16 +2055,22 @@
 urn ss;%0A
+    %7D%0A
     ss =
@@ -2162,22 +2162,8 @@
  %7D%0A%0A
-  //@Override%0A
   pr

Fix for Issue #94: Hive queries that triggers a MapredLocalTask fail