List of usage examples for org.apache.commons.cli CommandLine getArgList
public List getArgList()
From source file:org.apache.s4.MainApp.java
public static void main(String args[]) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("corehome").hasArg().withDescription("core home").create("c")); options.addOption(/*from w ww. ja va2 s.co m*/ OptionBuilder.withArgName("appshome").hasArg().withDescription("applications home").create("a")); options.addOption(OptionBuilder.withArgName("s4clock").hasArg().withDescription("s4 clock").create("d")); options.addOption(OptionBuilder.withArgName("seedtime").hasArg() .withDescription("event clock initialization time").create("s")); options.addOption( OptionBuilder.withArgName("extshome").hasArg().withDescription("extensions home").create("e")); options.addOption( OptionBuilder.withArgName("instanceid").hasArg().withDescription("instance id").create("i")); options.addOption( OptionBuilder.withArgName("configtype").hasArg().withDescription("configuration type").create("t")); CommandLineParser parser = new GnuParser(); CommandLine commandLine = null; String clockType = "wall"; try { commandLine = parser.parse(options, args); } catch (ParseException pe) { System.err.println(pe.getLocalizedMessage()); System.exit(1); } int instanceId = -1; if (commandLine.hasOption("i")) { String instanceIdStr = commandLine.getOptionValue("i"); try { instanceId = Integer.parseInt(instanceIdStr); } catch (NumberFormatException nfe) { System.err.println("Bad instance id: %s" + instanceIdStr); System.exit(1); } } if (commandLine.hasOption("c")) { coreHome = commandLine.getOptionValue("c"); } if (commandLine.hasOption("a")) { appsHome = commandLine.getOptionValue("a"); } if (commandLine.hasOption("d")) { clockType = commandLine.getOptionValue("d"); } if (commandLine.hasOption("e")) { extsHome = commandLine.getOptionValue("e"); } String configType = "typical"; if (commandLine.hasOption("t")) { configType = commandLine.getOptionValue("t"); } long seedTime = 0; if (commandLine.hasOption("s")) { seedTime = Long.parseLong(commandLine.getOptionValue("s")); } File coreHomeFile = new File(coreHome); if (!coreHomeFile.isDirectory()) { System.err.println("Bad core home: " + coreHome); System.exit(1); } File appsHomeFile = new File(appsHome); if (!appsHomeFile.isDirectory()) { System.err.println("Bad applications home: " + appsHome); System.exit(1); } if (instanceId > -1) { System.setProperty("instanceId", "" + instanceId); } else { System.setProperty("instanceId", "" + S4Util.getPID()); } List loArgs = commandLine.getArgList(); if (loArgs.size() < 1) { // System.err.println("No bean configuration file specified"); // System.exit(1); } // String s4ConfigXml = (String) loArgs.get(0); // System.out.println("s4ConfigXml is " + s4ConfigXml); ClassPathResource propResource = new ClassPathResource("s4-core.properties"); Properties prop = new Properties(); if (propResource.exists()) { prop.load(propResource.getInputStream()); } else { System.err.println("Unable to find s4-core.properties. It must be available in classpath"); System.exit(1); } ApplicationContext coreContext = null; String configBase = coreHome + File.separatorChar + "conf" + File.separatorChar + configType; String configPath = ""; List<String> coreConfigUrls = new ArrayList<String>(); File configFile = null; // load clock configuration configPath = configBase + File.separatorChar + clockType + "-clock.xml"; coreConfigUrls.add(configPath); // load core config xml configPath = configBase + File.separatorChar + "s4-core-conf.xml"; configFile = new File(configPath); if (!configFile.exists()) { System.err.printf("S4 core config file %s does not exist\n", configPath); System.exit(1); } coreConfigUrls.add(configPath); String[] coreConfigFiles = new String[coreConfigUrls.size()]; coreConfigUrls.toArray(coreConfigFiles); String[] coreConfigFileUrls = new String[coreConfigFiles.length]; for (int i = 0; i < coreConfigFiles.length; i++) { coreConfigFileUrls[i] = "file:" + coreConfigFiles[i]; } coreContext = new FileSystemXmlApplicationContext(coreConfigFileUrls, coreContext); ApplicationContext context = coreContext; Clock clock = (Clock) context.getBean("clock"); if (clock instanceof EventClock && seedTime > 0) { EventClock s4EventClock = (EventClock) clock; s4EventClock.updateTime(seedTime); System.out.println("Intializing event clock time with seed time " + s4EventClock.getCurrentTime()); } PEContainer peContainer = (PEContainer) context.getBean("peContainer"); Watcher w = (Watcher) context.getBean("watcher"); w.setConfigFilename(configPath); // load extension modules String[] configFileNames = getModuleConfigFiles(extsHome, prop); if (configFileNames.length > 0) { String[] configFileUrls = new String[configFileNames.length]; for (int i = 0; i < configFileNames.length; i++) { configFileUrls[i] = "file:" + configFileNames[i]; } context = new FileSystemXmlApplicationContext(configFileUrls, context); } // load application modules configFileNames = getModuleConfigFiles(appsHome, prop); if (configFileNames.length > 0) { String[] configFileUrls = new String[configFileNames.length]; for (int i = 0; i < configFileNames.length; i++) { configFileUrls[i] = "file:" + configFileNames[i]; } context = new FileSystemXmlApplicationContext(configFileUrls, context); // attach any beans that implement ProcessingElement to the PE // Container String[] processingElementBeanNames = context.getBeanNamesForType(AbstractPE.class); for (String processingElementBeanName : processingElementBeanNames) { AbstractPE bean = (AbstractPE) context.getBean(processingElementBeanName); bean.setClock(clock); try { bean.setSafeKeeper((SafeKeeper) context.getBean("safeKeeper")); } catch (NoSuchBeanDefinitionException ignored) { // no safe keeper = no checkpointing / recovery } // if the application did not specify an id, use the Spring bean name if (bean.getId() == null) { bean.setId(processingElementBeanName); } System.out.println("Adding processing element with bean name " + processingElementBeanName + ", id " + ((AbstractPE) bean).getId()); peContainer.addProcessor((AbstractPE) bean); } } }
From source file:org.apache.solr.cloud.ZkCLI.java
/** * Allows you to perform a variety of zookeeper related tasks, such as: * /*w w w . j av a2s. c o m*/ * Bootstrap the current configs for all collections in solr.xml. * * Upload a named config set from a given directory. * * Link a named config set explicity to a collection. * * Clear ZooKeeper info. * * If you also pass a solrPort, it will be used to start an embedded zk useful * for single machine, multi node tests. */ public static void main(String[] args) throws InterruptedException, TimeoutException, IOException, ParserConfigurationException, SAXException, KeeperException { CommandLineParser parser = new PosixParser(); Options options = new Options(); options.addOption(OptionBuilder.hasArg(true) .withDescription("cmd to run: " + BOOTSTRAP + ", " + UPCONFIG + ", " + DOWNCONFIG + ", " + LINKCONFIG + ", " + MAKEPATH + ", " + PUT + ", " + PUT_FILE + "," + GET + "," + GET_FILE + ", " + LIST + ", " + CLEAR + ", " + UPDATEACLS) .create(CMD)); Option zkHostOption = new Option("z", ZKHOST, true, "ZooKeeper host address"); options.addOption(zkHostOption); Option solrHomeOption = new Option("s", SOLRHOME, true, "for " + BOOTSTRAP + ", " + RUNZK + ": solrhome location"); options.addOption(zkHostOption); options.addOption(solrHomeOption); options.addOption("d", CONFDIR, true, "for " + UPCONFIG + ": a directory of configuration files"); options.addOption("n", CONFNAME, true, "for " + UPCONFIG + ", " + LINKCONFIG + ": name of the config set"); options.addOption("c", COLLECTION, true, "for " + LINKCONFIG + ": name of the collection"); options.addOption(EXCLUDE_REGEX_SHORT, EXCLUDE_REGEX, true, "for " + UPCONFIG + ": files matching this regular expression won't be uploaded"); options.addOption("r", RUNZK, true, "run zk internally by passing the solr run port - only for clusters on one machine (tests, dev)"); options.addOption("h", HELP, false, "bring up this help page"); options.addOption(NAME, true, "name of the cluster property to set"); options.addOption(VALUE_LONG, true, "value of the cluster to set"); try { // parse the command line arguments CommandLine line = parser.parse(options, args); if (line.hasOption(HELP) || !line.hasOption(ZKHOST) || !line.hasOption(CMD)) { // automatically generate the help statement HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(ZK_CLI_NAME, options); System.out.println("Examples:"); System.out.println( "zkcli.sh -zkhost localhost:9983 -cmd " + BOOTSTRAP + " -" + SOLRHOME + " /opt/solr"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPCONFIG + " -" + CONFDIR + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + DOWNCONFIG + " -" + CONFDIR + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + LINKCONFIG + " -" + COLLECTION + " collection1" + " -" + CONFNAME + " myconf"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + MAKEPATH + " /apache/solr"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT + " /solr.conf 'conf data'"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT_FILE + " /solr.xml /User/myuser/solr/solr.xml"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET + " /solr.xml"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET_FILE + " /solr.xml solr.xml.file"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLEAR + " /solr"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + LIST); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLUSTERPROP + " -" + NAME + " urlScheme -" + VALUE_LONG + " https"); System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPDATEACLS + " /solr"); return; } // start up a tmp zk server first String zkServerAddress = line.getOptionValue(ZKHOST); String solrHome = line.getOptionValue(SOLRHOME); String solrPort = null; if (line.hasOption(RUNZK)) { if (!line.hasOption(SOLRHOME)) { System.out.println("-" + SOLRHOME + " is required for " + RUNZK); System.exit(1); } solrPort = line.getOptionValue(RUNZK); } SolrZkServer zkServer = null; if (solrPort != null) { zkServer = new SolrZkServer("true", null, solrHome + "/zoo_data", solrHome, Integer.parseInt(solrPort)); zkServer.parseConfig(); zkServer.start(); } SolrZkClient zkClient = null; try { zkClient = new SolrZkClient(zkServerAddress, 30000, 30000, () -> { }); if (line.getOptionValue(CMD).equalsIgnoreCase(BOOTSTRAP)) { if (!line.hasOption(SOLRHOME)) { System.out.println("-" + SOLRHOME + " is required for " + BOOTSTRAP); System.exit(1); } CoreContainer cc = new CoreContainer(solrHome); if (!ZkController.checkChrootPath(zkServerAddress, true)) { System.out.println("A chroot was specified in zkHost but the znode doesn't exist. "); System.exit(1); } ZkController.bootstrapConf(zkClient, cc, solrHome); // No need to close the CoreContainer, as it wasn't started // up in the first place... } else if (line.getOptionValue(CMD).equalsIgnoreCase(UPCONFIG)) { if (!line.hasOption(CONFDIR) || !line.hasOption(CONFNAME)) { System.out.println("-" + CONFDIR + " and -" + CONFNAME + " are required for " + UPCONFIG); System.exit(1); } String confDir = line.getOptionValue(CONFDIR); String confName = line.getOptionValue(CONFNAME); final String excludeExpr = line.getOptionValue(EXCLUDE_REGEX, EXCLUDE_REGEX_DEFAULT); if (!ZkController.checkChrootPath(zkServerAddress, true)) { System.out.println("A chroot was specified in zkHost but the znode doesn't exist. "); System.exit(1); } ZkConfigManager configManager = new ZkConfigManager(zkClient); final Pattern excludePattern = Pattern.compile(excludeExpr); configManager.uploadConfigDir(Paths.get(confDir), confName, excludePattern); } else if (line.getOptionValue(CMD).equalsIgnoreCase(DOWNCONFIG)) { if (!line.hasOption(CONFDIR) || !line.hasOption(CONFNAME)) { System.out.println("-" + CONFDIR + " and -" + CONFNAME + " are required for " + DOWNCONFIG); System.exit(1); } String confDir = line.getOptionValue(CONFDIR); String confName = line.getOptionValue(CONFNAME); ZkConfigManager configManager = new ZkConfigManager(zkClient); configManager.downloadConfigDir(confName, Paths.get(confDir)); } else if (line.getOptionValue(CMD).equalsIgnoreCase(LINKCONFIG)) { if (!line.hasOption(COLLECTION) || !line.hasOption(CONFNAME)) { System.out.println( "-" + COLLECTION + " and -" + CONFNAME + " are required for " + LINKCONFIG); System.exit(1); } String collection = line.getOptionValue(COLLECTION); String confName = line.getOptionValue(CONFNAME); ZkController.linkConfSet(zkClient, collection, confName); } else if (line.getOptionValue(CMD).equalsIgnoreCase(LIST)) { zkClient.printLayoutToStdOut(); } else if (line.getOptionValue(CMD).equalsIgnoreCase(CLEAR)) { List arglist = line.getArgList(); if (arglist.size() != 1) { System.out.println("-" + CLEAR + " requires one arg - the path to clear"); System.exit(1); } zkClient.clean(arglist.get(0).toString()); } else if (line.getOptionValue(CMD).equalsIgnoreCase(MAKEPATH)) { List arglist = line.getArgList(); if (arglist.size() != 1) { System.out.println("-" + MAKEPATH + " requires one arg - the path to make"); System.exit(1); } zkClient.makePath(arglist.get(0).toString(), true); } else if (line.getOptionValue(CMD).equalsIgnoreCase(PUT)) { List arglist = line.getArgList(); if (arglist.size() != 2) { System.out .println("-" + PUT + " requires two args - the path to create and the data string"); System.exit(1); } String path = arglist.get(0).toString(); if (zkClient.exists(path, true)) { zkClient.setData(path, arglist.get(1).toString().getBytes(StandardCharsets.UTF_8), true); } else { zkClient.create(path, arglist.get(1).toString().getBytes(StandardCharsets.UTF_8), CreateMode.PERSISTENT, true); } } else if (line.getOptionValue(CMD).equalsIgnoreCase(PUT_FILE)) { List arglist = line.getArgList(); if (arglist.size() != 2) { System.out.println("-" + PUT_FILE + " requires two args - the path to create in ZK and the path to the local file"); System.exit(1); } String path = arglist.get(0).toString(); InputStream is = new FileInputStream(arglist.get(1).toString()); try { if (zkClient.exists(path, true)) { zkClient.setData(path, IOUtils.toByteArray(is), true); } else { zkClient.create(path, IOUtils.toByteArray(is), CreateMode.PERSISTENT, true); } } finally { IOUtils.closeQuietly(is); } } else if (line.getOptionValue(CMD).equalsIgnoreCase(GET)) { List arglist = line.getArgList(); if (arglist.size() != 1) { System.out.println("-" + GET + " requires one arg - the path to get"); System.exit(1); } byte[] data = zkClient.getData(arglist.get(0).toString(), null, null, true); System.out.println(new String(data, StandardCharsets.UTF_8)); } else if (line.getOptionValue(CMD).equalsIgnoreCase(GET_FILE)) { List arglist = line.getArgList(); if (arglist.size() != 2) { System.out.println( "-" + GET_FILE + "requires two args - the path to get and the file to save it to"); System.exit(1); } byte[] data = zkClient.getData(arglist.get(0).toString(), null, null, true); FileUtils.writeByteArrayToFile(new File(arglist.get(1).toString()), data); } else if (line.getOptionValue(CMD).equals(UPDATEACLS)) { List arglist = line.getArgList(); if (arglist.size() != 1) { System.out.println("-" + UPDATEACLS + " requires one arg - the path to update"); System.exit(1); } zkClient.updateACLs(arglist.get(0).toString()); } else if (line.getOptionValue(CMD).equalsIgnoreCase(CLUSTERPROP)) { if (!line.hasOption(NAME)) { System.out.println("-" + NAME + " is required for " + CLUSTERPROP); } String propertyName = line.getOptionValue(NAME); //If -val option is missing, we will use the null value. This is required to maintain //compatibility with Collections API. String propertyValue = line.getOptionValue(VALUE_LONG); ClusterProperties props = new ClusterProperties(zkClient); try { props.setClusterProperty(propertyName, propertyValue); } catch (IOException ex) { System.out.println("Unable to set the cluster property due to following error : " + ex.getLocalizedMessage()); System.exit(1); } } else { // If not cmd matches System.out.println("Unknown command " + line.getOptionValue(CMD) + ". Use -h to get help."); System.exit(1); } } finally { if (solrPort != null) { zkServer.stop(); } if (zkClient != null) { zkClient.close(); } } } catch (ParseException exp) { System.out.println("Unexpected exception:" + exp.getMessage()); } }
From source file:org.apache.solr.util.SolrCLI.java
public static CommandLine parseCmdLine(String[] args, Option[] toolOptions) throws Exception { String builderClassName = System.getProperty("solr.authentication.httpclient.builder"); if (builderClassName != null) { try {/*from w w w .j av a 2s. co m*/ Class c = Class.forName(builderClassName); SolrHttpClientBuilder builder = (SolrHttpClientBuilder) c.newInstance(); HttpClientUtil.setHttpClientBuilder(builder); log.info("Set SolrHttpClientBuilder from: " + builderClassName); } catch (Exception ex) { log.error(ex.getMessage()); throw new RuntimeException("Error during loading of builder '" + builderClassName + "'.", ex); } } // the parser doesn't like -D props List<String> toolArgList = new ArrayList<String>(); List<String> dashDList = new ArrayList<String>(); for (int a = 1; a < args.length; a++) { String arg = args[a]; if (arg.startsWith("-D")) { dashDList.add(arg); } else { toolArgList.add(arg); } } String[] toolArgs = toolArgList.toArray(new String[0]); // process command-line args to configure this application CommandLine cli = processCommandLineArgs(joinCommonAndToolOptions(toolOptions), toolArgs); List argList = cli.getArgList(); argList.addAll(dashDList); // for SSL support, try to accommodate relative paths set for SSL store props String solrInstallDir = System.getProperty("solr.install.dir"); if (solrInstallDir != null) { checkSslStoreSysProp(solrInstallDir, "keyStore"); checkSslStoreSysProp(solrInstallDir, "trustStore"); } return cli; }
From source file:org.apache.sqoop.shell.CloneJobFunction.java
@SuppressWarnings("unchecked") public Object executeFunction(CommandLine line, boolean isInteractive) throws IOException { return cloneJob(getLong(line, Constants.OPT_JID), line.getArgList(), isInteractive); }
From source file:org.apache.sqoop.shell.CloneLinkFunction.java
@Override @SuppressWarnings("unchecked") public Object executeFunction(CommandLine line, boolean isInteractive) throws IOException { return cloneLink(getLong(line, Constants.OPT_LID), line.getArgList(), isInteractive); }
From source file:org.apache.sqoop.shell.CreateJobFunction.java
@Override @SuppressWarnings("unchecked") public Object executeFunction(CommandLine line, boolean isInteractive) throws IOException { return createJob(getLong(line, Constants.OPT_FROM), getLong(line, Constants.OPT_TO), line.getArgList(), isInteractive);/* w w w .ja v a 2 s. com*/ }
From source file:org.apache.sqoop.shell.CreateLinkFunction.java
@Override @SuppressWarnings("unchecked") public Object executeFunction(CommandLine line, boolean isInteractive) throws IOException { return createLink(getLong(line, Constants.OPT_CID), line.getArgList(), isInteractive); }
From source file:org.apache.sqoop.shell.UpdateJobFunction.java
@Override @SuppressWarnings("unchecked") public Object executeFunction(CommandLine line, boolean isInteractive) throws IOException { return updateJob(getLong(line, Constants.OPT_JID), line.getArgList(), isInteractive); }
From source file:org.apache.sqoop.shell.UpdateLinkFunction.java
@Override @SuppressWarnings("unchecked") public Object executeFunction(CommandLine line, boolean isInteractive) throws IOException { return updateLink(getLong(line, Constants.OPT_LID), line.getArgList(), isInteractive); }
From source file:org.apache.storm.flux.Flux.java
private static void runCli(CommandLine cmd) throws Exception { if (!cmd.hasOption(OPTION_NO_SPLASH)) { printSplash();/*from w w w. ja v a 2 s . c o m*/ } boolean dumpYaml = cmd.hasOption("dump-yaml"); TopologyDef topologyDef = null; String filePath = (String) cmd.getArgList().get(0); // TODO conditionally load properties from a file our resource String filterProps = null; if (cmd.hasOption(OPTION_FILTER)) { filterProps = cmd.getOptionValue(OPTION_FILTER); } boolean envFilter = cmd.hasOption(OPTION_ENV_FILTER); if (cmd.hasOption(OPTION_RESOURCE)) { printf("Parsing classpath resource: %s", filePath); topologyDef = FluxParser.parseResource(filePath, dumpYaml, true, filterProps, envFilter); } else { printf("Parsing file: %s", new File(filePath).getAbsolutePath()); topologyDef = FluxParser.parseFile(filePath, dumpYaml, true, filterProps, envFilter); } String topologyName = topologyDef.getName(); // merge contents of `config` into topology config Config conf = FluxBuilder.buildConfig(topologyDef); ExecutionContext context = new ExecutionContext(topologyDef, conf); StormTopology topology = FluxBuilder.buildTopology(context); if (!cmd.hasOption(OPTION_NO_DETAIL)) { printTopologyInfo(context); } if (!cmd.hasOption(OPTION_DRY_RUN)) { if (cmd.hasOption(OPTION_REMOTE)) { LOG.info("Running remotely..."); try { // should the topology be active or inactive SubmitOptions submitOptions = null; if (cmd.hasOption(OPTION_INACTIVE)) { LOG.info("Deploying topology in an INACTIVE state..."); submitOptions = new SubmitOptions(TopologyInitialStatus.INACTIVE); } else { LOG.info("Deploying topology in an ACTIVE state..."); submitOptions = new SubmitOptions(TopologyInitialStatus.ACTIVE); } StormSubmitter.submitTopology(topologyName, conf, topology, submitOptions, null); } catch (Exception e) { LOG.warn("Unable to deploy topology to remote cluster.", e); } } else { LOG.info("Running in local mode..."); String sleepStr = cmd.getOptionValue(OPTION_SLEEP); Long sleepTime = DEFAULT_LOCAL_SLEEP_TIME; if (sleepStr != null) { sleepTime = Long.parseLong(sleepStr); } LOG.debug("Sleep time: {}", sleepTime); LocalCluster cluster = null; // in-process or external zookeeper if (cmd.hasOption(OPTION_ZOOKEEPER)) { String zkStr = cmd.getOptionValue(OPTION_ZOOKEEPER); LOG.info("Using ZooKeeper at '{}' instead of in-process one.", zkStr); long zkPort = DEFAULT_ZK_PORT; String zkHost = null; if (zkStr.contains(":")) { String[] hostPort = zkStr.split(":"); zkHost = hostPort[0]; zkPort = hostPort.length > 1 ? Long.parseLong(hostPort[1]) : DEFAULT_ZK_PORT; } else { zkHost = zkStr; } // the following constructor is only available in 0.9.3 and later try { cluster = new LocalCluster(zkHost, zkPort); } catch (NoSuchMethodError e) { LOG.error("The --zookeeper option can only be used with Apache Storm 0.9.3 and later."); System.exit(1); } } else { cluster = new LocalCluster(); } cluster.submitTopology(topologyName, conf, topology); Utils.sleep(sleepTime); cluster.killTopology(topologyName); cluster.shutdown(); } } }