@Test
  public void supportsPosixlyCorrectBehavior() {
    OptionParser parser = new OptionParser("i:j::k");
    String[] arguments = {
      "-ibar", "-i", "junk", "xyz", "-jixnay", "foo", "-k", "blah", "--", "bah"
    };

    OptionSet options = parser.parse(arguments);

    assertTrue(options.has("i"));
    assertTrue(options.has("j"));
    assertTrue(options.has("k"));
    assertEquals(asList("bar", "junk"), options.valuesOf("i"));
    assertEquals(singletonList("ixnay"), options.valuesOf("j"));
    assertEquals(asList("xyz", "foo", "blah", "bah"), options.nonOptionArguments());

    parser.posixlyCorrect(true);
    options = parser.parse(arguments);

    assertTrue(options.has("i"));
    assertFalse(options.has("j"));
    assertFalse(options.has("k"));
    assertEquals(asList("bar", "junk"), options.valuesOf("i"));
    assertEquals(emptyList(), options.valuesOf("j"));
    assertEquals(
        asList("xyz", "-jixnay", "foo", "-k", "blah", "--", "bah"), options.nonOptionArguments());
  }
Esempio n. 2
0
  /**
   * {@inheritDoc}
   *
   * <p>All necessary setup for the module. Populate the "processing" table in seqware_meta_db.
   * Create a temporary directory.
   */
  @Override
  public ReturnValue init() {

    ReturnValue ret = new ReturnValue();
    ret.setExitStatus(ReturnValue.SUCCESS);
    // fill in the [xxx] fields in the processing table
    ret.setAlgorithm("TrimAllReads");
    ret.setDescription("Trim all reads to a specific length.");
    ret.setVersion("0.7.0");

    try {
      OptionParser parser = getOptionParser();
      // The parameters object is actually an ArrayList of Strings created
      // by splitting the command line options by space. JOpt expects a String[]
      options = parser.parse(this.getParameters().toArray(new String[0]));
      // create a temp directory in current working directory
      // tempDir = FileTools.createTempDirectory(new File("."));
      // you can write to "stdout" or "stderr" which will be persisted back to the DB
      ret.setStdout(ret.getStdout() + "Output: " + (String) options.valueOf("outfastq") + "\n");
    } catch (OptionException e) {
      e.printStackTrace();
      ret.setStderr(e.getMessage());
      ret.setExitStatus(ReturnValue.INVALIDPARAMETERS);
    } // catch (IOException e) {
    //  e.printStackTrace();
    //  ret.setStderr(e.getMessage());
    //  ret.setExitStatus(ReturnValue.DIRECTORYNOTWRITABLE);
    // }

    return (ret);
  }
Esempio n. 3
0
  @Test
  public void testGetOptionParser() throws Exception {
    OptionParser optionParser = Main.getOptionParser();

    String tmpDir = systemTempDir().toString();

    OptionSet optionSet =
        optionParser.parse(
            "-f",
            "/tmp/dummy",
            "-s",
            "9908:976098897",
            "-r",
            "9908:810017902",
            "-u",
            "https://ap.unit4.com",
            "-m",
            "as2",
            "-e",
            tmpDir);
    assertTrue(optionSet.has("u"));
    assertTrue(optionSet.has("f"));
    assertTrue(optionSet.has("e"));
    Object e = optionSet.valueOf("e");
    assertNotNull(e);
    assertTrue(e instanceof File);
    File f = (File) e;
    assertEquals(f, new File(tmpDir));
  }
  public static void main(String[] args) throws Exception {

    OptionParser parser = new OptionParser();
    parser
        .accepts("client-zone-id", "client zone id for zone routing")
        .withRequiredArg()
        .describedAs("zone-id")
        .ofType(Integer.class);
    OptionSet options = parser.parse(args);

    List<String> nonOptions = options.nonOptionArguments();
    if (nonOptions.size() < 2 || nonOptions.size() > 3) {
      System.err.println(
          "Usage: java VoldemortClientShell store_name bootstrap_url [command_file] [options]");
      parser.printHelpOn(System.err);
      System.exit(-1);
    }

    String storeName = nonOptions.get(0);
    String bootstrapUrl = nonOptions.get(1);

    String commandsFileName = "";
    BufferedReader fileReader = null;
    BufferedReader inputReader = null;
    try {
      if (nonOptions.size() == 3) {
        commandsFileName = nonOptions.get(2);
        fileReader = new BufferedReader(new FileReader(commandsFileName));
      }

      inputReader = new BufferedReader(new InputStreamReader(System.in));
    } catch (IOException e) {
      Utils.croak("Failure to open input stream: " + e.getMessage());
    }

    ClientConfig clientConfig =
        new ClientConfig()
            .setBootstrapUrls(bootstrapUrl)
            .setRequestFormatType(RequestFormatType.VOLDEMORT_V3);

    if (options.has("client-zone-id")) {
      clientConfig.setClientZoneId((Integer) options.valueOf("client-zone-id"));
    }

    StoreClientFactory factory = new SocketStoreClientFactory(clientConfig);

    try {
      client = (DefaultStoreClient<Object, Object>) factory.getStoreClient(storeName);
    } catch (Exception e) {
      Utils.croak("Could not connect to server: " + e.getMessage());
    }

    System.out.println("Established connection to " + storeName + " via " + bootstrapUrl);
    System.out.print(PROMPT);
    if (fileReader != null) {
      processCommands(factory, fileReader, true);
      fileReader.close();
    }
    processCommands(factory, inputReader, false);
  }
  /**
   * This is the main interface to ConfigurationOptions. Passing in the arguments array will return
   * an object containing all parsed arguments and default values for arguments not specified
   *
   * @param args the arguments array from <code>main(String[] args)</code>.
   * @return A set of configuration options.
   * @throws Exception
   */
  static ConfigurationOptions parse(String[] args) throws Exception {
    ConfigurationOptions cfg = new ConfigurationOptions();

    OptionParser parser = setupParser();
    processOptions(parser.parse(args), cfg);

    return cfg;
  }
Esempio n. 6
0
  @Test
  public void senderAndReceiverIsOptional() throws Exception {
    OptionParser optionParser = Main.getOptionParser();

    OptionSet optionSet =
        optionParser.parse("-f", "/tmp/dummy", "-u", "https://ap.unit4.com", "-m", "as2");
    assertFalse(optionSet.has("-r"));
    assertFalse(optionSet.has("-s"));
  }
Esempio n. 7
0
  public static void parseArgs(String args[]) throws java.io.IOException {

    parser.acceptsAll(asList("h", "?"), "show help then exit");
    OptionSpec<String> CMD = parser.accepts("c", "command").withRequiredArg().ofType(String.class);
    OptionSpec<String> LOGIN =
        parser.accepts("u", "LoginId").withRequiredArg().ofType(String.class);
    OptionSpec<String> PASSWORD =
        parser.accepts("p", "password").withRequiredArg().ofType(String.class);
    OptionSpec<String> ISSUER =
        parser.accepts("i", "IssuerDN").withRequiredArg().ofType(String.class);
    OptionSpec<String> SUBJECT =
        parser.accepts("s", "SubjectDN").withRequiredArg().ofType(String.class);
    OptionSpec<String> CONTEXT =
        parser
            .accepts("C", "context:UNITTEST,DEVELOPMENT,SDK,PRODUCTION")
            .withRequiredArg()
            .ofType(String.class);

    OptionSet options = parser.parse(args);

    // check for help
    if (options.has("?")) {
      parser.printHelpOn(System.out);
      System.exit(0);
    }
    if (options.has(CMD)) {
      cmd = options.valueOf(CMD);
    } else {
      System.out.println("a -c cmd argument is required");
      parser.printHelpOn(System.out);
      System.exit(0);
    }
    if (options.has(LOGIN)) {
      loginName = options.valueOf(LOGIN);
    }
    if (options.has(PASSWORD)) {
      password = options.valueOf(PASSWORD);
    }
    if (options.has(ISSUER)) {
      issuer = options.valueOf(ISSUER);
    }
    if (options.has(SUBJECT)) {
      subject = options.valueOf(SUBJECT);
    }
    if (options.has(CONTEXT)) {
      context = options.valueOf(CONTEXT);
      if (!context.equals("UNITTEST")
          && !context.equals("SDK")
          && !context.equals("DEVELOPMENT")
          && !context.equals("PRODUCTION")) {
        System.out.println("unrecognized CONTEXT value: " + context);
        System.exit(-1);
      }
    }
  }
