List of usage examples for org.apache.commons.cli HelpFormatter printHelp
public void printHelp(String cmdLineSyntax, Options options)
options
with the specified command line syntax. From source file:net.mybox.mybox.Server.java
/** * Handle the command line args and instantiate the Server * @param args/* w ww . j a va 2 s .c o m*/ */ public static void main(String args[]) { Options options = new Options(); options.addOption("c", "config", true, "configuration file"); // options.addOption("d", "database", true, "accounts database file"); // TODO: handle in config? options.addOption("a", "apphome", true, "application home directory"); options.addOption("h", "help", false, "show help screen"); options.addOption("V", "version", false, "print the Mybox version"); CommandLineParser line = new GnuParser(); CommandLine cmd = null; try { cmd = line.parse(options, args); } catch (ParseException exp) { System.err.println(exp.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(Server.class.getName(), options); return; } if (cmd.hasOption("h")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(Server.class.getName(), options); return; } if (cmd.hasOption("V")) { Client.printMessage("version " + Common.appVersion); return; } if (cmd.hasOption("a")) { String appHomeDir = cmd.getOptionValue("a"); try { Common.updatePaths(appHomeDir); } catch (FileNotFoundException e) { printErrorExit(e.getMessage()); } updatePaths(); } String configFile = defaultConfigFile; // String accountsDBfile = defaultAccountsDbFile; if (cmd.hasOption("c")) { configFile = cmd.getOptionValue("c"); } File fileCheck = new File(configFile); if (!fileCheck.isFile()) Server.printErrorExit("Config not found: " + configFile + "\nPlease run ServerSetup"); // if (cmd.hasOption("d")){ // accountsDBfile = cmd.getOptionValue("d"); // } // // fileCheck = new File(accountsDBfile); // if (!fileCheck.isFile()) // Server.printErrorExit("Error: account database not found " + accountsDBfile); Server server = new Server(configFile); }
From source file:com.consol.citrus.Citrus.java
/** * Main CLI method.//from w w w . j a va2 s . c o m * @param args */ public static void main(String[] args) { Options options = new CitrusCliOptions(); HelpFormatter formatter = new HelpFormatter(); try { CommandLine cmd = new GnuParser().parse(options, args); if (cmd.hasOption("help")) { formatter.printHelp("CITRUS TestFramework", options); return; } Citrus citrus = new Citrus(cmd); citrus.run(); } catch (ParseException e) { log.error("Failed to parse command line arguments", e); formatter.printHelp("CITRUS TestFramework", options); } }
From source file:com.thimbleware.jmemcached.Main.java
public static void main(String[] args) throws Exception { // look for external log4j.properties // setup command line options Options options = new Options(); options.addOption("h", "help", false, "print this help screen"); options.addOption("bl", "block-store", false, "use external (from JVM) heap"); options.addOption("f", "mapped-file", false, "use external (from JVM) heap through a memory mapped file"); options.addOption("bs", "block-size", true, "block size (in bytes) for external memory mapped file allocator. default is 8 bytes"); options.addOption("i", "idle", true, "disconnect after idle <x> seconds"); options.addOption("p", "port", true, "port to listen on"); options.addOption("m", "memory", true, "max memory to use; in bytes, specify K, kb, M, GB for larger units"); options.addOption("c", "ceiling", true, "ceiling memory to use; in bytes, specify K, kb, M, GB for larger units"); options.addOption("l", "listen", true, "Address to listen on"); options.addOption("s", "size", true, "max items"); options.addOption("b", "binary", false, "binary protocol mode"); options.addOption("V", false, "Show version number"); options.addOption("v", false, "verbose (show commands)"); // read command line options CommandLineParser parser = new PosixParser(); CommandLine cmdline = parser.parse(options, args); if (cmdline.hasOption("help") || cmdline.hasOption("h")) { System.out.println("Memcached Version " + MemCacheDaemon.memcachedVersion); System.out.println("http://thimbleware.com/projects/memcached\n"); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar memcached.jar", options); return;/*from w w w. j a v a 2 s. c om*/ } if (cmdline.hasOption("V")) { System.out.println("Memcached Version " + MemCacheDaemon.memcachedVersion); return; } int port = 11211; if (cmdline.hasOption("p")) { port = Integer.parseInt(cmdline.getOptionValue("p")); } else if (cmdline.hasOption("port")) { port = Integer.parseInt(cmdline.getOptionValue("port")); } InetSocketAddress addr = new InetSocketAddress(port); if (cmdline.hasOption("l")) { addr = new InetSocketAddress(cmdline.getOptionValue("l"), port); } else if (cmdline.hasOption("listen")) { addr = new InetSocketAddress(cmdline.getOptionValue("listen"), port); } int max_size = 1000000; if (cmdline.hasOption("s")) max_size = (int) Bytes.valueOf(cmdline.getOptionValue("s")).bytes(); else if (cmdline.hasOption("size")) max_size = (int) Bytes.valueOf(cmdline.getOptionValue("size")).bytes(); System.out.println("Setting max cache elements to " + String.valueOf(max_size)); int idle = -1; if (cmdline.hasOption("i")) { idle = Integer.parseInt(cmdline.getOptionValue("i")); } else if (cmdline.hasOption("idle")) { idle = Integer.parseInt(cmdline.getOptionValue("idle")); } boolean memoryMapped = false; if (cmdline.hasOption("f")) { memoryMapped = true; } else if (cmdline.hasOption("mapped-file")) { memoryMapped = true; } boolean blockStore = false; if (cmdline.hasOption("bl")) { blockStore = true; } else if (cmdline.hasOption("block-store")) { blockStore = true; } boolean verbose = false; if (cmdline.hasOption("v")) { verbose = true; } long ceiling; if (cmdline.hasOption("c")) { ceiling = Bytes.valueOf(cmdline.getOptionValue("c")).bytes(); System.out.println("Setting ceiling memory size to " + Bytes.bytes(ceiling).megabytes() + "M"); } else if (cmdline.hasOption("ceiling")) { ceiling = Bytes.valueOf(cmdline.getOptionValue("ceiling")).bytes(); System.out.println("Setting ceiling memory size to " + Bytes.bytes(ceiling).megabytes() + "M"); } else if (!memoryMapped) { ceiling = 1024000; System.out.println( "Setting ceiling memory size to default limit of " + Bytes.bytes(ceiling).megabytes() + "M"); } else { System.out .println("ERROR : ceiling memory size mandatory when external memory mapped file is specified"); return; } boolean binary = false; if (cmdline.hasOption("b")) { binary = true; } int blockSize = 8; if (!memoryMapped && (cmdline.hasOption("bs") || cmdline.hasOption("block-size"))) { System.out.println( "WARN : block size option is only valid for memory mapped external heap storage; ignoring"); } else if (cmdline.hasOption("bs")) { blockSize = Integer.parseInt(cmdline.getOptionValue("bs")); } else if (cmdline.hasOption("block-size")) { blockSize = Integer.parseInt(cmdline.getOptionValue("block-size")); } long maxBytes; if (cmdline.hasOption("m")) { maxBytes = Bytes.valueOf(cmdline.getOptionValue("m")).bytes(); System.out.println("Setting max memory size to " + Bytes.bytes(maxBytes).gigabytes() + "GB"); } else if (cmdline.hasOption("memory")) { maxBytes = Bytes.valueOf(cmdline.getOptionValue("memory")).bytes(); System.out.println("Setting max memory size to " + Bytes.bytes(maxBytes).gigabytes() + "GB"); } else if (!memoryMapped) { maxBytes = Runtime.getRuntime().maxMemory(); System.out .println("Setting max memory size to JVM limit of " + Bytes.bytes(maxBytes).gigabytes() + "GB"); } else { System.out.println( "ERROR : max memory size and ceiling size are mandatory when external memory mapped file is specified"); return; } if (!memoryMapped && !blockStore && maxBytes > Runtime.getRuntime().maxMemory()) { System.out.println("ERROR : JVM heap size is not big enough. use '-Xmx" + String.valueOf(maxBytes / 1024000) + "m' java argument before the '-jar' option."); return; } else if ((memoryMapped || !blockStore) && maxBytes > Integer.MAX_VALUE) { System.out.println( "ERROR : when external memory mapped, memory size may not exceed the size of Integer.MAX_VALUE (" + Bytes.bytes(Integer.MAX_VALUE).gigabytes() + "GB"); return; } // create daemon and start it final MemCacheDaemon<LocalCacheElement> daemon = new MemCacheDaemon<LocalCacheElement>(); CacheStorage<Key, LocalCacheElement> storage; if (blockStore) { BlockStoreFactory blockStoreFactory = ByteBufferBlockStore.getFactory(); storage = new BlockStorageCacheStorage(8, (int) ceiling, blockSize, maxBytes, max_size, blockStoreFactory); } else if (memoryMapped) { BlockStoreFactory blockStoreFactory = MemoryMappedBlockStore.getFactory(); storage = new BlockStorageCacheStorage(8, (int) ceiling, blockSize, maxBytes, max_size, blockStoreFactory); } else { storage = ConcurrentLinkedHashMap.create(ConcurrentLinkedHashMap.EvictionPolicy.FIFO, max_size, maxBytes); } daemon.setCache(new CacheImpl(storage)); daemon.setBinary(binary); daemon.setAddr(addr); daemon.setIdleTime(idle); daemon.setVerbose(verbose); daemon.start(); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { if (daemon.isRunning()) daemon.stop(); } })); }
From source file:com.github.megatronking.svg.cli.Main.java
public static void main(String[] args) { Options opt = new Options(); opt.addOption("d", "dir", true, "the target svg directory"); opt.addOption("f", "file", true, "the target svg file"); opt.addOption("o", "output", true, "the output vector file or directory"); opt.addOption("w", "width", true, "the width size of target vector image"); opt.addOption("h", "height", true, "the height size of target vector image"); HelpFormatter formatter = new HelpFormatter(); CommandLineParser parser = new PosixParser(); CommandLine cl;//from w w w .j ava 2 s.c o m try { cl = parser.parse(opt, args); } catch (ParseException e) { formatter.printHelp(HELPER_INFO, opt); return; } if (cl == null) { formatter.printHelp(HELPER_INFO, opt); return; } int width = 0; int height = 0; if (cl.hasOption("w")) { width = SCU.parseInt(cl.getOptionValue("w")); } if (cl.hasOption("h")) { height = SCU.parseInt(cl.getOptionValue("h")); } String dir = null; String file = null; if (cl.hasOption("d")) { dir = cl.getOptionValue("d"); } else if (cl.hasOption("f")) { file = cl.getOptionValue("f"); } String output = null; if (cl.hasOption("o")) { output = cl.getOptionValue("o"); } if (output == null) { if (dir != null) { output = dir; } if (file != null) { output = FileUtils.noExtensionName(file) + ".xml"; } } if (dir == null && file == null) { formatter.printHelp(HELPER_INFO, opt); throw new RuntimeException("You must input the target svg file or directory"); } if (dir != null) { File inputDir = new File(dir); if (!inputDir.exists() || !inputDir.isDirectory()) { throw new RuntimeException("The path [" + dir + "] is not exist or valid directory"); } File outputDir = new File(output); if (outputDir.exists() || outputDir.mkdirs()) { svg2vectorForDirectory(inputDir, outputDir, width, height); } else { throw new RuntimeException("The path [" + outputDir + "] is not a valid directory"); } } if (file != null) { File inputFile = new File(file); if (!inputFile.exists() || !inputFile.isFile()) { throw new RuntimeException("The path [" + file + "] is not exist or valid file"); } svg2vectorForFile(inputFile, new File(output), width, height); } }
From source file:com.c4om.jschematronvalidator.JSchematronValidatorMain.java
/** * Main method, executed at application startup * @param args CLI arguments (for more details, just run the program with the --help option). * @throws Exception if any exception is thrown anywhere *//*from w w w .j a v a2s . c o m*/ public static void main(String[] args) throws Exception { LOGGER.info("Program starts"); CommandLine cmdLine; try { // create the command line parser CommandLineParser parser = new BasicParser(); // parse the command line arguments cmdLine = parser.parse(CMD_LINE_OPTIONS, args); } catch (ParseException e) { String fatalMessage = "Error at parsing command line: " + e.getMessage(); LOGGER.fatal(fatalMessage); System.err.println(fatalMessage); System.exit(1); return; //System.exit() makes this statement unreachable. However, 'return' is necessary to prevent the compiler from crying when I use 'cmdLine' outside the try-catch. } if (cmdLine.hasOption(CMD_LINE_OPTION_HELP)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(CMD_LINE_SYNTAX, CMD_LINE_OPTIONS); System.exit(0); } if (!cmdLine.hasOption(CMD_LINE_OPTION_SCHEMATRON_FILE)) { System.err.println("Error at parsing command line: No input schematron is provided."); System.exit(1); } Document inputSchematronDocument = loadW3CDocumentFromInputFile( new File(cmdLine.getOptionValue(CMD_LINE_OPTION_SCHEMATRON_FILE))); Document candidateDocument; if (cmdLine.hasOption(CMD_LINE_OPTION_INPUT)) { try (InputStream candidateInputStream = new FileInputStream( new File(cmdLine.getOptionValue(CMD_LINE_OPTION_INPUT)))) { candidateDocument = loadW3CDocumentFromInputStream(candidateInputStream); } } else { candidateDocument = loadW3CDocumentFromInputStream(System.in); } String phase = cmdLine.getOptionValue(CMD_LINE_OPTION_PHASE); boolean allowForeign = cmdLine.hasOption(CMD_LINE_OPTION_ALLOW_FOREIGN); boolean diagnose = !cmdLine.hasOption(CMD_LINE_OPTION_SKIP_DIAGNOSE); boolean generatePaths = !cmdLine.hasOption(CMD_LINE_OPTION_SKIP_GENERATE_PATHS); boolean generateFiredRule = !cmdLine.hasOption(CMD_LINE_OPTION_SKIP_GENERATE_FIRED_RULE); boolean ignoreFileNotAvailableAtDocumentFunction = cmdLine .hasOption(CMD_LINE_OPTION_SKIP_OPTIONAL_FILES_ERRORS); ValidationMethod currentSchematronValidationMethod = new SaxonXSLT2BasedValidationMethod( ignoreFileNotAvailableAtDocumentFunction, allowForeign, diagnose, generatePaths, generateFiredRule); Document svrlResult = currentSchematronValidationMethod.performValidation(candidateDocument, inputSchematronDocument, phase); if (cmdLine.hasOption(CMD_LINE_OPTION_OUTPUT)) { OutputStream outputStream = new FileOutputStream( new File(cmdLine.getOptionValue(CMD_LINE_OPTION_OUTPUT))); printW3CDocumentToOutputStream(svrlResult, outputStream); } else { printW3CDocumentToOutputStream(svrlResult, System.out); } }
From source file:com.linkedin.pinot.transport.perf.ScatterGatherPerfTester.java
public static void main(String[] args) throws Exception { CommandLineParser cliParser = new GnuParser(); Options cliOptions = buildCommandLineOptions(); CommandLine cmd = cliParser.parse(cliOptions, args, true); if (!cmd.hasOption(EXECUTION_MODE)) { System.out.println("Missing required argument (" + EXECUTION_MODE + ")"); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("", cliOptions); System.exit(-1);/*from w w w . j a va 2 s .c o m*/ } ExecutionMode mode = ExecutionMode.valueOf(cmd.getOptionValue(EXECUTION_MODE)); int numClients = 1; int numServers = 1; int requestSize = 1000; int responseSize = 100000; int numRequests = 10000; int startPortNum = 9078; boolean isAsyncRequest = true; List<String> servers = new ArrayList<String>(); int numActiveConnectionsPerPeer = 10; int numResponseReaders = 3; long serverInducedLatency = 10; if (mode == ExecutionMode.RUN_CLIENT) { if (!cmd.hasOption(SERVER_HOSTS)) { System.out.println("Missing required argument (" + SERVER_HOSTS + ")"); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("", cliOptions); System.exit(-1); } } if (cmd.hasOption(NUM_CLIENTS)) { numClients = Integer.parseInt(cmd.getOptionValue(NUM_CLIENTS)); } if (cmd.hasOption(NUM_SERVERS)) { numServers = Integer.parseInt(cmd.getOptionValue(NUM_SERVERS)); } if (cmd.hasOption(REQUEST_SIZE)) { requestSize = Integer.parseInt(cmd.getOptionValue(REQUEST_SIZE)); } if (cmd.hasOption(RESPONSE_SIZE)) { responseSize = Integer.parseInt(cmd.getOptionValue(RESPONSE_SIZE)); } if (cmd.hasOption(NUM_REQUESTS)) { numRequests = Integer.parseInt(cmd.getOptionValue(NUM_REQUESTS)); } if (cmd.hasOption(SERVER_START_PORT)) { startPortNum = Integer.parseInt(cmd.getOptionValue(SERVER_START_PORT)); } if (cmd.hasOption(SYNC_REQUEST_DISPATCH)) { isAsyncRequest = false; } if (cmd.hasOption(CONN_POOL_SIZE_PER_PEER)) { numActiveConnectionsPerPeer = Integer.parseInt(cmd.getOptionValue(CONN_POOL_SIZE_PER_PEER)); } if (cmd.hasOption(NUM_RESPONSE_READERS)) { numResponseReaders = Integer.parseInt(cmd.getOptionValue(NUM_RESPONSE_READERS)); } if (cmd.hasOption(RESPONSE_LATENCY)) { serverInducedLatency = Integer.parseInt(cmd.getOptionValue(RESPONSE_LATENCY)); } if (cmd.hasOption(SERVER_HOSTS)) { servers = Arrays.asList(cmd.getOptionValue(SERVER_HOSTS).split(",")); } ScatterGatherPerfTester tester = new ScatterGatherPerfTester(numClients, // num Client Threads numServers, // Num Servers requestSize, // Request Size responseSize, // Response Size numRequests, // Num Requests startPortNum, // Server start port isAsyncRequest, // Async Request sending ExecutionMode.RUN_CLIENT, // Execution mode servers, // Server Hosts. All servers need to run on the same port numActiveConnectionsPerPeer, // Number of Active Client connections per Client-Server pair numResponseReaders, // Number of Response Reader threads in client serverInducedLatency); // 10 ms latency at server tester.run(); }
From source file:net.ladenthin.snowman.imager.run.CLI.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { try {/*from w w w . j a va2s .c o m*/ CommandLineParser parser = new PosixParser(); Options options = new Options(); options.addOption(cmdHelpS, cmdHelp, false, cmdHelpD); options.addOption(cmdVersionS, cmdVersion, false, cmdVersionD); options.addOption(OptionBuilder.withDescription(cmdConfigurationD).withLongOpt(cmdConfiguration) .hasArg().withArgName(cmdConfigurationA).create(cmdConfigurationS)); // parse the command line arguments CommandLine line = parser.parse(options, args); final String cmdLineSyntax = "java -jar " + Imager.jarFilename; // automatically generate the help statement HelpFormatter formatter = new HelpFormatter(); final String configurationPath; if (line.hasOption(cmdConfiguration)) { configurationPath = line.getOptionValue(cmdConfiguration); } else { System.out.println("Need configuration value."); formatter.printHelp(cmdLineSyntax, options); return; } // check parameter if (args.length == 0 || line.hasOption(cmdHelp)) { formatter.printHelp(cmdLineSyntax, options); return; } if (line.hasOption(cmdVersion)) { System.out.println(Imager.version); return; } Imager imager = new Imager(configurationPath); imager.waitForAllThreads(); imager.restartAndExit(); } catch (IllegalArgumentException | ParseException | IOException | InstantiationException | InterruptedException e) { LOGGER.error("Critical exception.", e); System.exit(-1); } }
From source file:cc.twittertools.stream.GatherStatusStream.java
@SuppressWarnings("static-access") public static void main(String[] args) throws TwitterException { Options options = new Options(); options.addOption(OptionBuilder.withArgName("list").hasArgs() .withDescription("comma-separated list of BCP 47 language identifiers").withLongOpt(LANGUAGE_OPTION) .create('l')); options.addOption(OptionBuilder.withArgName("list").hasArgs() .withDescription(//from w w w . java 2 s . co m "comma-separated list of longitude,latitude pairs specifying a set of bounding boxes") .withLongOpt(LOCATIONS_OPTION).create('g')); options.addOption("n", NO_BOUNDING_BOX_OPTION, false, "do not consider places' bounding box"); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(RunQueriesThrift.class.getName(), options); System.exit(-1); } PatternLayout layoutStandard = new PatternLayout(); layoutStandard.setConversionPattern("[%p] %d %c %M - %m%n"); PatternLayout layoutSimple = new PatternLayout(); layoutSimple.setConversionPattern("%m%n"); // Filter for the statuses: we only want INFO messages LevelRangeFilter filter = new LevelRangeFilter(); filter.setLevelMax(Level.INFO); filter.setLevelMin(Level.INFO); filter.setAcceptOnMatch(true); filter.activateOptions(); TimeBasedRollingPolicy statusesRollingPolicy = new TimeBasedRollingPolicy(); statusesRollingPolicy.setFileNamePattern("statuses.log" + HOUR_ROLL); statusesRollingPolicy.activateOptions(); RollingFileAppender statusesAppender = new RollingFileAppender(); statusesAppender.setRollingPolicy(statusesRollingPolicy); statusesAppender.addFilter(filter); statusesAppender.setLayout(layoutSimple); statusesAppender.activateOptions(); TimeBasedRollingPolicy warningsRollingPolicy = new TimeBasedRollingPolicy(); warningsRollingPolicy.setFileNamePattern("warnings.log" + HOUR_ROLL); warningsRollingPolicy.activateOptions(); RollingFileAppender warningsAppender = new RollingFileAppender(); warningsAppender.setRollingPolicy(statusesRollingPolicy); warningsAppender.setThreshold(Level.WARN); warningsAppender.setLayout(layoutStandard); warningsAppender.activateOptions(); ConsoleAppender consoleAppender = new ConsoleAppender(); consoleAppender.setThreshold(Level.WARN); consoleAppender.setLayout(layoutStandard); consoleAppender.activateOptions(); // configures the root logger Logger rootLogger = Logger.getRootLogger(); rootLogger.setLevel(Level.INFO); rootLogger.removeAllAppenders(); rootLogger.addAppender(consoleAppender); rootLogger.addAppender(statusesAppender); rootLogger.addAppender(warningsAppender); // creates filters for the query FilterQuery fq = new FilterQuery(); StringBuilder criteria = new StringBuilder(); /* * @see https://dev.twitter.com/docs/streaming-apis/parameters#language */ final boolean filterLanguage = cmdline.hasOption(LANGUAGE_OPTION); String[] languages = null; if (filterLanguage) { languages = cmdline.getOptionValue(LANGUAGE_OPTION).split(","); fq.language(languages); criteria.append("languages: [" + cmdline.getOptionValue(LANGUAGE_OPTION) + "]\t"); } final String[] langs = languages; /* * @see https://dev.twitter.com/docs/streaming-apis/parameters#locations */ double[][] locations = null; if (cmdline.hasOption(LOCATIONS_OPTION)) { String[] locationsArg = cmdline.getOptionValue(LOCATIONS_OPTION).split(","); int nCoords = locationsArg.length; if (nCoords % 2 == 0) { int pairs = nCoords / 2; locations = new double[pairs][2]; int cnt = 0; for (int i = 0; i < pairs; i++) { locations[i][0] = Double.parseDouble(locationsArg[cnt]); cnt++; locations[i][1] = Double.parseDouble(locationsArg[cnt]); cnt++; } fq.locations(locations); criteria.append("locations: [" + cmdline.getOptionValue(LOCATIONS_OPTION) + "]\t"); } else { System.err.println("There is a missing coordinate. See " + "https://dev.twitter.com/docs/streaming-apis/parameters#locations"); System.exit(-1); } } else { fq.locations(new double[][] { { -180, -90 }, { 180, 90 } }); } final double[][] loc = locations; final boolean no_bounding_box = cmdline.hasOption(NO_BOUNDING_BOX_OPTION); if (no_bounding_box) { criteria.append("--no-bounding-box\t"); } // creates a custom logger and log messages final Logger logger = Logger.getLogger(GatherStatusStream.class); logger.info(criteria); RawStreamListener rawListener = new RawStreamListener() { @Override public void onMessage(String rawString) { if (no_bounding_box && loc != null) { try { JSONObject status = new JSONObject(rawString); JSONObject coordObj = status.getJSONObject("coordinates"); JSONArray coords = coordObj.getJSONArray("coordinates"); double longitude = coords.getDouble(0); double latitude = coords.getDouble(1); // checks location for (int i = 0; i < loc.length; i += 2) { if (((loc[i][0] <= longitude) && (longitude <= loc[i + 1][0])) || ((loc[i][1] <= latitude) && (latitude <= loc[i + 1][1]))) { break; } else if (i == loc.length - 1) return; } } catch (JSONException e) { /* Either "Coordinates" is null or trash is coming*/ return; } } if (filterLanguage) { try { JSONObject status = new JSONObject(rawString); // checks language String lang = status.getString("lang"); for (int i = 0; i < langs.length; i++) { if (langs[i].equals(lang)) break; else if (i == langs.length - 1) return; } } catch (JSONException e) { /* Trash is coming */ return; } } cnt++; logger.info(rawString); if (cnt % 1000 == 0) { System.out.println(cnt + " messages received."); } } @Override public void onException(Exception ex) { logger.warn(ex); } }; TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(rawListener); twitterStream.filter(fq); }
From source file:io.anserini.index.UserPostFrequencyDistribution.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(new Option(HELP_OPTION, "show help")); options.addOption(new Option(STORE_TERM_VECTORS_OPTION, "store term vectors")); options.addOption(OptionBuilder.withArgName("collection").hasArg() .withDescription("source collection directory").create(COLLECTION_OPTION)); options.addOption(OptionBuilder.withArgName("property").hasArg() .withDescription("source collection directory").create("property")); options.addOption(OptionBuilder.withArgName("collection_pattern").hasArg() .withDescription("source collection directory").create("collection_pattern")); CommandLine cmdline = null;//from www . j a va 2 s .co m CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (cmdline.hasOption(HELP_OPTION) || !cmdline.hasOption(COLLECTION_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(UserPostFrequencyDistribution.class.getName(), options); System.exit(-1); } String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION); final FieldType textOptions = new FieldType(); textOptions.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); textOptions.setStored(true); textOptions.setTokenized(true); textOptions.setStoreTermVectors(true); LOG.info("collection: " + collectionPath); LOG.info("collection_pattern " + cmdline.getOptionValue("collection_pattern")); LOG.info("property " + cmdline.getOptionValue("property")); LongOpenHashSet deletes = null; long startTime = System.currentTimeMillis(); File file = new File(collectionPath); if (!file.exists()) { System.err.println("Error: " + file + " does not exist!"); System.exit(-1); } final JsonStatusCorpusReader stream = new JsonStatusCorpusReader(file, cmdline.getOptionValue("collection_pattern")); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { stream.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } ; System.out.println("# of users indexed this round: " + userIndexedCount); System.out.println("Shutting down"); } }); Status status; boolean readerNotInitialized = true; try { Properties prop = new Properties(); while ((status = stream.next()) != null) { // try{ // status = DataObjectFactory.createStatus(s); // if (status==null||status.getText() == null) { // continue; // }}catch(Exception e){ // // } // boolean pittsburghRelated = false; try { if (Math.abs(status.getLongitude() - pittsburghLongitude) < 0.05d && Math.abs(status.getlatitude() - pittsburghLatitude) < 0.05d) pittsburghRelated = true; } catch (Exception e) { } try { if (status.getPlace().contains("Pittsburgh, PA")) pittsburghRelated = true; } catch (Exception e) { } try { if (status.getUserLocation().contains("Pittsburgh, PA")) pittsburghRelated = true; } catch (Exception e) { } try { if (status.getText().contains("Pittsburgh")) pittsburghRelated = true; } catch (Exception e) { } if (pittsburghRelated) { int previousPostCount = 0; if (prop.containsKey(String.valueOf(status.getUserid()))) { previousPostCount = Integer .valueOf(prop.getProperty(String.valueOf(status.getUserid())).split(" ")[1]); } prop.setProperty(String.valueOf(status.getUserid()), String.valueOf(status.getStatusesCount()) + " " + (1 + previousPostCount)); if (prop.size() > 0 && prop.size() % 1000 == 0) { Runtime runtime = Runtime.getRuntime(); runtime.gc(); System.out.println("Property size " + prop.size() + "Memory used: " + ((runtime.totalMemory() - runtime.freeMemory()) / (1024L * 1024L)) + " MB\n"); } OutputStream output = new FileOutputStream(cmdline.getOptionValue("property"), false); prop.store(output, null); output.close(); } } // prop.store(output, null); LOG.info(String.format("Total of %s statuses added", userIndexedCount)); LOG.info("Total elapsed time: " + (System.currentTimeMillis() - startTime) + "ms"); } catch (Exception e) { e.printStackTrace(); } finally { stream.close(); } }
From source file:cnxchecker.Client.java
public static void main(String[] args) { Options options = new Options(); options.addOption("H", "host", true, "Remote IP address"); options.addOption("p", "port", true, "Remote TCP port"); options.addOption("s", "size", true, "Packet size in bytes (1 KiB)"); options.addOption("f", "freq", true, "Packets per seconds (1 Hz)"); options.addOption("w", "workers", true, "Number of Workers (1)"); options.addOption("d", "duration", true, "Duration of the test in seconds (60 s)"); options.addOption("h", "help", false, "Print help"); CommandLineParser parser = new GnuParser(); try {//from w w w.j ava2s . c o m CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("h")) { HelpFormatter hf = new HelpFormatter(); hf.printHelp("client", options); System.exit(0); } // host InetAddress ia = null; if (cmd.hasOption("H")) { String host = cmd.getOptionValue("H"); try { ia = InetAddress.getByName(host); } catch (UnknownHostException e) { printAndExit("Unknown host: " + host); } } else { printAndExit("Host option is mandatory"); } // port int port = 0; if (cmd.hasOption("p")) { try { port = Integer.parseInt(cmd.getOptionValue("p")); } catch (NumberFormatException e) { printAndExit("Invalid port number " + cmd.getOptionValue("p")); } if (port < 0 || port > 65535) { printAndExit("Invalid port number " + port); } } // size int size = 1024; if (cmd.hasOption("s")) { try { size = Integer.parseInt(cmd.getOptionValue("s")); } catch (NumberFormatException e) { printAndExit("Invalid packet size " + cmd.getOptionValue("s")); } if (size < 0) { printAndExit("Invalid packet size: " + port); } } // freq double freq = 1; if (cmd.hasOption("f")) { try { freq = Double.parseDouble(cmd.getOptionValue("f")); } catch (NumberFormatException e) { printAndExit("Invalid frequency: " + cmd.getOptionValue("f")); } if (freq <= 0) { printAndExit("Invalid frequency: " + freq); } } // workers int workers = 1; if (cmd.hasOption("w")) { try { workers = Integer.parseInt(cmd.getOptionValue("w")); } catch (NumberFormatException e) { printAndExit("Invalid number of workers: " + cmd.getOptionValue("w")); } if (workers < 0) { printAndExit("Invalid number of workers: " + workers); } } // duration int duration = 60000; if (cmd.hasOption("d")) { try { duration = Integer.parseInt(cmd.getOptionValue("d")) * 1000; } catch (NumberFormatException e) { printAndExit("Invalid duration: " + cmd.getOptionValue("d")); } if (duration < 0) { printAndExit("Invalid duration: " + duration); } } Client client = new Client(ia, port, size, freq, workers, duration); client.doit(); } catch (ParseException e) { printAndExit("Failed to parse options: " + e.getMessage()); } System.exit(0); }