/*
 * Copyright (C) 2016 LinkedIn Corp. 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.
 */
package azkaban.jobtype.connectors.teradata;

import static azkaban.jobtype.connectors.teradata.TdchConstants.*;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.common.collect.ImmutableList;

import azkaban.jobtype.HadoopJavaJob;
import azkaban.jobtype.javautils.Whitelist;
import azkaban.utils.Props;

public abstract class TeradataJob extends HadoopJavaJob {
  private static final Logger logger = Logger.getLogger(TeradataJob.class);

  public TeradataJob(String jobid, Props sysProps, Props jobProps, Logger log) {
    super(jobid, sysProps, jobProps, log);
    jobProps.put(LIB_JARS_KEY, sysProps.get(LIB_JARS_KEY));
    //Initialize TDWallet if it hasn't on current JVM.
    File tempDir = new File(sysProps.getString("azkaban.temp.dir", "temp"));
    TeraDataWalletInitializer.initialize(tempDir, new File(sysProps.get(TD_WALLET_JAR)));

    if(sysProps.containsKey(Whitelist.WHITE_LIST_FILE_PATH_KEY)) {
      jobProps.put(Whitelist.WHITE_LIST_FILE_PATH_KEY, sysProps.getString(Whitelist.WHITE_LIST_FILE_PATH_KEY));
    }
  }

  @Override
  protected abstract String getJavaClass();

  /**
   * In addition to superclass's classpath, it adds jars from TDWallet unjarred folder.
   * {@inheritDoc}
   * @see azkaban.jobtype.HadoopJavaJob#getClassPaths()
   */
  @Override
  protected List<String> getClassPaths() {
    return ImmutableList.<String>builder()
                        //TDCH w. Tdwallet requires a classpath point to unjarred folder.
                        .add(TeraDataWalletInitializer.getTdchUnjarFolder())
                        .add(TeraDataWalletInitializer.getTdchUnjarFolder() + File.separator + "lib" + File.separator + "*")
                        .addAll(super.getClassPaths()).build();
  }
}

@@ -655,24 +655,58 @@
 a.util.List;
+%0Aimport java.util.StringTokenizer;
 %0A%0Aimport org
@@ -1903,24 +1903,88 @@
 ssPaths() %7B%0A
+%0A    List%3CString%3E hadoopJavaClassPath = super.getClassPaths();%0A%0A
     return I
@@ -2338,46 +2338,1083 @@
 .add
-All(super.getClassPaths()).build();
+(getSourcePathFromClass(TeradataToHdfsJobRunnerMain.class))%0A                        .addAll(hadoopJavaClassPath).build();%0A  %7D%0A%0A%0A  // Copy the method from HadoopJavaJob class. Since TeradataJob will eventually move to the main%0A  // AZ repo, we'd keep private method in HadoopJava, and will deprecate this method during the%0A  // move.%0A  // TODO kunkun-tang: Remove the method when moving teradata job to the main AZ repo.%0A  private static String getSourcePathFromClass(Class%3C?%3E containedClass) %7B%0A    File file =%0A        new File(containedClass.getProtectionDomain().getCodeSource()%0A            .getLocation().getPath());%0A%0A    if (!file.isDirectory() && file.getName().endsWith(%22.class%22)) %7B%0A      String name = containedClass.getName();%0A      StringTokenizer tokenizer = new StringTokenizer(name, %22.%22);%0A      while (tokenizer.hasMoreTokens()) %7B%0A        tokenizer.nextElement();%0A%0A        file = file.getParentFile();%0A      %7D%0A      return file.getPath();%0A    %7D else %7B%0A      return containedClass.getProtectionDomain().getCodeSource().getLocation()%0A          .getPath();%0A    %7D
 %0A  %7D%0A
-
 %7D%0A
+%0A%0A

fix teradata jar missing issue (#293)