Esempio n. 8
0
  private static OptionSet parseOptions(String[] argv) throws IOException {
    OptionSet args = null;
    OptionParser parser = new OptionParser();
    parser.acceptsAll(Arrays.asList("l", CMD_LIST), "list readers");
    parser.acceptsAll(Arrays.asList("p", OPT_PROVIDER), "specify provider").withRequiredArg();
    parser.acceptsAll(Arrays.asList("v", OPT_VERBOSE), "be verbose");
    parser.acceptsAll(Arrays.asList("e", OPT_ERROR), "fail if not 0x9000");
    parser.acceptsAll(Arrays.asList("h", OPT_HELP), "show help");
    parser.acceptsAll(Arrays.asList("r", OPT_READER), "use reader").withRequiredArg();
    parser.acceptsAll(Arrays.asList("a", CMD_APDU), "send APDU").withRequiredArg();
    parser.acceptsAll(Arrays.asList("w", OPT_WEB), "open ATR in web");
    parser.acceptsAll(Arrays.asList("V", OPT_VERSION), "show version information");
    parser.accepts(OPT_DUMP, "save dump to file").withRequiredArg().ofType(File.class);
    parser.accepts(OPT_REPLAY, "replay command from dump").withRequiredArg().ofType(File.class);

    parser.accepts(OPT_SUN, "load SunPCSC");
    parser.accepts(OPT_JNA, "load jnasmartcardio");
    parser.accepts(OPT_CONNECT, "connect to host:port or URL").withRequiredArg();
    parser.accepts(OPT_PINNED, "require certificate").withRequiredArg().ofType(File.class);

    parser.accepts(OPT_PROVIDERS, "list providers");
    parser.accepts(OPT_ALL, "process all readers");
    parser.accepts(OPT_WAIT, "wait for card insertion");
    parser.accepts(OPT_T0, "use T=0");
    parser.accepts(OPT_T1, "use T=1");
    parser.accepts(OPT_TEST_SERVER, "run a test server on port 10000").withRequiredArg();
    parser.accepts(OPT_NO_GET_RESPONSE, "don't use GET RESPONSE with SunPCSC");
    parser.accepts(OPT_LIB, "use specific PC/SC lib with SunPCSC").withRequiredArg();
    parser.accepts(OPT_PROVIDER_TYPE, "provider type if not PC/SC").withRequiredArg();

    // Parse arguments
    try {
      args = parser.parse(argv);
      // Try to fetch all values so that format is checked before usage
      for (String s : parser.recognizedOptions().keySet()) {
        args.valuesOf(s);
      }
    } catch (OptionException e) {
      if (e.getCause() != null) {
        System.err.println(e.getMessage() + ": " + e.getCause().getMessage());
      } else {
        System.err.println(e.getMessage());
      }
      System.err.println();
      help_and_exit(parser, System.err);
    }
    if (args.has(OPT_HELP)) {
      help_and_exit(parser, System.out);
    }
    return args;
  }
Esempio n. 9
0
  @SuppressWarnings("unchecked")
  public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser();

    parser.accepts("native", "use native admin client");
    parser.accepts("f", "execute fetch operation");
    parser.accepts("fu", "fetch and update").withRequiredArg().ofType(Integer.class);
    parser
        .accepts("n", "node id")
        .withRequiredArg()
        .ofType(Integer.class)
        .withValuesSeparatedBy(',');
    parser
        .accepts("p", "partition id")
        .withRequiredArg()
        .ofType(Integer.class)
        .withValuesSeparatedBy(',');
    OptionSet options = parser.parse(args);

    List<String> nonOptions = (List<String>) options.nonOptionArguments();

    if (args.length < 2) {
      System.out.println(usageStr);
      return;
    }

    String bootstrapUrl = nonOptions.get(0);
    String storeName = nonOptions.get(1);

    if (!options.has("p") && !options.has("n")) {
      printUsage(
          System.err,
          parser,
          "One or more node and/or one or more partition has" + " to be specified");
    }

    AdminTest adminTest;

    adminTest = new AdminTest(bootstrapUrl, storeName);

    SetMultimap<Integer, Integer> nodePartitions =
        adminTest.getNodePartitions(
            options.has("n") ? options.valuesOf("n") : null,
            options.has("p") ? options.valuesOf("p") : null);

    if (options.has("f")) adminTest.testFetch(nodePartitions);
    if (options.has("fu"))
      adminTest.testFetchAndUpdate(nodePartitions, (Integer) options.valueOf("fu"));
  }
Esempio n. 10
0
  @Test
  public void supportsOptionSynonyms() {
    OptionParser parser = new OptionParser();
    List<String> synonyms = asList("message", "blurb", "greeting");
    parser.acceptsAll(synonyms).withRequiredArg();
    String expectedMessage = "Hello";

    OptionSet options = parser.parse("--message", expectedMessage);

    for (String each : synonyms) {
      assertTrue(each, options.has(each));
      assertTrue(each, options.hasArgument(each));
      assertEquals(each, expectedMessage, options.valueOf(each));
      assertEquals(each, asList(expectedMessage), options.valuesOf(each));
    }
  }
  @Test
  public void allowsDifferentFormsOfPairingArgumentWithOption() {
    OptionParser parser = new OptionParser();
    parser.accepts("count").withRequiredArg();
    parser.accepts("level").withOptionalArg();

    OptionSet options = parser.parse("--count", "4", "--level=3");

    assertTrue(options.has("count"));
    assertTrue(options.hasArgument("count"));
    assertEquals("4", options.valueOf("count"));

    assertTrue(options.has("level"));
    assertTrue(options.hasArgument("level"));
    assertEquals("3", options.valueOf("level"));
  }
Esempio n. 12
0
 private OptionSet parseOptions(OptionParser p, String[] args) throws IOException {
   OptionSet options = null;
   try {
     options = p.parse(args);
     if (options.has("?")
         || !options.has(maxwiOptName)
         || options.nonOptionArguments().size() < 2) {
       System.err.println("Usage:\nSimilarity <input_paths> <output_path> [options]");
       System.err.println("Please specify " + maxwiOptName + " option\n");
       p.printHelpOn(System.err);
       System.exit(0);
     }
   } catch (OptionException e) {
     System.err.println(e.getMessage());
     System.exit(1);
   }
   return options;
 }
Esempio n. 13
0
  public final void parseAndExecute(final String[] cmds) throws IOException {
    opts = parser.parse(cmds);
    if (opts.has(HELP)) {
      parser.printHelpOn(System.out);
      return;
    }

    Indexing.STORE = opts.has(STORE);
    Indexing.COMMIT = (Integer) opts.valueOf(COMMIT);

    // FORMAT
    if (opts.has(FORMAT)) {
      format = (Format) opts.valueOf(FORMAT);
    } else printError(FORMAT);

    // DUMPS_DIR
    if (opts.has(DUMPS_DIR)) {
      dumpsDir = (File) opts.valueOf(DUMPS_DIR);
    } else printError(DUMPS_DIR);

    // INDEX_DIR
    if (opts.has(INDEX_DIR)) {
      indexDir = (File) opts.valueOf(INDEX_DIR);
    } else printError(INDEX_DIR);

    logger.info(
        "Creating index at {} from the files at {}",
        indexDir.getAbsolutePath(),
        dumpsDir.getAbsolutePath());
    final Indexing indexing;
    switch (format) {
      case SINDICE_DE:
        indexing = new SindiceDEIndexing(dumpsDir, FSDirectory.open(indexDir));
        break;
      case SINDICE_ED:
        indexing = new SindiceEDIndexing(dumpsDir, FSDirectory.open(indexDir));
        break;
      default:
        throw new IllegalArgumentException("No such dataset format: " + format);
    }
    indexing.indexIt();
    indexing.close();
    logger.info("Finished indexing");
  }
