HadoopArchives implements Tool { public int run(String[] args) throws Exception { try { Options options = new Options(); options.addOption(ARCHIVE_NAME, true, "Name of the Archive. This is mandatory option"); options.addOption(PARENT_PATH, true, "Parent path of sources. This is mandatory option"); options.addOption(REPLICATION, true, "Replication factor archive files"); options.addOption(HELP, false, "Show the usage"); Parser parser = new GnuParser(); CommandLine commandLine = parser.parse(options, args, true); if (commandLine.hasOption(HELP)) { printUsage(options, true); return 0; } if (!commandLine.hasOption(ARCHIVE_NAME)) { printUsage(options, false); throw new IOException("Archive Name not specified."); } String archiveName = commandLine.getOptionValue(ARCHIVE_NAME); if (!checkValidName(archiveName)) { printUsage(options, false); throw new IOException("Invalid name for archives. " + archiveName); } if (!commandLine.hasOption(PARENT_PATH)) { printUsage(options, false); throw new IOException("Parent path not specified."); } Path parentPath = new Path(commandLine.getOptionValue(PARENT_PATH)); if (!parentPath.isAbsolute()) { parentPath = parentPath.getFileSystem(getConf()).makeQualified( parentPath); } if (commandLine.hasOption(REPLICATION)) { repl = Short.parseShort(commandLine.getOptionValue(REPLICATION)); } args = commandLine.getArgs(); List<Path> srcPaths = new ArrayList<Path>(); Path destPath = null; for (int i = 0; i < args.length; i++) { if (i == (args.length - 1)) { destPath = new Path(args[i]); if (!destPath.isAbsolute()) { destPath = destPath.getFileSystem(getConf()).makeQualified(destPath); } } else { Path argPath = new Path(args[i]); if (argPath.isAbsolute()) { printUsage(options, false); throw new IOException("Source path " + argPath + " is not relative to "+ parentPath); } srcPaths.add(new Path(parentPath, argPath)); } } if (destPath == null) { printUsage(options, false); throw new IOException("Destination path not specified."); } if (srcPaths.size() == 0) { srcPaths.add(parentPath); } List<Path> globPaths = new ArrayList<Path>(); for (Path p: srcPaths) { FileSystem fs = p.getFileSystem(getConf()); FileStatus[] statuses = fs.globStatus(p); if (statuses != null) { for (FileStatus status: statuses) { globPaths.add(fs.makeQualified(status.getPath())); } } } if (globPaths.isEmpty()) { throw new IOException("The resolved paths set is empty." + " Please check whether the srcPaths exist, where srcPaths = " + srcPaths); } archive(parentPath, globPaths, archiveName, destPath); } catch(IOException ie) { System.err.println(ie.getLocalizedMessage()); return -1; } return 0; } HadoopArchives(Configuration conf); void setConf(Configuration conf); Configuration getConf(); int run(String[] args); static void main(String[] args); static final int VERSION; }
@Test public void testOutputPathValidity() throws Exception { final String inputPathStr = inputPath.toUri().getPath(); final URI uri = fs.getUri(); final String harName = "foo.har"; System.setProperty(HadoopArchives.TEST_HADOOP_ARCHIVES_JAR_PATH, HADOOP_ARCHIVES_JAR); final HadoopArchives har = new HadoopArchives(conf); PrintStream stderr = System.err; ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); PrintStream newErr = new PrintStream(byteStream); System.setErr(newErr); createFile(archivePath, fs, harName); final String[] args = { "-archiveName", harName, "-p", inputPathStr, "*", archivePath.toString() }; Assert.assertEquals(-1, ToolRunner.run(har, args)); String output = byteStream.toString(); final Path outputPath = new Path(archivePath, harName); Assert.assertTrue(output.indexOf("Archive path: " + outputPath.toString() + " already exists") != -1); byteStream.reset(); createFile(archivePath, fs, "sub1"); final Path archivePath2 = new Path(archivePath, "sub1"); final String[] args2 = { "-archiveName", harName, "-p", inputPathStr, "*", archivePath2.toString() }; Assert.assertEquals(-1, ToolRunner.run(har, args2)); output = byteStream.toString(); Assert.assertTrue(output.indexOf("Destination " + archivePath2.toString() + " should be a directory but is a file") != -1); System.setErr(stderr); }