Review Board 1.7.22


SQOOP-959 Use CredentialsUtil.fetchPasswordFromFile in BaseSqoopTool

Review Request #10049 - Created March 20, 2013 and submitted

Jarek Cecho
SQOOP-959
Reviewers
Sqoop
sqoop-trunk
I've removed the extra method in BaseSqoopTool.
Unit tests seems to be passing and I've test it on real cluster as well.

Diff revision 1 (Latest)

  1. src/java/org/apache/sqoop/tool/BaseSqoopTool.java: Loading...
src/java/org/apache/sqoop/tool/BaseSqoopTool.java
Revision e687137d2ac7d6527c81a197b1277108a416bea6 New Change
[20] 19 lines
[+20]
20

    
   
20

   
21
import java.io.File;
21
import java.io.File;
22
import java.io.FileInputStream;
22
import java.io.FileInputStream;
23
import java.io.IOException;
23
import java.io.IOException;
24
import java.io.InputStream;
24
import java.io.InputStream;
25
import java.io.StringWriter;

   
26
import java.sql.SQLException;
25
import java.sql.SQLException;
27
import java.util.Arrays;
26
import java.util.Arrays;
28
import java.util.Properties;
27
import java.util.Properties;
29

    
   
28

   
30
import org.apache.commons.cli.CommandLine;
29
import org.apache.commons.cli.CommandLine;
31
import org.apache.commons.cli.Option;
30
import org.apache.commons.cli.Option;
32
import org.apache.commons.cli.OptionBuilder;
31
import org.apache.commons.cli.OptionBuilder;
33
import org.apache.commons.cli.OptionGroup;
32
import org.apache.commons.cli.OptionGroup;
34
import org.apache.commons.io.IOUtils;

   
35
import org.apache.commons.logging.Log;
33
import org.apache.commons.logging.Log;
36
import org.apache.commons.logging.LogFactory;
34
import org.apache.commons.logging.LogFactory;
37
import org.apache.hadoop.fs.FileSystem;

   
38
import org.apache.hadoop.fs.Path;

   
39
import org.apache.hadoop.util.StringUtils;
35
import org.apache.hadoop.util.StringUtils;
40

    
   
36

   
41
import com.cloudera.sqoop.ConnFactory;
37
import com.cloudera.sqoop.ConnFactory;
42
import com.cloudera.sqoop.Sqoop;
38
import com.cloudera.sqoop.Sqoop;
43
import com.cloudera.sqoop.SqoopOptions;
39
import com.cloudera.sqoop.SqoopOptions;
44
import com.cloudera.sqoop.SqoopOptions.InvalidOptionsException;
40
import com.cloudera.sqoop.SqoopOptions.InvalidOptionsException;
45
import com.cloudera.sqoop.cli.RelatedOptions;
41
import com.cloudera.sqoop.cli.RelatedOptions;
46
import com.cloudera.sqoop.cli.ToolOptions;
42
import com.cloudera.sqoop.cli.ToolOptions;
47
import com.cloudera.sqoop.lib.DelimiterSet;
43
import com.cloudera.sqoop.lib.DelimiterSet;
48
import com.cloudera.sqoop.manager.ConnManager;
44
import com.cloudera.sqoop.manager.ConnManager;
49
import com.cloudera.sqoop.metastore.JobData;
45
import com.cloudera.sqoop.metastore.JobData;

    
   
46
import org.apache.sqoop.util.CredentialsUtil;
50
import org.apache.sqoop.util.LoggingUtils;
47
import org.apache.sqoop.util.LoggingUtils;
51

    
   
48

   
52
/**
49
/**
53
 * Layer on top of SqoopTool that provides some basic common code
50
 * Layer on top of SqoopTool that provides some basic common code
54
 * that most SqoopTool implementations will use.
51
 * that most SqoopTool implementations will use.
[+20] [20] 724 lines
[+20] [+] private void applyCredentialsOptions(CommandLine in, SqoopOptions out)
779
      }
776
      }
780

    
   
777

   
781
      try {
778
      try {
782
        out.setPasswordFilePath(in.getOptionValue(PASSWORD_PATH_ARG));
779
        out.setPasswordFilePath(in.getOptionValue(PASSWORD_PATH_ARG));
783
        // apply password from file into password in options
780
        // apply password from file into password in options
784
        out.setPassword(fetchPasswordFromFile(out));
781
        out.setPassword(CredentialsUtil.fetchPasswordFromFile(out));
785
      } catch (IOException ex) {
782
      } catch (IOException ex) {
786
        LOG.warn("Failed to load connection parameter file", ex);
783
        LOG.warn("Failed to load connection parameter file", ex);
787
        throw new InvalidOptionsException(
784
        throw new InvalidOptionsException(
788
          "Error while loading connection parameter file: "
785
          "Error while loading connection parameter file: "
789
            + ex.getMessage());
786
            + ex.getMessage());
790
      }
787
      }
791
    }
788
    }
792
  }
789
  }
793

    
   
790

   
794
  private String fetchPasswordFromFile(SqoopOptions options)

   
795
    throws IOException {

   
796
    String passwordFilePath = options.getPasswordFilePath();

   
797
    if (passwordFilePath == null) {

   
798
      return options.getPassword();

   
799
    }

   
800

    
   

   
801
    LOG.debug("Fetching password from specified path: " + passwordFilePath);

   
802
    FileSystem fs = FileSystem.get(options.getConf());

   
803
    Path path = new Path(passwordFilePath);

   
804

    
   

   
805
    if (!fs.exists(path)) {

   
806
      throw new IOException("The password file does not exist! "

   
807
        + passwordFilePath);

   
808
    }

   
809

    
   

   
810
    if (!fs.isFile(path)) {

   
811
      throw new IOException("The password file cannot be a directory! "

   
812
        + passwordFilePath);

   
813
    }

   
814

    
   

   
815
    InputStream is = fs.open(path);

   
816
    StringWriter writer = new StringWriter();

   
817
    try {

   
818
      IOUtils.copy(is, writer);

   
819
      return writer.toString();

   
820
    } finally {

   
821
      IOUtils.closeQuietly(is);

   
822
      IOUtils.closeQuietly(writer);

   
823
      fs.close();

   
824
    }

   
825
  }

   
826

    
   

   
827
  protected void applyHiveOptions(CommandLine in, SqoopOptions out)
791
  protected void applyHiveOptions(CommandLine in, SqoopOptions out)
828
      throws InvalidOptionsException {
792
      throws InvalidOptionsException {
829

    
   
793

   
830
    if (in.hasOption(HIVE_HOME_ARG)) {
794
    if (in.hasOption(HIVE_HOME_ARG)) {
831
      out.setHiveHome(in.getOptionValue(HIVE_HOME_ARG));
795
      out.setHiveHome(in.getOptionValue(HIVE_HOME_ARG));
[+20] [20] 317 lines
  1. src/java/org/apache/sqoop/tool/BaseSqoopTool.java: Loading...