Esempio n. 14
0
  public static void main(String[] args) throws IOException, TException {
    OptionParser parser = new OptionParser();
    parser.accepts("c", "configuration file").withRequiredArg().ofType(String.class);
    parser.accepts("help", "print help statement");
    OptionSet options = parser.parse(args);

    if (options.has("help")) {
      parser.printHelpOn(System.out);
      System.exit(-1);
    }

    // Logger configuration: log to the console
    BasicConfigurator.configure();
    LOG.setLevel(Level.DEBUG);
    LOG.debug("debug logging on");

    Configuration conf = new PropertiesConfiguration();

    if (options.has("c")) {
      String configFile = (String) options.valueOf("c");
      try {
        conf = new PropertiesConfiguration(configFile);
      } catch (ConfigurationException e) {
      }
    }
    // Start backend server
    BackendService.Processor<BackendService.Iface> processor =
        new BackendService.Processor<BackendService.Iface>(new ProtoBackend());

    int listenPort = conf.getInt("listen_port", DEFAULT_LISTEN_PORT);
    NM_PORT = conf.getInt("node_monitor_port", NodeMonitorThrift.DEFAULT_NM_THRIFT_PORT);
    TServers.launchThreadedThriftServer(listenPort, THRIFT_WORKER_THREADS, processor);

    // Register server
    client = TClients.createBlockingNmClient(NM_HOST, NM_PORT);

    try {
      client.registerBackend(APP_ID, "localhost:" + listenPort);
      LOG.debug("Client successfullly registered");
    } catch (TTransportException e) {
      LOG.debug("Error while registering backend: " + e.getMessage());
    }
  }
Esempio n. 15
0
  public static void main(String[] args) throws Exception {
    SaraTouchClient client = new SaraTouchClient();

    OptionParser parser = new OptionParser("ch:p:t");
    OptionSet options = null;

    try {
      options = parser.parse(args);
    } catch (Exception e) {
      System.out.println("Could not parse command-line options:\n" + e);
      System.out.println();
      System.out.println("-h <host>   Server host");
      System.out.println("-p <port>   Server port");
      System.out.println("-t          Send TUIO events instead of mouse events");
      System.out.println("-c          Connect after starting up");
      System.out.println();
      System.exit(-1);
    }

    if (options.has("h")) {
      Object o = options.valueOf("h");
      if (o != null) {
        String s = (String) o;
        client.setHost(s);
      }
    }

    if (options.has("p")) {
      Object o = options.valueOf("p");
      if (o != null) {
        int i = Integer.parseInt((String) o);
        client.setPort(i);
      }
    }

    if (options.has("t")) client.setModeToTUIO();

    if (options.has("c")) client.connect();

    client.setSize(600, 500);
    client.setVisible(true);
  }
Esempio n. 16
0
  // For passing args in Netbeans 7.3, see http://forums.netbeans.org/ptopic46048.html. Doesn't work
  // as of 17-09-2013 (it is ignored).
  public static void main(String[] args) {
    EmmaBiblioJOB emmaBiblioJob = new EmmaBiblioJOB();
    int retVal = 0;
    OptionParser optionParser = new OptionParser();
    optionParser.accepts("a"); // validate all biblios.
    optionParser.accepts(
        "v"); // validate update candidate biblios (whose 'updated' flag is not 'Y').
    optionParser.accepts("u"); // update candidate biblios that pass validation.

    OptionSet options = optionParser.parse(args);
    if (options.has("a")) retVal = Math.max(retVal, emmaBiblioJob.validateAll());

    if (options.has("v")) retVal = Math.max(retVal, emmaBiblioJob.validateUpdateCandidates());

    if (options.has("u")) retVal = Math.max(retVal, emmaBiblioJob.update());

    if (!((options.has("a")) || (options.has("v")) || (options.has("u")))) usage();

    System.exit(retVal);
  }
Esempio n. 17
0
  @Test
  public void allowsExportOfOptions() {
    Properties expected = new Properties();
    expected.setProperty("rice.count", "3");
    // Cannot check path as string directly - Windows flips the leading slash
    expected.setProperty("rice.output-dir", new File("/tmp").toString());
    expected.setProperty("rice.fun", "false");
    expected.setProperty("rice.verbose", "true");

    OptionParser parser = new OptionParser();
    OptionSpec<Integer> count = parser.accepts("count").withRequiredArg().ofType(Integer.class);
    OptionSpec<File> outputDir = parser.accepts("output-dir").withOptionalArg().ofType(File.class);
    OptionSpec<Void> verbose = parser.accepts("verbose");
    OptionSpec<Void> fun = parser.accepts("fun");
    OptionSpec<File> files = parser.nonOptions().ofType(File.class);

    OptionSet options =
        parser.parse("--count", "3", "--output-dir", "/tmp", "--verbose", "a.txt", "b.txt");

    assertEquals(expected, asProperties(options, "rice"));
  }
Esempio n. 18
0
  public void run() {
    Reflections.log = null;

    printVersion();

    OptionParser parser = buildOptionParser();

    OptionSet options = parser.parse(arguments);
    if (options.has(ARGUMENT_EXPERIMENT)) {
      runExperiment(options.valueOf(ARGUMENT_EXPERIMENT).toString());
      System.exit(0);
    }
    if (options.has(ARGUMENT_EXPERIMENT_LIST)) {
      printExperimentList();
      System.exit(0);
    }
    if (options.has(ARGUMENT_HELP)) {
      printHelp(parser);
      System.exit(0);
    }
    printHelp(parser);
  }
  /**
   * @param args
   * @throws Exception
   */
  public static void main(String[] args) throws Exception {
    PropertyConfigurator.configure("conf/log4j.properties");
    // args = new String[] { "-c", "127.0.0.1", "-k", "localhost:9092" };
    OptionParser parser = new OptionParser();
    parser.accepts("c", "cassandra server address").withRequiredArg().ofType(String.class);
    parser.accepts("k", "kafka server address").withRequiredArg().ofType(String.class);
    parser.accepts("r", "consume topics from the beginning");
    parser
        .accepts("g", "server address of ganglia gmond. e.g 10.11.1.212:8649")
        .withRequiredArg()
        .ofType(String.class);
    OptionSet set = parser.parse(args);
    if (!set.hasOptions()) {
      parser.printHelpOn(new PrintStream(System.out));
      System.exit(1);
    }

    String[] fields = set.valueOf("g").toString().split(":");
    MetricBasedPerfProfile.reportForGanglia(fields[0], Short.parseShort(fields[1].trim()));
    TimeSeriesProducer producer = new TimeSeriesProducer();
    producer.init(set.valueOf("k").toString(), set.valueOf("c").toString().trim(), set.has("r"));
    producer.start();
  }
Esempio n. 20
0
  public static void main(String[] args) {
    OptionParser p = new OptionParser("fc:q::");
    p.accepts("ftp");
    p.accepts("user").requiredIf("ftp").withRequiredArg();
    p.accepts("pass").requiredIf("ftp").withRequiredArg();

    OptionSet opt = p.parse(args);
    if (opt.has("f")) {
      System.out.println("-f exist");
      System.out.println("-f with : " + opt.valueOf("f"));
    }
    if (opt.has("c")) {
      System.out.println("-c exist with:" + opt.valueOf("c"));
    }

    if (opt.has("ftp")) {
      // we know username and password
      // existe if -ftp is set
      String user = (String) opt.valueOf("user");
      String pwd = (String) opt.valueOf("pass");
      System.out.println("user:"******" pass:" + pwd);
    }
  }
Esempio n. 21
0
  public static void main(String[] args) throws Exception {
    // We don't want to do the full argument parsing here as that might easily change in update
    // versions
    // So we only handle the absolute minimum which is APP_NAME, APP_DATA_DIR_KEY and USER_DATA_DIR
    OptionParser parser = new OptionParser();
    parser.allowsUnrecognizedOptions();
    parser
        .accepts(USER_DATA_DIR_KEY, description("User data directory", DEFAULT_USER_DATA_DIR))
        .withRequiredArg();
    parser
        .accepts(APP_NAME_KEY, description("Application name", DEFAULT_APP_NAME))
        .withRequiredArg();

    OptionSet options;
    try {
      options = parser.parse(args);
    } catch (OptionException ex) {
      System.out.println("error: " + ex.getMessage());
      System.out.println();
      parser.printHelpOn(System.out);
      System.exit(EXIT_FAILURE);
      return;
    }
    BitsquareEnvironment bitsquareEnvironment = new BitsquareEnvironment(options);

    // need to call that before BitsquareAppMain().execute(args)
    initAppDir(bitsquareEnvironment.getProperty(BitsquareEnvironment.APP_DATA_DIR_KEY));

    // For some reason the JavaFX launch process results in us losing the thread context class
    // loader: reset it.
    // In order to work around a bug in JavaFX 8u25 and below, you must include the following code
    // as the first line of your realMain method:
    Thread.currentThread().setContextClassLoader(BitsquareAppMain.class.getClassLoader());

    new BitsquareAppMain().execute(args);
  }
