@Test public void testPreserve() { DistCpOptions options = OptionsParser.parse(new String[] { "-f", "hdfs: "hdfs: Assert.assertFalse(options.shouldPreserve(FileAttribute.BLOCKSIZE)); Assert.assertFalse(options.shouldPreserve(FileAttribute.REPLICATION)); Assert.assertFalse(options.shouldPreserve(FileAttribute.PERMISSION)); Assert.assertFalse(options.shouldPreserve(FileAttribute.USER)); Assert.assertFalse(options.shouldPreserve(FileAttribute.GROUP)); Assert.assertFalse(options.shouldPreserve(FileAttribute.CHECKSUMTYPE)); options = OptionsParser.parse(new String[] { "-p", "-f", "hdfs: "hdfs: Assert.assertTrue(options.shouldPreserve(FileAttribute.BLOCKSIZE)); Assert.assertTrue(options.shouldPreserve(FileAttribute.REPLICATION)); Assert.assertTrue(options.shouldPreserve(FileAttribute.PERMISSION)); Assert.assertTrue(options.shouldPreserve(FileAttribute.USER)); Assert.assertTrue(options.shouldPreserve(FileAttribute.GROUP)); Assert.assertTrue(options.shouldPreserve(FileAttribute.CHECKSUMTYPE)); Assert.assertFalse(options.shouldPreserve(FileAttribute.ACL)); Assert.assertFalse(options.shouldPreserve(FileAttribute.XATTR)); options = OptionsParser.parse(new String[] { "-p", "hdfs: "hdfs: Assert.assertTrue(options.shouldPreserve(FileAttribute.BLOCKSIZE)); Assert.assertTrue(options.shouldPreserve(FileAttribute.REPLICATION)); Assert.assertTrue(options.shouldPreserve(FileAttribute.PERMISSION)); Assert.assertTrue(options.shouldPreserve(FileAttribute.USER)); Assert.assertTrue(options.shouldPreserve(FileAttribute.GROUP)); Assert.assertTrue(options.shouldPreserve(FileAttribute.CHECKSUMTYPE)); Assert.assertFalse(options.shouldPreserve(FileAttribute.ACL)); Assert.assertFalse(options.shouldPreserve(FileAttribute.XATTR)); options = OptionsParser.parse(new String[] { "-pbr", "-f", "hdfs: "hdfs: Assert.assertTrue(options.shouldPreserve(FileAttribute.BLOCKSIZE)); Assert.assertTrue(options.shouldPreserve(FileAttribute.REPLICATION)); Assert.assertFalse(options.shouldPreserve(FileAttribute.PERMISSION)); Assert.assertFalse(options.shouldPreserve(FileAttribute.USER)); Assert.assertFalse(options.shouldPreserve(FileAttribute.GROUP)); Assert.assertFalse(options.shouldPreserve(FileAttribute.CHECKSUMTYPE)); Assert.assertFalse(options.shouldPreserve(FileAttribute.ACL)); Assert.assertFalse(options.shouldPreserve(FileAttribute.XATTR)); options = OptionsParser.parse(new String[] { "-pbrgup", "-f", "hdfs: "hdfs: Assert.assertTrue(options.shouldPreserve(FileAttribute.BLOCKSIZE)); Assert.assertTrue(options.shouldPreserve(FileAttribute.REPLICATION)); Assert.assertTrue(options.shouldPreserve(FileAttribute.PERMISSION)); Assert.assertTrue(options.shouldPreserve(FileAttribute.USER)); Assert.assertTrue(options.shouldPreserve(FileAttribute.GROUP)); Assert.assertFalse(options.shouldPreserve(FileAttribute.CHECKSUMTYPE)); Assert.assertFalse(options.shouldPreserve(FileAttribute.ACL)); Assert.assertFalse(options.shouldPreserve(FileAttribute.XATTR)); options = OptionsParser.parse(new String[] { "-pbrgupcax", "-f", "hdfs: "hdfs: Assert.assertTrue(options.shouldPreserve(FileAttribute.BLOCKSIZE)); Assert.assertTrue(options.shouldPreserve(FileAttribute.REPLICATION)); Assert.assertTrue(options.shouldPreserve(FileAttribute.PERMISSION)); Assert.assertTrue(options.shouldPreserve(FileAttribute.USER)); Assert.assertTrue(options.shouldPreserve(FileAttribute.GROUP)); Assert.assertTrue(options.shouldPreserve(FileAttribute.CHECKSUMTYPE)); Assert.assertTrue(options.shouldPreserve(FileAttribute.ACL)); Assert.assertTrue(options.shouldPreserve(FileAttribute.XATTR)); options = OptionsParser.parse(new String[] { "-pc", "-f", "hdfs: "hdfs: Assert.assertFalse(options.shouldPreserve(FileAttribute.BLOCKSIZE)); Assert.assertFalse(options.shouldPreserve(FileAttribute.REPLICATION)); Assert.assertFalse(options.shouldPreserve(FileAttribute.PERMISSION)); Assert.assertFalse(options.shouldPreserve(FileAttribute.USER)); Assert.assertFalse(options.shouldPreserve(FileAttribute.GROUP)); Assert.assertTrue(options.shouldPreserve(FileAttribute.CHECKSUMTYPE)); Assert.assertFalse(options.shouldPreserve(FileAttribute.ACL)); Assert.assertFalse(options.shouldPreserve(FileAttribute.XATTR)); options = OptionsParser.parse(new String[] { "-p", "-f", "hdfs: "hdfs: int i = 0; Iterator<FileAttribute> attribIterator = options.preserveAttributes(); while (attribIterator.hasNext()) { attribIterator.next(); i++; } Assert.assertEquals(i, DistCpOptionSwitch.PRESERVE_STATUS_DEFAULT.length() - 2); try { OptionsParser.parse(new String[] { "-pabcd", "-f", "hdfs: "hdfs: Assert.fail("Invalid preserve attribute"); } catch (IllegalArgumentException ignore) {} catch (NoSuchElementException ignore) {} options = OptionsParser.parse(new String[] { "-f", "hdfs: "hdfs: Assert.assertFalse(options.shouldPreserve(FileAttribute.PERMISSION)); options.preserve(FileAttribute.PERMISSION); Assert.assertTrue(options.shouldPreserve(FileAttribute.PERMISSION)); options.preserve(FileAttribute.PERMISSION); Assert.assertTrue(options.shouldPreserve(FileAttribute.PERMISSION)); }
public static DistCpOptions parse(String args[]) throws IllegalArgumentException { CommandLineParser parser = new CustomParser(); CommandLine command; try { command = parser.parse(cliOptions, args, true); } catch (ParseException e) { throw new IllegalArgumentException("Unable to parse arguments. " + Arrays.toString(args), e); } DistCpOptions option; Path targetPath; List<Path> sourcePaths = new ArrayList<Path>(); String leftOverArgs[] = command.getArgs(); if (leftOverArgs == null || leftOverArgs.length < 1) { throw new IllegalArgumentException("Target path not specified"); } targetPath = new Path(leftOverArgs[leftOverArgs.length -1].trim()); for (int index = 0; index < leftOverArgs.length - 1; index++) { sourcePaths.add(new Path(leftOverArgs[index].trim())); } if (command.hasOption(DistCpOptionSwitch.SOURCE_FILE_LISTING.getSwitch())) { if (!sourcePaths.isEmpty()) { throw new IllegalArgumentException("Both source file listing and source paths present"); } option = new DistCpOptions(new Path(getVal(command, DistCpOptionSwitch. SOURCE_FILE_LISTING.getSwitch())), targetPath); } else { if (sourcePaths.isEmpty()) { throw new IllegalArgumentException("Neither source file listing nor source paths present"); } option = new DistCpOptions(sourcePaths, targetPath); } if (command.hasOption(DistCpOptionSwitch.IGNORE_FAILURES.getSwitch())) { option.setIgnoreFailures(true); } if (command.hasOption(DistCpOptionSwitch.ATOMIC_COMMIT.getSwitch())) { option.setAtomicCommit(true); } if (command.hasOption(DistCpOptionSwitch.WORK_PATH.getSwitch()) && option.shouldAtomicCommit()) { String workPath = getVal(command, DistCpOptionSwitch.WORK_PATH.getSwitch()); if (workPath != null && !workPath.isEmpty()) { option.setAtomicWorkPath(new Path(workPath)); } } else if (command.hasOption(DistCpOptionSwitch.WORK_PATH.getSwitch())) { throw new IllegalArgumentException("-tmp work-path can only be specified along with -atomic"); } if (command.hasOption(DistCpOptionSwitch.LOG_PATH.getSwitch())) { option.setLogPath(new Path(getVal(command, DistCpOptionSwitch.LOG_PATH.getSwitch()))); } if (command.hasOption(DistCpOptionSwitch.SYNC_FOLDERS.getSwitch())) { option.setSyncFolder(true); } if (command.hasOption(DistCpOptionSwitch.OVERWRITE.getSwitch())) { option.setOverwrite(true); } if (command.hasOption(DistCpOptionSwitch.APPEND.getSwitch())) { option.setAppend(true); } if (command.hasOption(DistCpOptionSwitch.DELETE_MISSING.getSwitch())) { option.setDeleteMissing(true); } if (command.hasOption(DistCpOptionSwitch.SKIP_CRC.getSwitch())) { option.setSkipCRC(true); } if (command.hasOption(DistCpOptionSwitch.BLOCKING.getSwitch())) { option.setBlocking(false); } if (command.hasOption(DistCpOptionSwitch.BANDWIDTH.getSwitch())) { try { Integer mapBandwidth = Integer.parseInt( getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()).trim()); if (mapBandwidth.intValue() <= 0) { throw new IllegalArgumentException("Bandwidth specified is not positive: " + mapBandwidth); } option.setMapBandwidth(mapBandwidth); } catch (NumberFormatException e) { throw new IllegalArgumentException("Bandwidth specified is invalid: " + getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()), e); } } if (command.hasOption(DistCpOptionSwitch.SSL_CONF.getSwitch())) { option.setSslConfigurationFile(command. getOptionValue(DistCpOptionSwitch.SSL_CONF.getSwitch())); } if (command.hasOption(DistCpOptionSwitch.MAX_MAPS.getSwitch())) { try { Integer maps = Integer.parseInt( getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()).trim()); option.setMaxMaps(maps); } catch (NumberFormatException e) { throw new IllegalArgumentException("Number of maps is invalid: " + getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()), e); } } if (command.hasOption(DistCpOptionSwitch.COPY_STRATEGY.getSwitch())) { option.setCopyStrategy( getVal(command, DistCpOptionSwitch.COPY_STRATEGY.getSwitch())); } if (command.hasOption(DistCpOptionSwitch.PRESERVE_STATUS.getSwitch())) { String attributes = getVal(command, DistCpOptionSwitch.PRESERVE_STATUS.getSwitch()); if (attributes == null || attributes.isEmpty()) { for (FileAttribute attribute : FileAttribute.values()) { option.preserve(attribute); } } else { for (int index = 0; index < attributes.length(); index++) { option.preserve(FileAttribute. getAttribute(attributes.charAt(index))); } } } if (command.hasOption(DistCpOptionSwitch.FILE_LIMIT.getSwitch())) { String fileLimitString = getVal(command, DistCpOptionSwitch.FILE_LIMIT.getSwitch().trim()); try { Integer.parseInt(fileLimitString); } catch (NumberFormatException e) { throw new IllegalArgumentException("File-limit is invalid: " + fileLimitString, e); } LOG.warn(DistCpOptionSwitch.FILE_LIMIT.getSwitch() + " is a deprecated" + " option. Ignoring."); } if (command.hasOption(DistCpOptionSwitch.SIZE_LIMIT.getSwitch())) { String sizeLimitString = getVal(command, DistCpOptionSwitch.SIZE_LIMIT.getSwitch().trim()); try { Long.parseLong(sizeLimitString); } catch (NumberFormatException e) { throw new IllegalArgumentException("Size-limit is invalid: " + sizeLimitString, e); } LOG.warn(DistCpOptionSwitch.SIZE_LIMIT.getSwitch() + " is a deprecated" + " option. Ignoring."); } return option; }
OptionsParser { public static DistCpOptions parse(String args[]) throws IllegalArgumentException { CommandLineParser parser = new CustomParser(); CommandLine command; try { command = parser.parse(cliOptions, args, true); } catch (ParseException e) { throw new IllegalArgumentException("Unable to parse arguments. " + Arrays.toString(args), e); } DistCpOptions option; Path targetPath; List<Path> sourcePaths = new ArrayList<Path>(); String leftOverArgs[] = command.getArgs(); if (leftOverArgs == null || leftOverArgs.length < 1) { throw new IllegalArgumentException("Target path not specified"); } targetPath = new Path(leftOverArgs[leftOverArgs.length -1].trim()); for (int index = 0; index < leftOverArgs.length - 1; index++) { sourcePaths.add(new Path(leftOverArgs[index].trim())); } if (command.hasOption(DistCpOptionSwitch.SOURCE_FILE_LISTING.getSwitch())) { if (!sourcePaths.isEmpty()) { throw new IllegalArgumentException("Both source file listing and source paths present"); } option = new DistCpOptions(new Path(getVal(command, DistCpOptionSwitch. SOURCE_FILE_LISTING.getSwitch())), targetPath); } else { if (sourcePaths.isEmpty()) { throw new IllegalArgumentException("Neither source file listing nor source paths present"); } option = new DistCpOptions(sourcePaths, targetPath); } if (command.hasOption(DistCpOptionSwitch.IGNORE_FAILURES.getSwitch())) { option.setIgnoreFailures(true); } if (command.hasOption(DistCpOptionSwitch.ATOMIC_COMMIT.getSwitch())) { option.setAtomicCommit(true); } if (command.hasOption(DistCpOptionSwitch.WORK_PATH.getSwitch()) && option.shouldAtomicCommit()) { String workPath = getVal(command, DistCpOptionSwitch.WORK_PATH.getSwitch()); if (workPath != null && !workPath.isEmpty()) { option.setAtomicWorkPath(new Path(workPath)); } } else if (command.hasOption(DistCpOptionSwitch.WORK_PATH.getSwitch())) { throw new IllegalArgumentException("-tmp work-path can only be specified along with -atomic"); } if (command.hasOption(DistCpOptionSwitch.LOG_PATH.getSwitch())) { option.setLogPath(new Path(getVal(command, DistCpOptionSwitch.LOG_PATH.getSwitch()))); } if (command.hasOption(DistCpOptionSwitch.SYNC_FOLDERS.getSwitch())) { option.setSyncFolder(true); } if (command.hasOption(DistCpOptionSwitch.OVERWRITE.getSwitch())) { option.setOverwrite(true); } if (command.hasOption(DistCpOptionSwitch.APPEND.getSwitch())) { option.setAppend(true); } if (command.hasOption(DistCpOptionSwitch.DELETE_MISSING.getSwitch())) { option.setDeleteMissing(true); } if (command.hasOption(DistCpOptionSwitch.SKIP_CRC.getSwitch())) { option.setSkipCRC(true); } if (command.hasOption(DistCpOptionSwitch.BLOCKING.getSwitch())) { option.setBlocking(false); } if (command.hasOption(DistCpOptionSwitch.BANDWIDTH.getSwitch())) { try { Integer mapBandwidth = Integer.parseInt( getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()).trim()); if (mapBandwidth.intValue() <= 0) { throw new IllegalArgumentException("Bandwidth specified is not positive: " + mapBandwidth); } option.setMapBandwidth(mapBandwidth); } catch (NumberFormatException e) { throw new IllegalArgumentException("Bandwidth specified is invalid: " + getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()), e); } } if (command.hasOption(DistCpOptionSwitch.SSL_CONF.getSwitch())) { option.setSslConfigurationFile(command. getOptionValue(DistCpOptionSwitch.SSL_CONF.getSwitch())); } if (command.hasOption(DistCpOptionSwitch.MAX_MAPS.getSwitch())) { try { Integer maps = Integer.parseInt( getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()).trim()); option.setMaxMaps(maps); } catch (NumberFormatException e) { throw new IllegalArgumentException("Number of maps is invalid: " + getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()), e); } } if (command.hasOption(DistCpOptionSwitch.COPY_STRATEGY.getSwitch())) { option.setCopyStrategy( getVal(command, DistCpOptionSwitch.COPY_STRATEGY.getSwitch())); } if (command.hasOption(DistCpOptionSwitch.PRESERVE_STATUS.getSwitch())) { String attributes = getVal(command, DistCpOptionSwitch.PRESERVE_STATUS.getSwitch()); if (attributes == null || attributes.isEmpty()) { for (FileAttribute attribute : FileAttribute.values()) { option.preserve(attribute); } } else { for (int index = 0; index < attributes.length(); index++) { option.preserve(FileAttribute. getAttribute(attributes.charAt(index))); } } } if (command.hasOption(DistCpOptionSwitch.FILE_LIMIT.getSwitch())) { String fileLimitString = getVal(command, DistCpOptionSwitch.FILE_LIMIT.getSwitch().trim()); try { Integer.parseInt(fileLimitString); } catch (NumberFormatException e) { throw new IllegalArgumentException("File-limit is invalid: " + fileLimitString, e); } LOG.warn(DistCpOptionSwitch.FILE_LIMIT.getSwitch() + " is a deprecated" + " option. Ignoring."); } if (command.hasOption(DistCpOptionSwitch.SIZE_LIMIT.getSwitch())) { String sizeLimitString = getVal(command, DistCpOptionSwitch.SIZE_LIMIT.getSwitch().trim()); try { Long.parseLong(sizeLimitString); } catch (NumberFormatException e) { throw new IllegalArgumentException("Size-limit is invalid: " + sizeLimitString, e); } LOG.warn(DistCpOptionSwitch.SIZE_LIMIT.getSwitch() + " is a deprecated" + " option. Ignoring."); } return option; } }
OptionsParser { public static DistCpOptions parse(String args[]) throws IllegalArgumentException { CommandLineParser parser = new CustomParser(); CommandLine command; try { command = parser.parse(cliOptions, args, true); } catch (ParseException e) { throw new IllegalArgumentException("Unable to parse arguments. " + Arrays.toString(args), e); } DistCpOptions option; Path targetPath; List<Path> sourcePaths = new ArrayList<Path>(); String leftOverArgs[] = command.getArgs(); if (leftOverArgs == null || leftOverArgs.length < 1) { throw new IllegalArgumentException("Target path not specified"); } targetPath = new Path(leftOverArgs[leftOverArgs.length -1].trim()); for (int index = 0; index < leftOverArgs.length - 1; index++) { sourcePaths.add(new Path(leftOverArgs[index].trim())); } if (command.hasOption(DistCpOptionSwitch.SOURCE_FILE_LISTING.getSwitch())) { if (!sourcePaths.isEmpty()) { throw new IllegalArgumentException("Both source file listing and source paths present"); } option = new DistCpOptions(new Path(getVal(command, DistCpOptionSwitch. SOURCE_FILE_LISTING.getSwitch())), targetPath); } else { if (sourcePaths.isEmpty()) { throw new IllegalArgumentException("Neither source file listing nor source paths present"); } option = new DistCpOptions(sourcePaths, targetPath); } if (command.hasOption(DistCpOptionSwitch.IGNORE_FAILURES.getSwitch())) { option.setIgnoreFailures(true); } if (command.hasOption(DistCpOptionSwitch.ATOMIC_COMMIT.getSwitch())) { option.setAtomicCommit(true); } if (command.hasOption(DistCpOptionSwitch.WORK_PATH.getSwitch()) && option.shouldAtomicCommit()) { String workPath = getVal(command, DistCpOptionSwitch.WORK_PATH.getSwitch()); if (workPath != null && !workPath.isEmpty()) { option.setAtomicWorkPath(new Path(workPath)); } } else if (command.hasOption(DistCpOptionSwitch.WORK_PATH.getSwitch())) { throw new IllegalArgumentException("-tmp work-path can only be specified along with -atomic"); } if (command.hasOption(DistCpOptionSwitch.LOG_PATH.getSwitch())) { option.setLogPath(new Path(getVal(command, DistCpOptionSwitch.LOG_PATH.getSwitch()))); } if (command.hasOption(DistCpOptionSwitch.SYNC_FOLDERS.getSwitch())) { option.setSyncFolder(true); } if (command.hasOption(DistCpOptionSwitch.OVERWRITE.getSwitch())) { option.setOverwrite(true); } if (command.hasOption(DistCpOptionSwitch.APPEND.getSwitch())) { option.setAppend(true); } if (command.hasOption(DistCpOptionSwitch.DELETE_MISSING.getSwitch())) { option.setDeleteMissing(true); } if (command.hasOption(DistCpOptionSwitch.SKIP_CRC.getSwitch())) { option.setSkipCRC(true); } if (command.hasOption(DistCpOptionSwitch.BLOCKING.getSwitch())) { option.setBlocking(false); } if (command.hasOption(DistCpOptionSwitch.BANDWIDTH.getSwitch())) { try { Integer mapBandwidth = Integer.parseInt( getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()).trim()); if (mapBandwidth.intValue() <= 0) { throw new IllegalArgumentException("Bandwidth specified is not positive: " + mapBandwidth); } option.setMapBandwidth(mapBandwidth); } catch (NumberFormatException e) { throw new IllegalArgumentException("Bandwidth specified is invalid: " + getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()), e); } } if (command.hasOption(DistCpOptionSwitch.SSL_CONF.getSwitch())) { option.setSslConfigurationFile(command. getOptionValue(DistCpOptionSwitch.SSL_CONF.getSwitch())); } if (command.hasOption(DistCpOptionSwitch.MAX_MAPS.getSwitch())) { try { Integer maps = Integer.parseInt( getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()).trim()); option.setMaxMaps(maps); } catch (NumberFormatException e) { throw new IllegalArgumentException("Number of maps is invalid: " + getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()), e); } } if (command.hasOption(DistCpOptionSwitch.COPY_STRATEGY.getSwitch())) { option.setCopyStrategy( getVal(command, DistCpOptionSwitch.COPY_STRATEGY.getSwitch())); } if (command.hasOption(DistCpOptionSwitch.PRESERVE_STATUS.getSwitch())) { String attributes = getVal(command, DistCpOptionSwitch.PRESERVE_STATUS.getSwitch()); if (attributes == null || attributes.isEmpty()) { for (FileAttribute attribute : FileAttribute.values()) { option.preserve(attribute); } } else { for (int index = 0; index < attributes.length(); index++) { option.preserve(FileAttribute. getAttribute(attributes.charAt(index))); } } } if (command.hasOption(DistCpOptionSwitch.FILE_LIMIT.getSwitch())) { String fileLimitString = getVal(command, DistCpOptionSwitch.FILE_LIMIT.getSwitch().trim()); try { Integer.parseInt(fileLimitString); } catch (NumberFormatException e) { throw new IllegalArgumentException("File-limit is invalid: " + fileLimitString, e); } LOG.warn(DistCpOptionSwitch.FILE_LIMIT.getSwitch() + " is a deprecated" + " option. Ignoring."); } if (command.hasOption(DistCpOptionSwitch.SIZE_LIMIT.getSwitch())) { String sizeLimitString = getVal(command, DistCpOptionSwitch.SIZE_LIMIT.getSwitch().trim()); try { Long.parseLong(sizeLimitString); } catch (NumberFormatException e) { throw new IllegalArgumentException("Size-limit is invalid: " + sizeLimitString, e); } LOG.warn(DistCpOptionSwitch.SIZE_LIMIT.getSwitch() + " is a deprecated" + " option. Ignoring."); } return option; }  }
OptionsParser { public static DistCpOptions parse(String args[]) throws IllegalArgumentException { CommandLineParser parser = new CustomParser(); CommandLine command; try { command = parser.parse(cliOptions, args, true); } catch (ParseException e) { throw new IllegalArgumentException("Unable to parse arguments. " + Arrays.toString(args), e); } DistCpOptions option; Path targetPath; List<Path> sourcePaths = new ArrayList<Path>(); String leftOverArgs[] = command.getArgs(); if (leftOverArgs == null || leftOverArgs.length < 1) { throw new IllegalArgumentException("Target path not specified"); } targetPath = new Path(leftOverArgs[leftOverArgs.length -1].trim()); for (int index = 0; index < leftOverArgs.length - 1; index++) { sourcePaths.add(new Path(leftOverArgs[index].trim())); } if (command.hasOption(DistCpOptionSwitch.SOURCE_FILE_LISTING.getSwitch())) { if (!sourcePaths.isEmpty()) { throw new IllegalArgumentException("Both source file listing and source paths present"); } option = new DistCpOptions(new Path(getVal(command, DistCpOptionSwitch. SOURCE_FILE_LISTING.getSwitch())), targetPath); } else { if (sourcePaths.isEmpty()) { throw new IllegalArgumentException("Neither source file listing nor source paths present"); } option = new DistCpOptions(sourcePaths, targetPath); } if (command.hasOption(DistCpOptionSwitch.IGNORE_FAILURES.getSwitch())) { option.setIgnoreFailures(true); } if (command.hasOption(DistCpOptionSwitch.ATOMIC_COMMIT.getSwitch())) { option.setAtomicCommit(true); } if (command.hasOption(DistCpOptionSwitch.WORK_PATH.getSwitch()) && option.shouldAtomicCommit()) { String workPath = getVal(command, DistCpOptionSwitch.WORK_PATH.getSwitch()); if (workPath != null && !workPath.isEmpty()) { option.setAtomicWorkPath(new Path(workPath)); } } else if (command.hasOption(DistCpOptionSwitch.WORK_PATH.getSwitch())) { throw new IllegalArgumentException("-tmp work-path can only be specified along with -atomic"); } if (command.hasOption(DistCpOptionSwitch.LOG_PATH.getSwitch())) { option.setLogPath(new Path(getVal(command, DistCpOptionSwitch.LOG_PATH.getSwitch()))); } if (command.hasOption(DistCpOptionSwitch.SYNC_FOLDERS.getSwitch())) { option.setSyncFolder(true); } if (command.hasOption(DistCpOptionSwitch.OVERWRITE.getSwitch())) { option.setOverwrite(true); } if (command.hasOption(DistCpOptionSwitch.APPEND.getSwitch())) { option.setAppend(true); } if (command.hasOption(DistCpOptionSwitch.DELETE_MISSING.getSwitch())) { option.setDeleteMissing(true); } if (command.hasOption(DistCpOptionSwitch.SKIP_CRC.getSwitch())) { option.setSkipCRC(true); } if (command.hasOption(DistCpOptionSwitch.BLOCKING.getSwitch())) { option.setBlocking(false); } if (command.hasOption(DistCpOptionSwitch.BANDWIDTH.getSwitch())) { try { Integer mapBandwidth = Integer.parseInt( getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()).trim()); if (mapBandwidth.intValue() <= 0) { throw new IllegalArgumentException("Bandwidth specified is not positive: " + mapBandwidth); } option.setMapBandwidth(mapBandwidth); } catch (NumberFormatException e) { throw new IllegalArgumentException("Bandwidth specified is invalid: " + getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()), e); } } if (command.hasOption(DistCpOptionSwitch.SSL_CONF.getSwitch())) { option.setSslConfigurationFile(command. getOptionValue(DistCpOptionSwitch.SSL_CONF.getSwitch())); } if (command.hasOption(DistCpOptionSwitch.MAX_MAPS.getSwitch())) { try { Integer maps = Integer.parseInt( getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()).trim()); option.setMaxMaps(maps); } catch (NumberFormatException e) { throw new IllegalArgumentException("Number of maps is invalid: " + getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()), e); } } if (command.hasOption(DistCpOptionSwitch.COPY_STRATEGY.getSwitch())) { option.setCopyStrategy( getVal(command, DistCpOptionSwitch.COPY_STRATEGY.getSwitch())); } if (command.hasOption(DistCpOptionSwitch.PRESERVE_STATUS.getSwitch())) { String attributes = getVal(command, DistCpOptionSwitch.PRESERVE_STATUS.getSwitch()); if (attributes == null || attributes.isEmpty()) { for (FileAttribute attribute : FileAttribute.values()) { option.preserve(attribute); } } else { for (int index = 0; index < attributes.length(); index++) { option.preserve(FileAttribute. getAttribute(attributes.charAt(index))); } } } if (command.hasOption(DistCpOptionSwitch.FILE_LIMIT.getSwitch())) { String fileLimitString = getVal(command, DistCpOptionSwitch.FILE_LIMIT.getSwitch().trim()); try { Integer.parseInt(fileLimitString); } catch (NumberFormatException e) { throw new IllegalArgumentException("File-limit is invalid: " + fileLimitString, e); } LOG.warn(DistCpOptionSwitch.FILE_LIMIT.getSwitch() + " is a deprecated" + " option. Ignoring."); } if (command.hasOption(DistCpOptionSwitch.SIZE_LIMIT.getSwitch())) { String sizeLimitString = getVal(command, DistCpOptionSwitch.SIZE_LIMIT.getSwitch().trim()); try { Long.parseLong(sizeLimitString); } catch (NumberFormatException e) { throw new IllegalArgumentException("Size-limit is invalid: " + sizeLimitString, e); } LOG.warn(DistCpOptionSwitch.SIZE_LIMIT.getSwitch() + " is a deprecated" + " option. Ignoring."); } return option; }  static DistCpOptions parse(String args[]); static void usage(); }
OptionsParser { public static DistCpOptions parse(String args[]) throws IllegalArgumentException { CommandLineParser parser = new CustomParser(); CommandLine command; try { command = parser.parse(cliOptions, args, true); } catch (ParseException e) { throw new IllegalArgumentException("Unable to parse arguments. " + Arrays.toString(args), e); } DistCpOptions option; Path targetPath; List<Path> sourcePaths = new ArrayList<Path>(); String leftOverArgs[] = command.getArgs(); if (leftOverArgs == null || leftOverArgs.length < 1) { throw new IllegalArgumentException("Target path not specified"); } targetPath = new Path(leftOverArgs[leftOverArgs.length -1].trim()); for (int index = 0; index < leftOverArgs.length - 1; index++) { sourcePaths.add(new Path(leftOverArgs[index].trim())); } if (command.hasOption(DistCpOptionSwitch.SOURCE_FILE_LISTING.getSwitch())) { if (!sourcePaths.isEmpty()) { throw new IllegalArgumentException("Both source file listing and source paths present"); } option = new DistCpOptions(new Path(getVal(command, DistCpOptionSwitch. SOURCE_FILE_LISTING.getSwitch())), targetPath); } else { if (sourcePaths.isEmpty()) { throw new IllegalArgumentException("Neither source file listing nor source paths present"); } option = new DistCpOptions(sourcePaths, targetPath); } if (command.hasOption(DistCpOptionSwitch.IGNORE_FAILURES.getSwitch())) { option.setIgnoreFailures(true); } if (command.hasOption(DistCpOptionSwitch.ATOMIC_COMMIT.getSwitch())) { option.setAtomicCommit(true); } if (command.hasOption(DistCpOptionSwitch.WORK_PATH.getSwitch()) && option.shouldAtomicCommit()) { String workPath = getVal(command, DistCpOptionSwitch.WORK_PATH.getSwitch()); if (workPath != null && !workPath.isEmpty()) { option.setAtomicWorkPath(new Path(workPath)); } } else if (command.hasOption(DistCpOptionSwitch.WORK_PATH.getSwitch())) { throw new IllegalArgumentException("-tmp work-path can only be specified along with -atomic"); } if (command.hasOption(DistCpOptionSwitch.LOG_PATH.getSwitch())) { option.setLogPath(new Path(getVal(command, DistCpOptionSwitch.LOG_PATH.getSwitch()))); } if (command.hasOption(DistCpOptionSwitch.SYNC_FOLDERS.getSwitch())) { option.setSyncFolder(true); } if (command.hasOption(DistCpOptionSwitch.OVERWRITE.getSwitch())) { option.setOverwrite(true); } if (command.hasOption(DistCpOptionSwitch.APPEND.getSwitch())) { option.setAppend(true); } if (command.hasOption(DistCpOptionSwitch.DELETE_MISSING.getSwitch())) { option.setDeleteMissing(true); } if (command.hasOption(DistCpOptionSwitch.SKIP_CRC.getSwitch())) { option.setSkipCRC(true); } if (command.hasOption(DistCpOptionSwitch.BLOCKING.getSwitch())) { option.setBlocking(false); } if (command.hasOption(DistCpOptionSwitch.BANDWIDTH.getSwitch())) { try { Integer mapBandwidth = Integer.parseInt( getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()).trim()); if (mapBandwidth.intValue() <= 0) { throw new IllegalArgumentException("Bandwidth specified is not positive: " + mapBandwidth); } option.setMapBandwidth(mapBandwidth); } catch (NumberFormatException e) { throw new IllegalArgumentException("Bandwidth specified is invalid: " + getVal(command, DistCpOptionSwitch.BANDWIDTH.getSwitch()), e); } } if (command.hasOption(DistCpOptionSwitch.SSL_CONF.getSwitch())) { option.setSslConfigurationFile(command. getOptionValue(DistCpOptionSwitch.SSL_CONF.getSwitch())); } if (command.hasOption(DistCpOptionSwitch.MAX_MAPS.getSwitch())) { try { Integer maps = Integer.parseInt( getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()).trim()); option.setMaxMaps(maps); } catch (NumberFormatException e) { throw new IllegalArgumentException("Number of maps is invalid: " + getVal(command, DistCpOptionSwitch.MAX_MAPS.getSwitch()), e); } } if (command.hasOption(DistCpOptionSwitch.COPY_STRATEGY.getSwitch())) { option.setCopyStrategy( getVal(command, DistCpOptionSwitch.COPY_STRATEGY.getSwitch())); } if (command.hasOption(DistCpOptionSwitch.PRESERVE_STATUS.getSwitch())) { String attributes = getVal(command, DistCpOptionSwitch.PRESERVE_STATUS.getSwitch()); if (attributes == null || attributes.isEmpty()) { for (FileAttribute attribute : FileAttribute.values()) { option.preserve(attribute); } } else { for (int index = 0; index < attributes.length(); index++) { option.preserve(FileAttribute. getAttribute(attributes.charAt(index))); } } } if (command.hasOption(DistCpOptionSwitch.FILE_LIMIT.getSwitch())) { String fileLimitString = getVal(command, DistCpOptionSwitch.FILE_LIMIT.getSwitch().trim()); try { Integer.parseInt(fileLimitString); } catch (NumberFormatException e) { throw new IllegalArgumentException("File-limit is invalid: " + fileLimitString, e); } LOG.warn(DistCpOptionSwitch.FILE_LIMIT.getSwitch() + " is a deprecated" + " option. Ignoring."); } if (command.hasOption(DistCpOptionSwitch.SIZE_LIMIT.getSwitch())) { String sizeLimitString = getVal(command, DistCpOptionSwitch.SIZE_LIMIT.getSwitch().trim()); try { Long.parseLong(sizeLimitString); } catch (NumberFormatException e) { throw new IllegalArgumentException("Size-limit is invalid: " + sizeLimitString, e); } LOG.warn(DistCpOptionSwitch.SIZE_LIMIT.getSwitch() + " is a deprecated" + " option. Ignoring."); } return option; }  static DistCpOptions parse(String args[]); static void usage();  }