Esempio n. 22
0
  public static void main(final String[] args) throws Exception {
    if (OS.getJavaVersion() < 1.8f) {
      System.out.println(ResourceUtils.getString("Message.JVM8"));
      System.out.println(
          ResourceUtils.getString("Message.Version")
              + " "
              + System.getProperty("java.version")
              + "\n");

      // try and show a dialog
      JOptionPane.showMessageDialog(
          null,
          ResourceUtils.getString("Message.JVM8"),
          ResourceUtils.getString("Title.Error"),
          JOptionPane.ERROR_MESSAGE);
      return;
    }

    if (OS.getJavaRelease() < OS.JVM_RELEASE_60) {
      JOptionPane.showMessageDialog(
          null,
          ResourceUtils.getString("Message.JFX"),
          ResourceUtils.getString("Title.Error"),
          JOptionPane.ERROR_MESSAGE);
      return;
    }

    // Register the default exception handler
    Thread.setDefaultUncaughtExceptionHandler(new StaticUIMethods.ExceptionHandler());

    configureLogging();

    OptionParser parser = buildParser();

    try {
      final OptionSet options = parser.parse(args);

      // Does the user want to uninstall and clear their registry settings
      if (options.has(UNINSTALL_OPTION_SHORT)) {
        PortablePreferences.deleteUserPreferences();
        System.exit(0);
      }

      // Needs to be checked for launching
      if (options.has(VERBOSE_OPTION_SHORT)) {
        System.setProperty("javafx.verbose", "true");
      }

      // Check to see if portable preferences are being used
      if (options.has(PORTABLE_FILE_OPTION)) {
        final File file = (File) options.valueOf(PORTABLE_FILE_OPTION);
        if (file.exists()) {
          PortablePreferences.initPortablePreferences(file.getAbsolutePath());
        }
      } else if (options.has(PORTABLE_OPTION_SHORT)) { // simple use of portable preferences
        PortablePreferences.initPortablePreferences(null);
      }

      if (options.has(PORT_OPTION)) {
        port = (Integer) options.valueOf(PORT_OPTION);
      }

      if (options.has(PASSWORD_OPTION)) {
        password = ((String) options.valueOf(PASSWORD_OPTION)).toCharArray();
      }

      if (options.has(FILE_OPTION_SHORT)) {
        final File file = (File) options.valueOf(FILE_OPTION_SHORT);
        if (file.exists()) {
          dataFile = file;
        }
      } else if (!options.nonOptionArguments().isEmpty() && dataFile == null) {
        // Check for no-option version of a file load
        for (Object object : options.nonOptionArguments()) {
          if (object instanceof String) {
            if (Files.exists(Paths.get((String) object))) {
              dataFile = new File((String) object);
              break;
            }
          }
        }
      }

      if (options.has(HOST_OPTION)) {
        host = (String) options.valueOf(HOST_OPTION);
      }

      if (options.has(SERVER_OPTION)) {
        final File file = (File) options.valueOf(SERVER_OPTION);
        if (file.exists()) {
          serverFile = file;
        }
      }

      // parser.printHelpOn(System.err);

      if (serverFile != null) {
        parser = null; // not needed anymore, trigger GC
        startServer();
      } else {
        parser = null; // not needed anymore, trigger GC
        setupNetworking();
        launch(args);
      }
    } catch (final Exception exception) {
      if (parser != null) {
        parser.printHelpOn(System.err);
      }
    }
  }
Esempio n. 23
0
  /**
   * Try to set the arguments from main method on the fields loaded by loadOpts(Class<?> c).
   *
   * @param args - Arguments passed from main method.
   * @return this
   */
  public Flags parse(String[] args) {
    optionSet = optionParser.parse(args);

    // Store non option arguments
    nonOptionArguments = optionSet.nonOptionArguments();
    if (nonOptionArguments == null) {
      nonOptionArguments = new ArrayList<String>();
    }

    // do not parse options if "help" is a part of the arguments given
    if (helpFlagged()) {
      return this;
    }
    if (propertiesFlagged()) {
      List<String> files = optionSet.valuesOf(PROPERTIES_FILE);
      ArrayList<String> newArgs = new ArrayList<String>();
      for (String filename : files) {
        final Properties props = new Properties();
        try {
          final FileInputStream stream = new FileInputStream(filename);
          props.load(stream);
          for (Enumeration<?> keys = props.propertyNames(); keys.hasMoreElements(); ) {
            String flagName = (String) keys.nextElement();
            if (!options.containsKey(flagName) || optionSet.hasArgument(flagName)) {
              // Properties contains something not in options or is already set by commandline
              // argument
              // Command line argument takes precedence over properties file
              continue;
            }
            newArgs.add("--" + flagName);
            newArgs.add(props.getProperty(flagName));
          }

          stream.close();
        } catch (IOException e) {
          throw new RuntimeException("Could not parse property-file", e);
        }
      }
      Collections.addAll(newArgs, args);
      optionSet = optionParser.parse(newArgs.toArray(new String[newArgs.size()]));
    }

    for (OptionHolder holder : options.values()) {
      try {
        OptionSpec<?> optionSpec = holder.getOptionSpec();

        // Deal with the flags that were given on the command line.
        if (optionSet.has(optionSpec)) {
          switch (holder.getType()) {
            case INTEGER:
              if (holder.isInstanced()) {
                holder.getField().set(holder.getObjectSource(), optionSet.valueOf(optionSpec));
              } else {
                holder.getField().set(holder.getField().getClass(), optionSet.valueOf(optionSpec));
              }
              break;

            case LONG:
              if (holder.isInstanced()) {
                holder.getField().set(holder.getObjectSource(), optionSet.valueOf(optionSpec));
              } else {
                holder.getField().set(holder.getField().getClass(), optionSet.valueOf(optionSpec));
              }
              break;

            case STRING:
              if (holder.isInstanced()) {
                holder.getField().set(holder.getObjectSource(), optionSet.valueOf(optionSpec));
              } else {
                holder.getField().set(holder.getField().getClass(), optionSet.valueOf(optionSpec));
              }
              break;

            case BOOLEAN:
              Object value = optionSet.valueOf(optionSpec);
              if (holder.isInstanced()) {
                holder.getField().set(holder.getObjectSource(), (value == null) ? true : value);
              } else {
                holder.getField().set(holder.getField().getClass(), (value == null) ? true : value);
              }
              break;

            case ENUM:
              if (holder.isInstanced()) {
                try {
                  holder.getField().set(holder.getObjectSource(), optionSet.valueOf(optionSpec));
                } catch (Exception e) {
                  throw new IllegalArgumentException(
                      "Option given is not a valid option. Valid options are: "
                          + enumOptions.get(holder.flag.options()).toString()
                          + ".");
                }
              } else {
                try {
                  holder
                      .getField()
                      .set(holder.getField().getClass(), optionSet.valueOf(optionSpec));
                } catch (Exception e) {
                  throw new IllegalArgumentException(
                      "Option given is not a valid option. Valid options are: "
                          + enumOptions.get(holder.flag.options()).toString()
                          + ".");
                }
              }
              break;
          }

          // No further action needed for this field.
          continue;
        }

        // Check if flag that does not occur in command line was required.
        if (holder.getFlag().required()) {
          throw new IllegalArgumentException(
              "Required argument missing: " + holder.getFlag().name());
        }
      } catch (IllegalAccessException e) {
        throw new RuntimeException(
            "Programming error, illegal access for " + holder.getField().toGenericString());
      }
    }
    return this;
  }
Esempio n. 24
0
  public static void main(String[] args) throws InterruptedException, IOException {
    OptionParser parser = new OptionParser();
    ArgumentAcceptingOptionSpec<String> consumerConfigOpt =
        parser
            .accepts(
                "consumer.config",
                "Kafka 0.7 consumer config to consume from the source 0.7 cluster. "
                    + "You man specify multiple of these.")
            .withRequiredArg()
            .describedAs("config file")
            .ofType(String.class);

    ArgumentAcceptingOptionSpec<String> producerConfigOpt =
        parser
            .accepts("producer.config", "Producer config.")
            .withRequiredArg()
            .describedAs("config file")
            .ofType(String.class);

    ArgumentAcceptingOptionSpec<Integer> numProducersOpt =
        parser
            .accepts("num.producers", "Number of producer instances")
            .withRequiredArg()
            .describedAs("Number of producers")
            .ofType(Integer.class)
            .defaultsTo(1);

    ArgumentAcceptingOptionSpec<String> zkClient01JarOpt =
        parser
            .accepts("zkclient.01.jar", "zkClient 0.1 jar file")
            .withRequiredArg()
            .describedAs("zkClient 0.1 jar file required by Kafka 0.7")
            .ofType(String.class);

    ArgumentAcceptingOptionSpec<String> kafka07JarOpt =
        parser
            .accepts("kafka.07.jar", "Kafka 0.7 jar file")
            .withRequiredArg()
            .describedAs("kafka 0.7 jar")
            .ofType(String.class);

    ArgumentAcceptingOptionSpec<Integer> numStreamsOpt =
        parser
            .accepts("num.streams", "Number of consumer streams")
            .withRequiredArg()
            .describedAs("Number of consumer threads")
            .ofType(Integer.class)
            .defaultsTo(1);

    ArgumentAcceptingOptionSpec<String> whitelistOpt =
        parser
            .accepts("whitelist", "Whitelist of topics to migrate from the 0.7 cluster")
            .withRequiredArg()
            .describedAs("Java regex (String)")
            .ofType(String.class);

    ArgumentAcceptingOptionSpec<String> blacklistOpt =
        parser
            .accepts("blacklist", "Blacklist of topics to migrate from the 0.7 cluster")
            .withRequiredArg()
            .describedAs("Java regex (String)")
            .ofType(String.class);

    ArgumentAcceptingOptionSpec<Integer> queueSizeOpt =
        parser
            .accepts(
                "queue.size",
                "Number of messages that are buffered between the 0.7 consumer and 0.8 producer")
            .withRequiredArg()
            .describedAs("Queue size in terms of number of messages")
            .ofType(Integer.class)
            .defaultsTo(10000);

    OptionSpecBuilder helpOpt = parser.accepts("help", "Print this message.");

    OptionSet options = parser.parse(args);

    if (options.has(helpOpt)) {
      parser.printHelpOn(System.out);
      System.exit(0);
    }

    checkRequiredArgs(
        parser,
        options,
        new OptionSpec[] {consumerConfigOpt, producerConfigOpt, zkClient01JarOpt, kafka07JarOpt});
    int whiteListCount = options.has(whitelistOpt) ? 1 : 0;
    int blackListCount = options.has(blacklistOpt) ? 1 : 0;
    if (whiteListCount + blackListCount != 1) {
      System.err.println("Exactly one of whitelist or blacklist is required.");
      System.exit(1);
    }

    String kafkaJarFile_07 = options.valueOf(kafka07JarOpt);
    String zkClientJarFile = options.valueOf(zkClient01JarOpt);
    String consumerConfigFile_07 = options.valueOf(consumerConfigOpt);
    int numConsumers = options.valueOf(numStreamsOpt);
    String producerConfigFile_08 = options.valueOf(producerConfigOpt);
    int numProducers = options.valueOf(numProducersOpt);
    final List<MigrationThread> migrationThreads = new ArrayList<MigrationThread>(numConsumers);
    final List<ProducerThread> producerThreads = new ArrayList<ProducerThread>(numProducers);

    try {
      File kafkaJar_07 = new File(kafkaJarFile_07);
      File zkClientJar = new File(zkClientJarFile);
      ParentLastURLClassLoader c1 =
          new ParentLastURLClassLoader(
              new URL[] {kafkaJar_07.toURI().toURL(), zkClientJar.toURI().toURL()});

      /** Construct the 07 consumer config * */
      ConsumerConfig_07 = c1.loadClass(KAFKA_07_CONSUMER_CONFIG_CLASS_NAME);
      KafkaStaticConsumer_07 = c1.loadClass(KAFKA_07_STATIC_CONSUMER_CLASS_NAME);
      ConsumerConnector_07 = c1.loadClass(KAFKA_07_CONSUMER_CONNECTOR_CLASS_NAME);
      KafkaStream_07 = c1.loadClass(KAFKA_07_CONSUMER_STREAM_CLASS_NAME);
      TopicFilter_07 = c1.loadClass(KAFKA_07_TOPIC_FILTER_CLASS_NAME);
      WhiteList_07 = c1.loadClass(KAFKA_07_WHITE_LIST_CLASS_NAME);
      BlackList_07 = c1.loadClass(KAFKA_07_BLACK_LIST_CLASS_NAME);
      KafkaMessageClass_07 = c1.loadClass(KAFKA_07_MESSAGE_CLASS_NAME);
      KafkaConsumerIteratorClass_07 = c1.loadClass(KAFKA_07_CONSUMER_ITERATOR_CLASS_NAME);
      KafkaMessageAndMetatDataClass_07 = c1.loadClass(KAFKA_07_MESSAGE_AND_METADATA_CLASS_NAME);

      Constructor ConsumerConfigConstructor_07 = ConsumerConfig_07.getConstructor(Properties.class);
      Properties kafkaConsumerProperties_07 = new Properties();
      kafkaConsumerProperties_07.load(new FileInputStream(consumerConfigFile_07));
      /**
       * Disable shallow iteration because the message format is different between 07 and 08, we
       * have to get each individual message *
       */
      if (kafkaConsumerProperties_07.getProperty("shallow.iterator.enable", "").equals("true")) {
        log.warn("Shallow iterator should not be used in the migration tool");
        kafkaConsumerProperties_07.setProperty("shallow.iterator.enable", "false");
      }
      Object consumerConfig_07 =
          ConsumerConfigConstructor_07.newInstance(kafkaConsumerProperties_07);

      /** Construct the 07 consumer connector * */
      Method ConsumerConnectorCreationMethod_07 =
          KafkaStaticConsumer_07.getMethod("createJavaConsumerConnector", ConsumerConfig_07);
      final Object consumerConnector_07 =
          ConsumerConnectorCreationMethod_07.invoke(null, consumerConfig_07);
      Method ConsumerConnectorCreateMessageStreamsMethod_07 =
          ConsumerConnector_07.getMethod("createMessageStreamsByFilter", TopicFilter_07, int.class);
      final Method ConsumerConnectorShutdownMethod_07 = ConsumerConnector_07.getMethod("shutdown");
      Constructor WhiteListConstructor_07 = WhiteList_07.getConstructor(String.class);
      Constructor BlackListConstructor_07 = BlackList_07.getConstructor(String.class);
      Object filterSpec = null;
      if (options.has(whitelistOpt))
        filterSpec = WhiteListConstructor_07.newInstance(options.valueOf(whitelistOpt));
      else filterSpec = BlackListConstructor_07.newInstance(options.valueOf(blacklistOpt));

      Object retKafkaStreams =
          ConsumerConnectorCreateMessageStreamsMethod_07.invoke(
              consumerConnector_07, filterSpec, numConsumers);

      Properties kafkaProducerProperties_08 = new Properties();
      kafkaProducerProperties_08.load(new FileInputStream(producerConfigFile_08));
      kafkaProducerProperties_08.setProperty("serializer.class", "kafka.serializer.DefaultEncoder");
      // create a producer channel instead
      int queueSize = options.valueOf(queueSizeOpt);
      ProducerDataChannel<KeyedMessage<byte[], byte[]>> producerDataChannel =
          new ProducerDataChannel<KeyedMessage<byte[], byte[]>>(queueSize);
      int threadId = 0;

      Runtime.getRuntime()
          .addShutdownHook(
              new Thread() {
                @Override
                public void run() {
                  try {
                    ConsumerConnectorShutdownMethod_07.invoke(consumerConnector_07);
                  } catch (Exception e) {
                    log.error("Error while shutting down Kafka consumer", e);
                  }
                  for (MigrationThread migrationThread : migrationThreads) {
                    migrationThread.shutdown();
                  }
                  for (ProducerThread producerThread : producerThreads) {
                    producerThread.shutdown();
                  }
                  for (ProducerThread producerThread : producerThreads) {
                    producerThread.awaitShutdown();
                  }
                  log.info("Kafka migration tool shutdown successfully");
                }
              });

      // start consumer threads
      for (Object stream : (List) retKafkaStreams) {
        MigrationThread thread = new MigrationThread(stream, producerDataChannel, threadId);
        threadId++;
        thread.start();
        migrationThreads.add(thread);
      }

      String clientId = kafkaProducerProperties_08.getProperty("client.id");
      // start producer threads
      for (int i = 0; i < numProducers; i++) {
        kafkaProducerProperties_08.put("client.id", clientId + "-" + i);
        ProducerConfig producerConfig_08 = new ProducerConfig(kafkaProducerProperties_08);
        Producer producer = new Producer(producerConfig_08);
        ProducerThread producerThread = new ProducerThread(producerDataChannel, producer, i);
        producerThread.start();
        producerThreads.add(producerThread);
      }
    } catch (Throwable e) {
      System.out.println("Kafka migration tool failed due to: " + Utils.stackTrace(e));
      log.error("Kafka migration tool failed: ", e);
    }
  }
  public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser();
    ArgumentAcceptingOptionSpec<String> topicIdOpt =
        parser
            .accepts("topic", "REQUIRED: The topic id to consumer on.") //
            .withRequiredArg()
            .describedAs("topic")
            .ofType(String.class);
    ArgumentAcceptingOptionSpec<String> serverOpt =
        parser
            .accepts("server", "REQUIRED: The jafka server connection string.") //
            .withRequiredArg()
            .describedAs("jafka://hostname:port")
            .ofType(String.class);
    ArgumentAcceptingOptionSpec<Long> offsetOpt =
        parser
            .accepts("offset", "The offset to start consuming from.") //
            .withRequiredArg()
            .describedAs("offset")
            .ofType(Long.class)
            .defaultsTo(0L);

    //
    OptionSet options = parser.parse(args);
    checkRequiredArgs(parser, options, topicIdOpt, serverOpt);

    final URI server = new URI(options.valueOf(serverOpt));
    final String topic = options.valueOf(topicIdOpt);
    final long startingOffset = options.valueOf(offsetOpt).longValue();
    //
    final SimpleConsumer consumer =
        new SimpleConsumer(server.getHost(), server.getPort(), 10000, 64 * 1024);
    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {

              public void run() {
                Closer.closeQuietly(consumer);
              }
            });
    //
    Thread thread =
        new Thread() {

          public void run() {
            long offset = startingOffset;
            int consumed = 0;
            while (true) {
              try {
                FetchRequest fetchRequest = new FetchRequest(topic, 0, offset, 1000000);
                ByteBufferMessageSet messageSets = consumer.fetch(fetchRequest);
                boolean empty = true;
                for (MessageAndOffset messageAndOffset : messageSets) {
                  empty = false;
                  consumed++;
                  offset = messageAndOffset.offset;
                  System.out.println(
                      String.format(
                          "[%d] %d: %s",
                          consumed,
                          offset, //
                          Utils.toString(messageAndOffset.message.payload(), "UTF-8")));
                }
                if (empty) {
                  Thread.sleep(1000L);
                }
              } catch (Exception e) {
                throw new RuntimeException(e);
              }
            }
          }
        };
    thread.start();
    thread.join();
  }
  public int execute(String[] args) throws IOException {
    if (args.length == 0) {
      printUsage(errorWriter);
      return 1;
    }
    final OptionSet options = optionParser.parse(args);
    if (options.has(OptionParserFactory.OPT_HELP_SHORT)
        || options.has(OptionParserFactory.OPT_HELP_LONG)) {
      printHelp();
      return 0;
    }
    if (!options.has(OPT_APPLICATION)) {
      errorWriter.println("Missing required argument --" + OPT_APPLICATION);
      return 1;
    }
    if (!(options.has(OPT_GROUP_NAME))
        && !(options.has(OPT_GROUP_ID))
        && !(options.has(OPT_SERVER_ID))
        && !(options.has(OPT_SERVER_NAME))) {
      errorWriter.println("Either the name or ID of a server or group must be specified");
      return 1;
    }
    if ((options.has(OPT_GROUP_NAME) || options.has(OPT_GROUP_ID))
        && (options.has(OPT_SERVER_ID) || options.has(OPT_SERVER_NAME))) {
      errorWriter.println("Only one of either server or group identifiers should be specified");
      return 1;
    }
    if (options.has(OPT_SERVER_ID) && options.has(OPT_SERVER_NAME)) {
      errorWriter.println("Only serverid or servername can be specified, not both.");
      return 1;
    }
    final Service service = new Service();
    service.setName("Catalina");
    if (options.has(OPT_SERVICE)) {
      service.setName((String) options.valueOf(OPT_SERVICE));
    }
    final Host host = new Host();
    host.setName("localhost");
    if (options.has(OPT_HOST)) {
      host.setName((String) options.valueOf(OPT_HOST));
    }
    final Application application = new Application();
    application.setName((String) options.valueOf(OPT_APPLICATION));
    application.setVersion("");
    if (options.has(OPT_REVISION)) {
      String version = (String) options.valueOf(OPT_REVISION);
      application.setVersion("0".equals(version) ? "" : version);
    }

    if (options.has(OPT_SERVER_ID) || options.has(OPT_SERVER_NAME)) {
      final Resource server = new Resource();
      if (options.has(OPT_SERVER_ID)) {
        server.setId((Integer) options.valueOf(OPT_SERVER_ID));
      }
      server.setName((String) options.valueOf(OPT_SERVER_NAME));

      final ApplicationManagementResponse response =
          applicationManager.reloadApplication(server, service, host, application);
      return handleResponse(response);
    }
    final Group group = new Group();
    if (options.has(OPT_GROUP_ID)) {
      group.setId((Integer) options.valueOf(OPT_GROUP_ID));
    }
    group.setName((String) options.valueOf(OPT_GROUP_NAME));
    final ApplicationManagementResponse response =
        applicationManager.reloadApplication(group, service, host, application);
    return handleResponse(response);
  }
  private static PropertiesConfiguration initConfiguration(String[] args) {
    OptionSet options = null;
    OptionParser parser = new OptionParser();
    PropertiesConfiguration conf = null;

    // Setup the option parser
    parser.accepts("help", "print this help statement");
    parser
        .accepts("conf", "configuration file (required)")
        .withRequiredArg()
        .ofType(String.class)
        .required();
    parser
        .accepts("interval", "interval between rate measurements")
        .withRequiredArg()
        .ofType(Integer.class);
    parser.accepts("ops", "total number of operations").withRequiredArg().ofType(Integer.class);
    parser
        .accepts("lbound", "lowerbound for the number of operations")
        .withRequiredArg()
        .ofType(Integer.class);
    parser
        .accepts("time", "time tests will run for (milliseconds)")
        .withRequiredArg()
        .ofType(Integer.class);
    parser.accepts("sync", "sync or async test").withRequiredArg().ofType(Boolean.class);

    // Parse and gather the arguments
    try {
      options = parser.parse(args);
    } catch (OptionException e) {
      System.out.println("\nError parsing arguments.\n");
      try {
        parser.printHelpOn(System.out);
      } catch (IOException e2) {
        LOG.error("Exception while printing help message", e2);
      }
      System.exit(-1);
    }

    Integer interval = (Integer) options.valueOf("interval");
    Integer totOps = (Integer) options.valueOf("ops");
    Integer lowerbound = (Integer) options.valueOf("lbound");
    Integer time = (Integer) options.valueOf("time");
    Boolean sync = (Boolean) options.valueOf("sync");

    // Load and parse the configuration file
    String configFile = (String) options.valueOf("conf");
    LOG.info("Loading benchmark from configuration file: " + configFile);

    try {
      conf = new PropertiesConfiguration(configFile);
    } catch (ConfigurationException e) {
      LOG.error("Failed to read configuration file: " + configFile, e);
      System.exit(-2);
    }

    // If there are options from command line, override the conf
    if (interval != null) conf.setProperty("interval", interval);
    if (totOps != null) conf.setProperty("totalOperations", totOps);
    if (lowerbound != null) conf.setProperty("lowerbound", lowerbound);
    if (time != null) conf.setProperty("totalTime", time);
    if (sync != null) conf.setProperty("sync", sync);

    return conf;
  }
Esempio n. 28
0
 public static void main(String[] args) throws Exception {
   OptionParser parser = new OptionParser();
   parser.accepts("no-message", "do not decode message(utf-8 strings)");
   parser.accepts("no-offset", "do not print message offset");
   parser.accepts("no-size", "do not print message size");
   ArgumentAcceptingOptionSpec<Integer> countOpt =
       parser
           .accepts("c", "max count mesages.") //
           .withRequiredArg()
           .describedAs("count") //
           .ofType(Integer.class)
           .defaultsTo(-1);
   ArgumentAcceptingOptionSpec<String> fileOpt =
       parser
           .accepts("file", "decode file list") //
           .withRequiredArg()
           .ofType(String.class)
           .describedAs("filepath");
   OptionSet options = parser.parse(args);
   if (!options.has(fileOpt)) {
     System.err.println("Usage: [options] --file <file>...");
     parser.printHelpOn(System.err);
     System.exit(1);
   }
   final boolean decode = !options.has("no-message");
   final boolean withOffset = !options.has("no-offset");
   final boolean withSize = !options.has("no-size");
   int count = countOpt.value(options);
   count = count <= 0 ? Integer.MAX_VALUE : count;
   int index = 0;
   final String logformat = "%s|%s|%s";
   for (String filepath : fileOpt.values(options)) {
     if (index >= count) break;
     File file = new File(filepath);
     String filename = file.getName();
     final long startOffset = Long.parseLong(filename.substring(0, filename.lastIndexOf('.')));
     FileMessageSet messageSet = new FileMessageSet(file, false);
     long offset = 0L;
     long totalSize = 0L;
     try {
       int messageCount = 0;
       for (MessageAndOffset mao : messageSet) {
         final Message msg = mao.message;
         if (index >= count) {
           break;
         }
         if (decode || withOffset || withSize) {
           System.out.println(
               format(
                   logformat, //
                   withOffset ? "" + (startOffset + offset) : "", //
                   withSize ? "" + msg.payloadSize() : "", //
                   decode ? Utils.toString(msg.payload(), "UTF-8") : ""));
         }
         offset = mao.offset;
         totalSize += msg.payloadSize();
         messageCount++;
         index++;
       }
       System.out.println(
           "-----------------------------------------------------------------------------");
       System.out.println(filepath);
       System.out.println("total message count: " + messageCount);
       System.out.println("total message size: " + totalSize);
       System.out.println("=============================================");
     } finally {
       messageSet.close();
     }
   }
 }
Esempio n. 29
0
  public static void main(String[] args) {
    // TODO main
    OptionParser parser = new OptionParser();
    parser.acceptsAll(Arrays.asList("h", "help"), "Show this help dialog.");
    OptionSpec<String> serverOption =
        parser
            .acceptsAll(Arrays.asList("s", "server"), "Server to join.")
            .withRequiredArg()
            .describedAs("server-address[:port]");
    OptionSpec<String> proxyOption =
        parser
            .acceptsAll(
                Arrays.asList("P", "proxy"),
                "SOCKS proxy to use. Ignored in presence of 'socks-proxy-list'.")
            .withRequiredArg()
            .describedAs("proxy-address");
    OptionSpec<String> ownerOption =
        parser
            .acceptsAll(
                Arrays.asList("o", "owner"), "Owner of the bot (username of in-game control).")
            .withRequiredArg()
            .describedAs("username");
    OptionSpec<?> offlineOption =
        parser.acceptsAll(
            Arrays.asList("O", "offline"),
            "Offline-mode. Ignores 'password' and 'account-list' (will "
                + "generate random usernames if 'username' is not supplied).");
    OptionSpec<?> autoRejoinOption =
        parser.acceptsAll(Arrays.asList("a", "auto-rejoin"), "Auto-rejoin a server on disconnect.");
    OptionSpec<Integer> loginDelayOption =
        parser
            .acceptsAll(
                Arrays.asList("d", "login-delay"),
                "Delay between bot joins, in milliseconds. 5000 is "
                    + "recommended if not using socks proxies.")
            .withRequiredArg()
            .describedAs("delay")
            .ofType(Integer.class);
    OptionSpec<Integer> botAmountOption =
        parser
            .acceptsAll(
                Arrays.asList("b", "bot-amount"),
                "Amount of bots to join. Must be <= amount of accounts.")
            .withRequiredArg()
            .describedAs("amount")
            .ofType(Integer.class);

    OptionSpec<String> accountListOption =
        parser
            .accepts(
                "account-list",
                "File containing a list of accounts, in username/email:password format.")
            .withRequiredArg()
            .describedAs("file");
    OptionSpec<String> socksProxyListOption =
        parser
            .accepts(
                "socks-proxy-list",
                "File containing a list of SOCKS proxies, in address:port format.")
            .withRequiredArg()
            .describedAs("file");
    OptionSpec<String> httpProxyListOption =
        parser
            .accepts(
                "http-proxy-list",
                "File containing a list of HTTP proxies, in address:port format.")
            .withRequiredArg()
            .describedAs("file");

    OptionSet options;
    try {
      options = parser.parse(args);
    } catch (OptionException exception) {
      try {
        parser.printHelpOn(System.out);
      } catch (Exception exception1) {
        exception1.printStackTrace();
      }
      return;
    }

    if (options.has("help")) {
      printHelp(parser);
      return;
    }

    final boolean offline = options.has(offlineOption);
    final boolean autoRejoin = options.has(autoRejoinOption);

    final List<String> accounts;
    if (options.has(accountListOption)) {
      accounts = loadAccounts(options.valueOf(accountListOption));
    } else if (!offline) {
      System.out.println("Option 'accounts' must be supplied in " + "absence of option 'offline'.");
      printHelp(parser);
      return;
    } else accounts = null;

    final String server;
    if (!options.has(serverOption)) {
      System.out.println("Option 'server' required.");
      printHelp(parser);
      return;
    } else server = options.valueOf(serverOption);

    final String owner;
    if (!options.has(ownerOption)) {
      System.out.println("Option 'owner' required.");
      printHelp(parser);
      return;
    } else owner = options.valueOf(ownerOption);

    final List<String> socksProxies;
    if (options.has(socksProxyListOption))
      socksProxies = loadProxies(options.valueOf(socksProxyListOption));
    else socksProxies = null;
    final boolean useProxy = socksProxies != null;

    final List<String> httpProxies;
    if (options.has(httpProxyListOption))
      httpProxies = loadLoginProxies(options.valueOf(httpProxyListOption));
    else if (!offline && accounts != null) {
      System.out.println(
          "Option 'http-proxy-list' required if " + "option 'account-list' is supplied.");
      printHelp(parser);
      return;
    } else httpProxies = null;

    final int loginDelay;
    if (options.has(loginDelayOption)) loginDelay = options.valueOf(loginDelayOption);
    else loginDelay = 0;

    final int botAmount;
    if (!options.has(botAmountOption)) {
      System.out.println("Option 'bot-amount' required.");
      printHelp(parser);
      return;
    } else botAmount = options.valueOf(botAmountOption);

    initGui();
    while (!sessions.get()) {
      synchronized (sessions) {
        try {
          sessions.wait(5000);
        } catch (InterruptedException exception) {
        }
      }
    }

    final Queue<Runnable> lockQueue = new ArrayDeque<Runnable>();

    ExecutorService service = Executors.newFixedThreadPool(botAmount + (loginDelay > 0 ? 1 : 0));
    final Object firstWait = new Object();
    if (loginDelay > 0) {
      service.execute(
          new Runnable() {
            @Override
            public void run() {
              synchronized (firstWait) {
                try {
                  firstWait.wait();
                } catch (InterruptedException exception) {
                }
              }
              while (true) {
                if (die) return;
                while (slotsTaken.get() >= 2) {
                  synchronized (slotsTaken) {
                    try {
                      slotsTaken.wait(500);
                    } catch (InterruptedException exception) {
                    }
                  }
                }
                synchronized (lockQueue) {
                  if (lockQueue.size() > 0) {
                    Runnable thread = lockQueue.poll();
                    synchronized (thread) {
                      thread.notifyAll();
                    }
                    lockQueue.offer(thread);
                  } else continue;
                }
                try {
                  Thread.sleep(loginDelay);
                } catch (InterruptedException exception) {
                }
                while (!sessions.get()) {
                  synchronized (sessions) {
                    try {
                      sessions.wait(5000);
                    } catch (InterruptedException exception) {
                    }
                  }
                }
              }
            }
          });
    }
    final List<String> accountsInUse = new ArrayList<String>();
    for (int i = 0; i < botAmount; i++) {
      final int botNumber = i;
      Runnable runnable =
          new Runnable() {
            @Override
            public void run() {
              if (loginDelay > 0)
                synchronized (lockQueue) {
                  lockQueue.add(this);
                }
              Random random = new Random();

              if (!offline) {
                boolean authenticated = false;
                user:
                while (true) {
                  if (authenticated) {
                    authenticated = false;
                    sessionCount.decrementAndGet();
                  }
                  Session session = null;
                  String loginProxy;
                  String account = accounts.get(random.nextInt(accounts.size()));
                  synchronized (accountsInUse) {
                    if (accountsInUse.size() == accounts.size()) System.exit(0);
                    while (accountsInUse.contains(account))
                      account = accounts.get(random.nextInt(accounts.size()));
                    accountsInUse.add(account);
                  }
                  String[] accountParts = account.split(":");
                  while (true) {
                    while (!sessions.get()) {
                      synchronized (sessions) {
                        try {
                          sessions.wait(5000);
                        } catch (InterruptedException exception) {
                        }
                      }
                    }
                    loginProxy = httpProxies.get(random.nextInt(httpProxies.size()));
                    try {
                      session = Util.retrieveSession(accountParts[0], accountParts[1], loginProxy);
                      // addAccount(session);
                      sessionCount.incrementAndGet();
                      authenticated = true;
                      break;
                    } catch (AuthenticationException exception) {
                      System.err.println("[Bot" + botNumber + "] " + exception);
                      if (!exception.getMessage().startsWith("Exception"))
                        // && !exception.getMessage().equals(
                        // "Too many failed logins"))
                        continue user;
                    }
                  }
                  System.out.println(
                      "["
                          + session.getUsername()
                          + "] Password: "******", Session ID: "
                          + session.getSessionId());
                  while (!joins.get()) {
                    synchronized (joins) {
                      try {
                        joins.wait(5000);
                      } catch (InterruptedException exception) {
                      }
                    }
                  }
                  if (loginDelay > 0) {
                    synchronized (this) {
                      try {
                        synchronized (firstWait) {
                          firstWait.notifyAll();
                        }
                        wait();
                      } catch (InterruptedException exception) {
                      }
                    }
                  }

                  while (true) {
                    String proxy =
                        useProxy ? socksProxies.get(random.nextInt(socksProxies.size())) : null;
                    try {
                      new DarkBotMCSpambot(
                          DARK_BOT,
                          server,
                          session.getUsername(),
                          session.getPassword(),
                          session.getSessionId(),
                          null,
                          proxy,
                          owner);
                      if (die) break user;
                      else if (!autoRejoin) break;
                    } catch (Exception exception) {
                      exception.printStackTrace();
                      System.out.println(
                          "["
                              + session.getUsername()
                              + "] Error connecting: "
                              + exception.getCause().toString());
                    }
                  }
                  System.out.println("[" + session.getUsername() + "] Account failed");
                }
              } else {
                while (true) {
                  String proxy =
                      useProxy ? socksProxies.get(random.nextInt(socksProxies.size())) : null;
                  try {
                    String username = "";
                    if (accounts != null) {
                      username = accounts.get(random.nextInt(accounts.size())).split(":")[0];
                      synchronized (accountsInUse) {
                        while (accountsInUse.contains(username))
                          username = accounts.get(random.nextInt(accounts.size()));
                        accountsInUse.add(username);
                      }
                    } else
                      for (int i = 0; i < 10 + random.nextInt(6); i++)
                        username += alphas[random.nextInt(alphas.length)];
                    if (loginDelay > 0) {
                      synchronized (this) {
                        try {
                          synchronized (firstWait) {
                            firstWait.notifyAll();
                          }
                          wait();
                        } catch (InterruptedException exception) {
                        }
                      }
                    }
                    new DarkBotMCSpambot(DARK_BOT, server, username, "", "", null, proxy, owner);
                    if (die || !autoRejoin) break;
                    else continue;
                  } catch (Exception exception) {
                    System.out.println(
                        "[Bot" + botNumber + "] Error connecting: " + exception.toString());
                  }
                }
              }
            }
          };
      service.execute(runnable);
    }
    service.shutdown();
    while (!service.isTerminated()) {
      try {
        service.awaitTermination(9000, TimeUnit.DAYS);
      } catch (InterruptedException exception) {
        exception.printStackTrace();
      }
    }
    System.exit(0);
  }
Esempio n. 30
0
  public static void main(String args[]) {
    try {
      OptionParser parser = new OptionParser();

      ArgumentAcceptingOptionSpec<String> hardwareLayoutOpt =
          parser
              .accepts("hardwareLayout", "The path of the hardware layout file")
              .withRequiredArg()
              .describedAs("hardware_layout")
              .ofType(String.class);

      ArgumentAcceptingOptionSpec<String> partitionLayoutOpt =
          parser
              .accepts("partitionLayout", "The path of the partition layout file")
              .withRequiredArg()
              .describedAs("partition_layout")
              .ofType(String.class);

      ArgumentAcceptingOptionSpec<String> rootDirectoryForPartitionOpt =
          parser
              .accepts(
                  "rootDirectoryForPartition",
                  "Directory which contains all replicas for a partition which in turn will have all index files "
                      + "for the respective replica, for Operation ConsistencyCheckForIndex ")
              .withRequiredArg()
              .describedAs("root_directory_partition")
              .ofType(String.class);

      ArgumentAcceptingOptionSpec<String> includeAcceptableInconsistentBlobsOpt =
          parser
              .accepts(
                  "includeAcceptableInconsistentBlobs", "To include acceptable inconsistent blobs")
              .withRequiredArg()
              .describedAs("Whether to output acceptable inconsistent blobs or not")
              .defaultsTo("false")
              .ofType(String.class);

      ArgumentAcceptingOptionSpec<String> subjectOpt =
          parser
              .accepts(
                  "subject",
                  "The subject of the check (" + SUBJECT_BLOBS + ", " + SUBJECT_INDEX + " etc)")
              .withRequiredArg()
              .describedAs(
                  "To select the subject of the check ("
                      + SUBJECT_BLOBS
                      + ", "
                      + SUBJECT_INDEX
                      + " etc)")
              .defaultsTo(SUBJECT_BLOBS)
              .ofType(String.class);

      OptionSet options = parser.parse(args);

      ArrayList<OptionSpec<?>> listOpt = new ArrayList<OptionSpec<?>>();
      listOpt.add(hardwareLayoutOpt);
      listOpt.add(partitionLayoutOpt);
      listOpt.add(rootDirectoryForPartitionOpt);

      for (OptionSpec opt : listOpt) {
        if (!options.has(opt)) {
          System.err.println("Missing required argument \"" + opt + "\"");
          parser.printHelpOn(System.err);
          System.exit(1);
        }
      }

      String hardwareLayoutPath = options.valueOf(hardwareLayoutOpt);
      String partitionLayoutPath = options.valueOf(partitionLayoutOpt);
      ClusterMap map =
          new ClusterMapManager(
              hardwareLayoutPath,
              partitionLayoutPath,
              new ClusterMapConfig(new VerifiableProperties(new Properties())));
      String rootDirectoryForPartition = options.valueOf(rootDirectoryForPartitionOpt);
      boolean includeAcceptableInconsistentBlobs =
          Boolean.parseBoolean(options.valueOf(includeAcceptableInconsistentBlobsOpt));
      String subject = options.valueOf(subjectOpt);

      ConsistencyCheckerTool consistencyCheckerTool = null;
      if (subject.equals(SUBJECT_INDEX)) {
        consistencyCheckerTool = new IndexConsistencyCheckerTool(map);
      } else {
        consistencyCheckerTool =
            new BlobConsistencyCheckerTool(map, includeAcceptableInconsistentBlobs);
      }

      consistencyCheckerTool.checkConsistency(rootDirectoryForPartition);
    } catch (Exception e) {
      System.err.println("Consistency checker exited with Exception: " + e);
    }
  }