Example #1
0
  public static void main(String args[]) throws IOException, ParseException {
    Options options = new Options();
    options.addOption("u", "uniquehits", false, "only output hits with a single mapping");
    options.addOption(
        "s",
        "nosuboptimal",
        false,
        "do not include hits whose score is not equal to the best score for the read");
    CommandLineParser parser = new GnuParser();
    CommandLine cl = parser.parse(options, args, false);
    boolean uniqueOnly = cl.hasOption("uniquehits");
    boolean filterSubOpt = cl.hasOption("nosuboptimal");

    ArrayList<String[]> lines = new ArrayList<String[]>();

    String line;
    String lastRead = "";
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    while ((line = reader.readLine()) != null) {
      String pieces[] = line.split("\t");
      if (!pieces[0].equals(lastRead)) {
        printLines(lines, uniqueOnly, filterSubOpt);
        lines.clear();
      }
      lines.add(pieces);
      lastRead = pieces[0];
    }
    printLines(lines, uniqueOnly, filterSubOpt);
  }
Example #2
0
  public void init(String[] args) throws UserError {
    Context.reset();
    CommandLineParser globalParser = new GnuParser();

    List<String> globalArgs = new ArrayList<String>();

    boolean inGlobal = true;
    for (String arg : args) {
      if (inGlobal) {
        if (arg.startsWith("--")) {
          globalArgs.add(arg);
        } else {
          this.command = arg;
          inGlobal = false;
        }
      } else {
        commandArgs.add(arg);
      }
    }

    try {
      this.globalArguments =
          globalParser.parse(globalOptions, globalArgs.toArray(new String[globalArgs.size()]));
    } catch (ParseException e) {
      throw new UserError("Error parsing global command line argument: " + e.getMessage());
    }
  }
    public static Options parseArgs(String cmdArgs[]) {
      CommandLineParser parser = new GnuParser();
      CmdLineOptions options = getCmdLineOptions();
      try {
        CommandLine cmd = parser.parse(options, cmdArgs, false);

        if (cmd.hasOption(HELP_OPTION)) {
          printUsage(options);
          System.exit(0);
        }

        String[] args = cmd.getArgs();
        if (args.length == 0) {
          System.err.println("No sstables to split");
          printUsage(options);
          System.exit(1);
        }
        Options opts = new Options(Arrays.asList(args));
        opts.debug = cmd.hasOption(DEBUG_OPTION);
        opts.verbose = cmd.hasOption(VERBOSE_OPTION);
        opts.snapshot = !cmd.hasOption(NO_SNAPSHOT_OPTION);
        opts.sizeInMB = DEFAULT_SSTABLE_SIZE;

        if (cmd.hasOption(SIZE_OPTION))
          opts.sizeInMB = Integer.valueOf(cmd.getOptionValue(SIZE_OPTION));

        return opts;
      } catch (ParseException e) {
        errorMsg(e.getMessage(), options);
        return null;
      }
    }
    public static Options parseArgs(String cmdArgs[]) {
      CommandLineParser parser = new GnuParser();
      CmdLineOptions options = getCmdLineOptions();
      try {
        CommandLine cmd = parser.parse(options, cmdArgs, false);

        if (cmd.hasOption(HELP_OPTION)) {
          printUsage(options);
          System.exit(0);
        }

        String[] args = cmd.getArgs();
        if (args.length >= 4 || args.length < 2) {
          String msg = args.length < 2 ? "Missing arguments" : "Too many arguments";
          errorMsg(msg, options);
          System.exit(1);
        }

        String keyspace = args[0];
        String cf = args[1];
        String snapshot = null;
        if (args.length == 3) snapshot = args[2];

        Options opts = new Options(keyspace, cf, snapshot);

        opts.debug = cmd.hasOption(DEBUG_OPTION);
        opts.keepSource = cmd.hasOption(KEEP_SOURCE);

        return opts;
      } catch (ParseException e) {
        errorMsg(e.getMessage(), options);
        return null;
      }
    }
    public static LoaderOptions parseArgs(String cmdArgs[]) {
      CommandLineParser parser = new GnuParser();
      CmdLineOptions options = getCmdLineOptions();
      try {
        CommandLine cmd = parser.parse(options, cmdArgs, false);

        if (cmd.hasOption(HELP_OPTION)) {
          printUsage(options);
          System.exit(0);
        }

        String[] args = cmd.getArgs();
        if (args.length == 0) {
          System.err.println("Missing sstable directory argument");
          printUsage(options);
          System.exit(1);
        }

        if (args.length > 1) {
          System.err.println("Too many arguments");
          printUsage(options);
          System.exit(1);
        }

        String dirname = args[0];
        File dir = new File(dirname);

        if (!dir.exists()) errorMsg("Unknown directory: " + dirname, options);

        if (!dir.isDirectory()) errorMsg(dirname + " is not a directory", options);

        LoaderOptions opts = new LoaderOptions(dir);

        opts.debug = cmd.hasOption(DEBUG_OPTION);
        opts.verbose = cmd.hasOption(VERBOSE_OPTION);
        opts.noProgress = cmd.hasOption(NOPROGRESS_OPTION);

        if (cmd.hasOption(IGNORE_NODES_OPTION)) {
          String[] nodes = cmd.getOptionValue(IGNORE_NODES_OPTION).split(",");
          try {
            for (String node : nodes) {
              opts.ignores.add(InetAddress.getByName(node));
            }
          } catch (UnknownHostException e) {
            errorMsg(e.getMessage(), options);
          }
        }

        return opts;
      } catch (ParseException e) {
        errorMsg(e.getMessage(), options);
        return null;
      }
    }
Example #6
0
  public static void main(String[] args) {
    Main main = new Main();

    main.header("Liquibase SDK");

    try {
      main.init(args);

      if (main.command == null) {
        throw new UserError("No command passed");
      }

      if (main.command.equals("help")) {
        main.printHelp();
        return;
      }

      VagrantControl vagrantControl;
      if (main.command.equals("vagrant")) {
        vagrantControl = new VagrantControl(main);
      } else {
        throw new UserError("Unknown command: " + main.command);
      }

      CommandLineParser commandParser = new GnuParser();
      try {
        CommandLine commandArguments =
            commandParser.parse(
                vagrantControl.getOptions(),
                main.commandArgs.toArray(new String[main.commandArgs.size()]));

        vagrantControl.execute(commandArguments);
      } catch (ParseException e) {
        throw new UserError("Error parsing command arguments: " + e.getMessage());
      }

      main.divider();
      main.out("Command executed successfully");

    } catch (UserError userError) {
      main.out("");
      main.header("ERROR EXECUTING COMMAND");
      main.out(userError.getMessage());
      main.out("");
      main.out("");
      return;
    } catch (Throwable exception) {
      System.out.println("Unexpected error: " + exception.getMessage());
      exception.printStackTrace();
    }
  }
  public static void main(String[] args) throws Exception {

    String propertyFile = null;

    // create the command line parser
    CommandLineParser parser = new PosixParser();
    Options options = new Options();
    options.addOption("f", "file", true, "The properties file");
    options.addOption("v", "verbose", false, "Verbose logging output");
    options.addOption("h", "help", false, "Help output");

    try {
      CommandLine line = parser.parse(options, args);

      if (line.hasOption('v')) {
        org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.DEBUG);
      }

      if (line.hasOption('h')) {
        usage(options);
      }

      if (line.hasOption('f')) {
        propertyFile = line.getOptionValue('f');
      } else {
        throw new Exception("Please supply -f");
      }

    } catch (Exception e) {
      log.error("Invalid command line: " + e, e);
      usage(options);
    }

    Properties props = new Properties();
    props.load(new FileReader(propertyFile));
    startFromProperties(props);

    Runtime.getRuntime()
        .addShutdownHook(
            new Thread(
                new Runnable() {
                  @Override
                  public void run() {
                    log.info("Received shutdown signal. Shutting down...");
                    server.stop(3);
                  }
                }));
  }
Example #8
0
  private void parseCmdLine(String[] args) {
    CommandLineParser parser = new PosixParser();

    Options options = new Options();
    options.addOption("v", "version", false, "Q2's version");
    options.addOption("d", "deploydir", true, "Deployment directory");
    options.addOption("r", "recursive", false, "Deploy subdirectories recursively");
    options.addOption("h", "help", false, "Usage information");
    options.addOption("C", "config", true, "Configuration bundle");
    options.addOption("e", "encrypt", true, "Encrypt configuration bundle");
    options.addOption("i", "cli", false, "Command Line Interface");
    options.addOption("c", "command", true, "Command to execute");

    try {
      CommandLine line = parser.parse(options, args);
      if (line.hasOption("v")) {
        displayVersion();
        System.exit(0);
      }
      if (line.hasOption("h")) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("Q2", options);
        System.exit(0);
      }
      if (line.hasOption("c")) {
        cli = new CLI(this, line.getOptionValue("c"), line.hasOption("i"));
      } else if (line.hasOption("i")) cli = new CLI(this, null, true);

      String dir = DEFAULT_DEPLOY_DIR;
      if (line.hasOption("d")) {
        dir = line.getOptionValue("d");
      }
      recursive = line.hasOption("r");
      this.deployDir = new File(dir);
      if (line.hasOption("C")) deployBundle(new File(line.getOptionValue("C")), false);
      if (line.hasOption("e")) deployBundle(new File(line.getOptionValue("e")), true);
    } catch (MissingArgumentException e) {
      System.out.println("ERROR: " + e.getMessage());
      System.exit(1);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
  public static void main(String[] args) {
    Options options = new Options();
    options.addOption("v", true, "Input Vector folder"); // yearly vector data
    options.addOption("p", true, "Points folder"); // yearly mds (rotate) output
    options.addOption("d", true, "Destination folder");
    options.addOption("o", true, "Original stock file"); // global 10 year stock file
    options.addOption(
        "s",
        true,
        "Sector file"); // If Histogram true then set this as the folder to histogram output
    options.addOption("h", false, "Gen from histogram");
    options.addOption("e", true, "Extra classes file"); // a file containing fixed classes
    options.addOption("ci", true, "Cluster input file");
    options.addOption("co", true, "Cluster output file");

    CommandLineParser commandLineParser = new BasicParser();
    try {
      CommandLine cmd = commandLineParser.parse(options, args);
      String vectorFile = cmd.getOptionValue("v");
      String pointsFolder = cmd.getOptionValue("p");
      String distFolder = cmd.getOptionValue("d");
      String originalStocks = cmd.getOptionValue("o");
      String sectorFile = cmd.getOptionValue("s");
      boolean histogram = cmd.hasOption("h");
      String fixedClasses = cmd.getOptionValue("e");
      String clusterInputFile = cmd.getOptionValue("ci");
      String clusterOutputFile = cmd.getOptionValue("co");

      LabelApply program =
          new LabelApply(
              vectorFile,
              pointsFolder,
              distFolder,
              originalStocks,
              sectorFile,
              histogram,
              fixedClasses,
              clusterInputFile,
              clusterOutputFile);
      program.process();
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }
Example #10
0
  /**
   * Same as main(args) except that exceptions are thrown out instead of causing the VM to exit and
   * the lookup for .groovy files can be controlled
   */
  public static void commandLineCompile(String[] args, boolean lookupUnnamedFiles)
      throws Exception {
    Options options = createCompilationOptions();

    CommandLineParser cliParser = new GroovyPosixParser();

    CommandLine cli;
    cli = cliParser.parse(options, args);

    if (cli.hasOption('h')) {
      displayHelp(options);
      return;
    }

    if (cli.hasOption('v')) {
      displayVersion();
      return;
    }

    displayStackTraceOnError = cli.hasOption('e');

    CompilerConfiguration configuration = generateCompilerConfigurationFromOptions(cli);

    //
    // Load the file name list
    String[] filenames = generateFileNamesFromOptions(cli);
    boolean fileNameErrors = filenames == null;
    if (!fileNameErrors && (filenames.length == 0)) {
      displayHelp(options);
      return;
    }

    fileNameErrors = fileNameErrors && !validateFiles(filenames);

    if (!fileNameErrors) {
      doCompilation(configuration, null, filenames, lookupUnnamedFiles);
    }
  }
Example #11
0
  private static void parse_args(String[] args, String formatstr, Options opt) {
    HelpFormatter formatter = new HelpFormatter();
    CommandLineParser parser = new PosixParser();
    CommandLine cl = null;

    try {
      cl = parser.parse(opt, args);
    } catch (ParseException e) {
      formatter.printHelp(formatstr, opt); // 如果发生异常,则打印出帮助信息
    }

    if (cl.hasOption("in") && cl.hasOption("out") && cl.hasOption("dd") && cl.hasOption("sw")) {
      String stopWordsPath = cl.getOptionValue("sw");
      String inPath = cl.getOptionValue("in");
      String outPath = cl.getOptionValue("out");
      String dicPath = cl.getOptionValue("dd");
      processOperation(stopWordsPath, inPath, outPath, dicPath);
    } else {
      HelpFormatter hf = new HelpFormatter();
      hf.printHelp(formatstr, "", opt, "");
      return;
    }
  }
  /**
   * Given arguments specifying an SSTable, and optionally an output file, export the contents of
   * the SSTable to JSON.
   *
   * @param args command lines arguments
   * @throws ConfigurationException on configuration failure (wrong params given)
   */
  public static void main(String[] args) throws ConfigurationException {
    CommandLineParser parser = new PosixParser();
    try {
      cmd = parser.parse(options, args);
    } catch (ParseException e1) {
      System.err.println(e1.getMessage());
      printUsage();
      System.exit(1);
    }

    if (cmd.getArgs().length != 1) {
      System.err.println("You must supply exactly one sstable");
      printUsage();
      System.exit(1);
    }

    String[] keys = cmd.getOptionValues(KEY_OPTION);
    HashSet<String> excludes =
        new HashSet<>(
            Arrays.asList(
                cmd.getOptionValues(EXCLUDE_KEY_OPTION) == null
                    ? new String[0]
                    : cmd.getOptionValues(EXCLUDE_KEY_OPTION)));
    String ssTableFileName = new File(cmd.getArgs()[0]).getAbsolutePath();

    if (Descriptor.isLegacyFile(new File(ssTableFileName))) {
      System.err.println("Unsupported legacy sstable");
      System.exit(1);
    }
    if (!new File(ssTableFileName).exists()) {
      System.err.println("Cannot find file " + ssTableFileName);
      System.exit(1);
    }
    Descriptor desc = Descriptor.fromFilename(ssTableFileName);
    try {
      CFMetaData metadata = metadataFromSSTable(desc);
      if (cmd.hasOption(ENUMERATE_KEYS_OPTION)) {
        JsonTransformer.keysToJson(
            null,
            iterToStream(new KeyIterator(desc, metadata)),
            cmd.hasOption(RAW_TIMESTAMPS),
            metadata,
            System.out);
      } else {
        SSTableReader sstable = SSTableReader.openNoValidation(desc, metadata);
        IPartitioner partitioner = sstable.getPartitioner();
        final ISSTableScanner currentScanner;
        if ((keys != null) && (keys.length > 0)) {
          List<AbstractBounds<PartitionPosition>> bounds =
              Arrays.stream(keys)
                  .filter(key -> !excludes.contains(key))
                  .map(metadata.getKeyValidator()::fromString)
                  .map(partitioner::decorateKey)
                  .sorted()
                  .map(DecoratedKey::getToken)
                  .map(token -> new Bounds<>(token.minKeyBound(), token.maxKeyBound()))
                  .collect(Collectors.toList());
          currentScanner = sstable.getScanner(bounds.iterator());
        } else {
          currentScanner = sstable.getScanner();
        }
        Stream<UnfilteredRowIterator> partitions =
            iterToStream(currentScanner)
                .filter(
                    i ->
                        excludes.isEmpty()
                            || !excludes.contains(
                                metadata.getKeyValidator().getString(i.partitionKey().getKey())));
        if (cmd.hasOption(DEBUG_OUTPUT_OPTION)) {
          AtomicLong position = new AtomicLong();
          partitions.forEach(
              partition -> {
                position.set(currentScanner.getCurrentPosition());

                if (!partition.partitionLevelDeletion().isLive()) {
                  System.out.println(
                      "["
                          + metadata.getKeyValidator().getString(partition.partitionKey().getKey())
                          + "]@"
                          + position.get()
                          + " "
                          + partition.partitionLevelDeletion());
                }
                if (!partition.staticRow().isEmpty()) {
                  System.out.println(
                      "["
                          + metadata.getKeyValidator().getString(partition.partitionKey().getKey())
                          + "]@"
                          + position.get()
                          + " "
                          + partition.staticRow().toString(metadata, true));
                }
                partition.forEachRemaining(
                    row -> {
                      System.out.println(
                          "["
                              + metadata
                                  .getKeyValidator()
                                  .getString(partition.partitionKey().getKey())
                              + "]@"
                              + position.get()
                              + " "
                              + row.toString(metadata, false, true));
                      position.set(currentScanner.getCurrentPosition());
                    });
              });
        } else {
          JsonTransformer.toJson(
              currentScanner, partitions, cmd.hasOption(RAW_TIMESTAMPS), metadata, System.out);
        }
      }
    } catch (IOException e) {
      // throwing exception outside main with broken pipe causes windows cmd to hang
      e.printStackTrace(System.err);
    }

    System.exit(0);
  }
Example #13
0
  /*
   *main function
   */
  public static void main(String args[]) {
    String title1 = " ";
    String publisher_name1 = " ";
    String published_date1 = "";
    String content1 = "";
    int n = 0;
    String str = "";

    // try block starts here
    try {
      Options option = new Options();
      option.addOption("hp", false, "help");
      option.addOption("hlp", false, "help");
      CommandLineParser parser = new DefaultParser();
      CommandLine cmd = parser.parse(option, args);
      // str=cmd.getArgs()[0];

      /*prints the help message here*/
      if ((cmd.hasOption("hp")) || (cmd.hasOption("hlp"))) {
        System.out.println(
            "this program gets an argument through commandline 'n' number of books. then reads n books from standard input"
                + "and prints them on standard output");

        if (args.length <= 1) {

          // exit the program
          System.exit(0);
        }
      } // close outer if block
      else {
        if (args.length < 1) {

          // if user not gives option  and value of n then prints this message.
          System.out.println(
              "this program gets an argument through commandline 'n' number of books. then reads n books from standard input"
                  + "and prints them on standard output");
          System.exit(0);
        }
      } // close the  outer else block

      str = cmd.getArgs()[0];
      n = Integer.parseInt(str);

    } catch (Exception e) {
      // System.exit(0);
      // if any erroy occurs it catches here
      System.out.println(e);
    }

    int num = 0;
    Scanner sc1 = new Scanner(System.in); /*create an instance of scanner object*/
    Scanner sc = new Scanner(System.in);

    CmdBook b[] = new CmdBook[n]; /* create arraylist of objects of class Book*/
    for (int i = 0; i < n; i++) {

      // initialise the array of objects
      b[i] = new CmdBook();
    }
    for (int i = 0; i < n; i++) {

      /* take input from  standard input*/
      System.out.println("enter the title of the book:");
      title1 = sc.next();

      System.out.println("enter the name of publisher");
      publisher_name1 = sc.next();

      System.out.println("enter published date");
      published_date1 = sc.next();

      System.out.println("how many authors are");
      num = sc1.nextInt();

      ArrayList<String> author_name1 =
          new ArrayList<String>(); /*create an ArrayList author_name1 */

      for (int j = 0; j < num; j++) {

        /*take input */
        System.out.println("enter authorname" + (j + 1));
        author_name1.add(sc.next());
      }

      System.out.println("enter contents of book:");
      content1 = sc.next();

      /* call the method bookdetail with auguments which contains from
       *  standard input
       */

      b[i].bookdetail(title1, publisher_name1, published_date1, content1, author_name1);
    }
    for (int i = 0; i < n; i++) {

      /*call the methode printbook withe no aurgument*/
      b[i].printbook();
    }
  } /*end of main function here*/
Example #14
0
  private void loadArgs(String[] args) throws ParameterProblem {

    logger.debug("Parsing command line arguments");
    final CommandLineParser parser = new PosixParser();

    final Opts opts = new Opts();
    final CommandLine line;
    try {
      line = parser.parse(opts.getOptions(), args);
    } catch (ParseException e) {
      throw new ParameterProblem(e.getMessage(), e);
    }

    // figure action first
    AdminAction theAction = null;
    for (AdminAction a : AdminAction.values()) {
      if (line.hasOption(a.option())) {
        if (theAction == null) {
          theAction = a;
        } else {
          throw new ParameterProblem("You may only specify a single action");
        }
      }
    }

    if (theAction == null) {
      throw new ParameterProblem("You must specify an action");
    }

    this.action = theAction;
    logger.debug("Action: " + theAction);

    // short circuit for --help arg
    if (theAction == AdminAction.Help) {
      return;
    }

    // then action-specific arguments
    if (theAction == AdminAction.AddNodes || theAction == AdminAction.UpdateNodes) {
      this.hosts = parseHosts(line.getOptionValue(theAction.option()));

      if (line.hasOption(Opts.MEMORY)) {
        final String memString = line.getOptionValue(Opts.MEMORY);
        if (memString == null || memString.trim().length() == 0) {
          throw new ParameterProblem("Node memory value is empty");
        }
        this.nodeMemory = parseMemory(memString);
        this.nodeMemoryConfigured = true;
      }

      if (line.hasOption(Opts.NETWORKS)) {
        this.nodeNetworks = line.getOptionValue(Opts.NETWORKS);
      }

      if (line.hasOption(Opts.POOL)) {
        String pool = line.getOptionValue(Opts.POOL);
        if (pool == null || pool.trim().length() == 0) {
          throw new ParameterProblem("Node pool value is empty");
        }
        this.nodePool = pool.trim();
      }

      final boolean active = line.hasOption(Opts.ACTIVE);
      final boolean inactive = line.hasOption(Opts.INACTIVE);

      if (active && inactive) {
        throw new ParameterProblem(
            "You cannot specify both " + Opts.ACTIVE_LONG + " and " + Opts.INACTIVE_LONG);
      }

      if (active) {
        this.nodeActiveConfigured = true;
      }
      if (inactive) {
        this.nodeActive = false;
        this.nodeActiveConfigured = true;
      }

    } else if (theAction == AdminAction.RemoveNodes) {
      this.hosts = parseHosts(line.getOptionValue(theAction.option()));
    } else if (theAction == AdminAction.ListNodes) {
      final String hostArg = line.getOptionValue(AdminAction.ListNodes.option());
      if (hostArg != null) {
        this.hosts = parseHosts(hostArg);
      }
    } else if (theAction == AdminAction.PoolAvailability) {
      if (line.hasOption(Opts.POOL)) {
        final String pool = line.getOptionValue(Opts.POOL);
        if (pool == null || pool.trim().length() == 0) {
          throw new ParameterProblem("Pool name value is empty");
        }
        this.nodePool = pool;
      }
      if (line.hasOption(Opts.FREE)) {
        this.inUse = RemoteNodeManagement.FREE_ENTRIES;
      }
      if (line.hasOption(Opts.USED)) {
        this.inUse = RemoteNodeManagement.USED_ENTRIES;
      }
    }

    // finally everything else
    if (!line.hasOption(Opts.CONFIG)) {
      throw new ParameterProblem(Opts.CONFIG_LONG + " option is required");
    }
    String config = line.getOptionValue(Opts.CONFIG);
    if (config == null || config.trim().length() == 0) {
      throw new ParameterProblem("Config file path is invalid");
    }
    super.configPath = config.trim();

    final boolean batchMode = line.hasOption(Opts.BATCH);
    final boolean json = line.hasOption(Opts.JSON);

    final Reporter.OutputMode mode;
    if (batchMode && json) {
      throw new ParameterProblem(
          "You cannot specify both " + Opts.BATCH_LONG + " and " + Opts.JSON_LONG);
    } else if (batchMode) {
      mode = Reporter.OutputMode.Batch;
    } else if (json) {
      mode = Reporter.OutputMode.Json;
    } else {
      mode = Reporter.OutputMode.Friendly;
    }

    final String[] fields;
    if (line.hasOption(Opts.REPORT)) {
      fields = parseFields(line.getOptionValue(Opts.REPORT), theAction);
    } else {
      fields = theAction.fields();
    }

    String delimiter = null;
    if (line.hasOption(Opts.DELIMITER)) {
      delimiter = line.getOptionValue(Opts.DELIMITER);
    }

    this.reporter = new Reporter(mode, fields, delimiter);

    if (line.hasOption(Opts.OUTPUT)) {
      final String filename = line.getOptionValue(Opts.OUTPUT);
      final File f = new File(filename);
      try {
        this.outStream = new FileOutputStream(f);
      } catch (FileNotFoundException e) {
        throw new ParameterProblem(
            "Specified output file could not be opened for writing: " + f.getAbsolutePath(), e);
      }
    } else {
      this.outStream = System.out;
    }

    final List leftovers = line.getArgList();
    if (leftovers != null && !leftovers.isEmpty()) {
      throw new ParameterProblem(
          "There are unrecognized arguments, check -h to make "
              + "sure you are doing the intended thing: "
              + leftovers.toString());
    }
  }
    public static LoaderOptions parseArgs(String cmdArgs[]) {
      CommandLineParser parser = new GnuParser();
      CmdLineOptions options = getCmdLineOptions();
      try {
        CommandLine cmd = parser.parse(options, cmdArgs, false);

        if (cmd.hasOption(HELP_OPTION)) {
          printUsage(options);
          System.exit(0);
        }

        String[] args = cmd.getArgs();
        if (args.length == 0) {
          System.err.println("Missing sstable directory argument");
          printUsage(options);
          System.exit(1);
        }

        if (args.length > 1) {
          System.err.println("Too many arguments");
          printUsage(options);
          System.exit(1);
        }

        String dirname = args[0];
        File dir = new File(dirname);

        if (!dir.exists()) errorMsg("Unknown directory: " + dirname, options);

        if (!dir.isDirectory()) errorMsg(dirname + " is not a directory", options);

        LoaderOptions opts = new LoaderOptions(dir);

        opts.debug = cmd.hasOption(DEBUG_OPTION);
        opts.verbose = cmd.hasOption(VERBOSE_OPTION);
        opts.noProgress = cmd.hasOption(NOPROGRESS_OPTION);

        if (cmd.hasOption(RPC_PORT_OPTION))
          opts.rpcPort = Integer.parseInt(cmd.getOptionValue(RPC_PORT_OPTION));

        if (cmd.hasOption(USER_OPTION)) opts.user = cmd.getOptionValue(USER_OPTION);

        if (cmd.hasOption(PASSWD_OPTION)) opts.passwd = cmd.getOptionValue(PASSWD_OPTION);

        if (cmd.hasOption(INITIAL_HOST_ADDRESS_OPTION)) {
          String[] nodes = cmd.getOptionValue(INITIAL_HOST_ADDRESS_OPTION).split(",");
          try {
            for (String node : nodes) {
              opts.hosts.add(InetAddress.getByName(node.trim()));
            }
          } catch (UnknownHostException e) {
            errorMsg("Unknown host: " + e.getMessage(), options);
          }

        } else {
          System.err.println("Initial hosts must be specified (-d)");
          printUsage(options);
          System.exit(1);
        }

        if (cmd.hasOption(IGNORE_NODES_OPTION)) {
          String[] nodes = cmd.getOptionValue(IGNORE_NODES_OPTION).split(",");
          try {
            for (String node : nodes) {
              opts.ignores.add(InetAddress.getByName(node.trim()));
            }
          } catch (UnknownHostException e) {
            errorMsg("Unknown host: " + e.getMessage(), options);
          }
        }

        // try to load config file first, so that values can be rewritten with other option values.
        // otherwise use default config.
        Config config;
        if (cmd.hasOption(CONFIG_PATH)) {
          File configFile = new File(cmd.getOptionValue(CONFIG_PATH));
          if (!configFile.exists()) {
            errorMsg("Config file not found", options);
          }
          config = new YamlConfigurationLoader().loadConfig(configFile.toURI().toURL());
        } else {
          config = new Config();
        }
        opts.storagePort = config.storage_port;
        opts.sslStoragePort = config.ssl_storage_port;
        opts.throttle = config.stream_throughput_outbound_megabits_per_sec;
        opts.encOptions = config.client_encryption_options;
        opts.serverEncOptions = config.server_encryption_options;

        if (cmd.hasOption(THROTTLE_MBITS)) {
          opts.throttle = Integer.parseInt(cmd.getOptionValue(THROTTLE_MBITS));
        }

        if (cmd.hasOption(SSL_TRUSTSTORE)) {
          opts.encOptions.truststore = cmd.getOptionValue(SSL_TRUSTSTORE);
        }

        if (cmd.hasOption(SSL_TRUSTSTORE_PW)) {
          opts.encOptions.truststore_password = cmd.getOptionValue(SSL_TRUSTSTORE_PW);
        }

        if (cmd.hasOption(SSL_KEYSTORE)) {
          opts.encOptions.keystore = cmd.getOptionValue(SSL_KEYSTORE);
          // if a keystore was provided, lets assume we'll need to use it
          opts.encOptions.require_client_auth = true;
        }

        if (cmd.hasOption(SSL_KEYSTORE_PW)) {
          opts.encOptions.keystore_password = cmd.getOptionValue(SSL_KEYSTORE_PW);
        }

        if (cmd.hasOption(SSL_PROTOCOL)) {
          opts.encOptions.protocol = cmd.getOptionValue(SSL_PROTOCOL);
        }

        if (cmd.hasOption(SSL_ALGORITHM)) {
          opts.encOptions.algorithm = cmd.getOptionValue(SSL_ALGORITHM);
        }

        if (cmd.hasOption(SSL_STORE_TYPE)) {
          opts.encOptions.store_type = cmd.getOptionValue(SSL_STORE_TYPE);
        }

        if (cmd.hasOption(SSL_CIPHER_SUITES)) {
          opts.encOptions.cipher_suites = cmd.getOptionValue(SSL_CIPHER_SUITES).split(",");
        }

        if (cmd.hasOption(TRANSPORT_FACTORY)) {
          ITransportFactory transportFactory =
              getTransportFactory(cmd.getOptionValue(TRANSPORT_FACTORY));
          configureTransportFactory(transportFactory, opts);
          opts.transportFactory = transportFactory;
        }

        return opts;
      } catch (ParseException | ConfigurationException | MalformedURLException e) {
        errorMsg(e.getMessage(), options);
        return null;
      }
    }
  public static void main(String[] args) {
    String logConfig = System.getProperty("log-config");
    if (logConfig == null) {
      logConfig = "log-config.txt";
    }

    Options options = new Options();
    options.addOption("h", "help", false, "print this message");
    options.addOption("v", "version", false, "output version information and exit");
    options.addOption(OptionBuilder.withDescription("trace mode").withLongOpt("trace").create());
    options.addOption(OptionBuilder.withDescription("debug mode").withLongOpt("debug").create());
    options.addOption(OptionBuilder.withDescription("info mode").withLongOpt("info").create());
    options.addOption(
        OptionBuilder.withArgName("file")
            .hasArg()
            .withDescription(
                "file from which to read the labelled training set in tabular format (reference annotation)")
            .isRequired()
            .withLongOpt("labelled")
            .create("l"));
    options.addOption(
        OptionBuilder.withArgName("file")
            .hasArg()
            .withDescription(
                "file from which to read the translated unlabelled training set in tabular format (one sentence per line, including id)")
            .isRequired()
            .withLongOpt("unlabelled")
            .create("u"));
    options.addOption(
        OptionBuilder.withArgName("file")
            .hasArg()
            .withDescription(
                "file from which to read the translated roles (phrase table: source language \\t target language)")
            .isRequired()
            .withLongOpt("roles")
            .create("r"));
    options.addOption(
        OptionBuilder.withArgName("file")
            .hasArg()
            .withDescription(
                "file in which to write the translated labelled training set in tabular format (aligned with the reference annotation)")
            .isRequired()
            .withLongOpt("output")
            .create("o"));
    options.addOption(
        OptionBuilder.withArgName("int")
            .hasArg()
            .withDescription("example from which to start from")
            .withLongOpt("start")
            .create());

    CommandLineParser parser = new PosixParser();
    CommandLine commandLine = null;

    try {
      commandLine = parser.parse(options, args);

      Properties defaultProps = new Properties();
      try {
        defaultProps.load(new InputStreamReader(new FileInputStream(logConfig), "UTF-8"));
      } catch (Exception e) {
        defaultProps.setProperty("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender");
        defaultProps.setProperty(
            "log4j.appender.stdout.layout.ConversionPattern", "[%t] %-5p (%F:%L) - %m %n");
        defaultProps.setProperty("log4j.appender.stdout.layout", "org.apache.log4j.PatternLayout");
        defaultProps.setProperty("log4j.appender.stdout.Encoding", "UTF-8");
      }

      if (commandLine.hasOption("trace")) {
        defaultProps.setProperty("log4j.rootLogger", "trace,stdout");
      } else if (commandLine.hasOption("debug")) {
        defaultProps.setProperty("log4j.rootLogger", "debug,stdout");
      } else if (commandLine.hasOption("info")) {
        defaultProps.setProperty("log4j.rootLogger", "info,stdout");
      } else {
        if (defaultProps.getProperty("log4j.rootLogger") == null) {
          defaultProps.setProperty("log4j.rootLogger", "info,stdout");
        }
      }
      PropertyConfigurator.configure(defaultProps);

      if (commandLine.hasOption("help") || commandLine.hasOption("version")) {
        throw new ParseException("");
      }

      File labelled = new File(commandLine.getOptionValue("labelled"));
      File unlabelled = new File(commandLine.getOptionValue("unlabelled"));
      File roles = new File(commandLine.getOptionValue("roles"));
      File output = new File(commandLine.getOptionValue("output"));
      try {
        if (commandLine.hasOption("start")) {
          int start = Integer.parseInt(commandLine.getOptionValue("start"));
          new AnnotationMigration(labelled, unlabelled, roles, output, start);
        } else {
          new AnnotationMigration(labelled, unlabelled, roles, output);
        }

      } catch (IOException e) {
        logger.error(e);
      }

    } catch (ParseException exp) {
      if (exp.getMessage().length() > 0) {
        System.err.println("Parsing failed: " + exp.getMessage() + "\n");
      }
      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp(
          200,
          "java -Dfile.encoding=UTF-8 -cp dist/dirha.jar org.fbk.cit.hlt.dirha.AnnotationMigration",
          "\n",
          options,
          "\n",
          true);
      System.exit(1);
    }
  }
Example #17
0
  public static void main(String[] args) throws Exception {
    String inputFileName = "";
    String inputFileName2 = "";
    String outputFileName = "";
    String solrServerHost = "";
    String keepListFileName = "";
    String filterListFileName = "";
    String searchTerm = "";
    HashMap<String, String> keepGrayList = new HashMap<String, String>();
    HashMap<String, String> filterGrayList = new HashMap<String, String>();
    boolean useAlias = false;

    CommandLineParser parser = new GnuParser();
    Options options = createCLIOptions();
    try {
      CommandLine line = parser.parse(options, args);

      if (line.hasOption("f1")) {
        // get the input file
        inputFileName = line.getOptionValue("f1");
      }
      if (line.hasOption("f2")) {
        inputFileName2 = line.getOptionValue("f2");
      }
      if (line.hasOption("o")) {
        // get the output file
        outputFileName = line.getOptionValue("o");
      }
      if (line.hasOption("s")) {
        // get the server host name
        solrServerHost = line.getOptionValue("s");
      }
      if (line.hasOption("term")) {
        searchTerm = line.getOptionValue("term");
      }
      if (line.hasOption("a")) {
        useAlias = true;
      }
      if (line.hasOption("k")) {
        keepListFileName = line.getOptionValue("k");
      }
      if (line.hasOption("r")) {
        filterListFileName = line.getOptionValue("r");
      }
    } catch (ParseException exp) {
      log.warning("Command line parsing failed.  Reason:" + exp.getMessage());
      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp("pubcrawl", options);
      System.exit(1);
    }

    if (isEmpty(outputFileName) || isEmpty(inputFileName) && isEmpty(searchTerm)) {
      // missing required elements, print usage and exit
      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp("pubcrawl", options);
      System.exit(1);
    }

    if (!isEmpty(keepListFileName)) {
      // need to load the keepList hashmap
      FileReader inputReader = new FileReader(keepListFileName);
      BufferedReader bufReader = new BufferedReader(inputReader);
      String keepTerm = bufReader.readLine();
      while (keepTerm != null) {
        String[] keepInfoArr = keepTerm.trim().split("\t");
        keepGrayList.put(keepInfoArr[0].toLowerCase(), keepInfoArr[1]);
        keepTerm = bufReader.readLine();
      }
      bufReader.close();
    }

    log.info("loading filterlist filename");
    if (!isEmpty(filterListFileName)) {
      // need to load the filterlist hashmap
      FileReader inputReader = new FileReader(filterListFileName);
      BufferedReader bufReader = new BufferedReader(inputReader);
      String filterTerm = bufReader.readLine();
      while (filterTerm != null) {
        String[] filterInfoArr = filterTerm.trim().split("\t");
        filterGrayList.put(filterInfoArr[0].toLowerCase(), filterInfoArr[1]);
        filterTerm = bufReader.readLine();
      }
      bufReader.close();
    }

    SolrServer server = getSolrServer(solrServerHost);

    String logname = outputFileName + "_log.out";
    // create output files
    FileWriter logFileStream = new FileWriter(logname);
    BufferedWriter logFileOut = new BufferedWriter(logFileStream);
    FileWriter dataResultsStream = new FileWriter(outputFileName);
    BufferedWriter dataResultsOut = new BufferedWriter(dataResultsStream);

    final Map<String, Integer> singleCountMap = new HashMap<String, Integer>();
    final List<String> term2List = new ArrayList<String>();

    // now load the appropriate list of gene terms  - if the second file name wasn't entered
    if (isEmpty(inputFileName2)) {
      String sql = "Select term1,count from singletermcount";
      if (useAlias) {
        sql = "Select alias,count from singletermcount_alias";
      }

      JdbcTemplate jdbcTemplate = getJdbcTemplate();
      jdbcTemplate.query(
          sql,
          new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
              while (rs.next()) {
                String geneName = rs.getString(1).trim();
                int count = rs.getInt(2);
                singleCountMap.put(geneName.toLowerCase(), count);
                if (count > 0) {
                  term2List.add(geneName.toLowerCase());
                }
              }
              return null;
            }
          });
    } else { // have a second input file, so read the file in and put those as the terms in the
             // term2List, set the SingleCountMap to empty
      FileReader inputReader2 = new FileReader(inputFileName2);
      BufferedReader bufReader2 = new BufferedReader(inputReader2);
      String searchTerm2 = bufReader2.readLine();
      while (searchTerm2 != null) {
        term2List.add(searchTerm2.trim().toLowerCase());
        searchTerm2 = bufReader2.readLine();
      }
    }

    Long totalDocCount = getTotalDocCount(server);
    logFileOut.write("Total doc count: " + totalDocCount);
    Pubcrawl p = new Pubcrawl();
    if (isEmpty(inputFileName)) { // entered term option, just have one to calculate
      SearchTermAndList searchTermArray = getTermAndTermList(searchTerm.trim(), useAlias, false);
      Long searchTermCount =
          getTermCount(server, singleCountMap, searchTermArray, filterGrayList, keepGrayList);

      ExecutorService pool = Executors.newFixedThreadPool(32);
      Set<Future<NGDItem>> set = new HashSet<Future<NGDItem>>();
      Date firstTime = new Date();

      for (String secondTerm : term2List) {
        SearchTermAndList secondTermArray = getTermAndTermList(secondTerm, useAlias, false);
        long secondTermCount =
            getTermCount(server, singleCountMap, secondTermArray, filterGrayList, keepGrayList);
        Callable<NGDItem> callable =
            p
            .new SolrCallable(
                searchTermArray,
                secondTermArray,
                searchTermCount,
                secondTermCount,
                server,
                useAlias,
                filterGrayList,
                keepGrayList,
                totalDocCount);
        Future<NGDItem> future = pool.submit(callable);
        set.add(future);
      }

      for (Future<NGDItem> future : set) {
        dataResultsOut.write(future.get().printItem());
      }

      Date secondTime = new Date();
      logFileOut.write(
          "First set of queries took "
              + (secondTime.getTime() - firstTime.getTime()) / 1000
              + " seconds.\n");
      logFileOut.flush();
      logFileOut.close();
      dataResultsOut.flush();
      dataResultsOut.close();
      pool.shutdown();

    } else {

      FileReader inputReader = new FileReader(inputFileName);
      BufferedReader bufReader = new BufferedReader(inputReader);
      String fileSearchTerm = bufReader.readLine();
      SearchTermAndList searchTermArray = getTermAndTermList(fileSearchTerm, useAlias, false);
      Long searchTermCount =
          getTermCount(server, singleCountMap, searchTermArray, filterGrayList, keepGrayList);

      // do this once with a lower amount of threads, in case we are running on a server where new
      // caching is taking place
      ExecutorService pool = Executors.newFixedThreadPool(32);
      List<Future<NGDItem>> set = new ArrayList<Future<NGDItem>>();
      long firstTime = currentTimeMillis();
      int count = 0;

      for (String secondTerm : term2List) {
        count++;
        SearchTermAndList secondTermArray = getTermAndTermList(secondTerm, useAlias, false);
        long secondTermCount =
            getTermCount(server, singleCountMap, secondTermArray, filterGrayList, keepGrayList);
        Callable<NGDItem> callable =
            p
            .new SolrCallable(
                searchTermArray,
                secondTermArray,
                searchTermCount,
                secondTermCount,
                server,
                useAlias,
                filterGrayList,
                keepGrayList,
                totalDocCount);
        Future<NGDItem> future = pool.submit(callable);
        set.add(future);

        if (count > 5000) {
          for (Future<NGDItem> futureItem : set) {
            dataResultsOut.write(futureItem.get().printItem());
            futureItem = null;
          }
          count = 0;
          set.clear();
        }
      }

      for (Future<NGDItem> future : set) {
        dataResultsOut.write(future.get().printItem());
      }

      long secondTime = currentTimeMillis();
      logFileOut.write(
          "First set of queries took " + (secondTime - firstTime) / 1000 + " seconds.\n");
      logFileOut.flush();
      set.clear();

      pool = Executors.newFixedThreadPool(32);
      fileSearchTerm = bufReader.readLine();
      count = 0;
      while (fileSearchTerm != null) {
        searchTermArray = getTermAndTermList(fileSearchTerm, useAlias, false);
        searchTermCount =
            getTermCount(server, singleCountMap, searchTermArray, filterGrayList, keepGrayList);
        secondTime = currentTimeMillis();
        for (String secondTerm : term2List) {
          SearchTermAndList secondTermArray = getTermAndTermList(secondTerm, useAlias, false);
          long secondTermCount =
              getTermCount(server, singleCountMap, secondTermArray, filterGrayList, keepGrayList);
          Callable<NGDItem> callable =
              p
              .new SolrCallable(
                  searchTermArray,
                  secondTermArray,
                  searchTermCount,
                  secondTermCount,
                  server,
                  useAlias,
                  filterGrayList,
                  keepGrayList,
                  totalDocCount);
          Future<NGDItem> future = pool.submit(callable);
          set.add(future);
          count++;
          if (count > 5000) {
            for (Future<NGDItem> futureItem : set) {
              dataResultsOut.write(futureItem.get().printItem());
              futureItem = null;
            }
            count = 0;
            set.clear();
          }
        }

        for (Future<NGDItem> future : set) {
          dataResultsOut.write(future.get().printItem());
          future = null;
        }

        logFileOut.write("Query took " + (currentTimeMillis() - secondTime) / 1000 + " seconds.\n");
        logFileOut.flush();
        set.clear();
        fileSearchTerm = bufReader.readLine();
      }

      long fourthTime = currentTimeMillis();
      logFileOut.write("Final time: " + (fourthTime - firstTime) / 1000 + " seconds.\n");
      bufReader.close();
      logFileOut.flush();
      logFileOut.close();
      dataResultsOut.flush();
      dataResultsOut.close();
      pool.shutdown();
    }
    System.exit(0);
  }
Example #18
0
  public static void main(String[] args) throws Exception {
    boolean isInteractive = false;
    classUrl = MynaInstaller.class.getResource("MynaInstaller.class").toString();
    isJar = (classUrl.indexOf("jar") == 0);
    if (!isJar) {
      System.err.println("Installer can only be run from inside a Myna distribution war file");
      System.exit(1);
    }

    Thread.sleep(1000);

    Console console = System.console();
    String response = null;
    CommandLineParser parser = new PosixParser();

    // create the Options
    Options options = new Options();
    options.addOption(
        "c", "context", true, "Webapp context. Must Start with \"/\" Default: " + webctx);
    options.addOption("h", "help", false, "Displays help.");
    options.addOption(
        "w",
        "webroot",
        true,
        "Webroot to use. Will be created if webroot/WEB-INF does not exist. Default: " + webroot);
    options.addOption(
        "l",
        "logfile",
        true,
        "Log file to use. Will be created if it does not exist. Default: ./<context>.log");
    options.addOption(
        "s",
        "servername",
        true,
        "Name of this instance. Will also be the name of the init script. Defaults to either \"myna\" or the value of <context> if defined");
    // options.addOption( "P", "purpose", true, "Purpose of the Server, such as DEV,PROD,TRAIN, etc.
    // Defaults to DEV" );

    options.addOption("p", "port", true, "HTTP port. Set to 0 to disable HTTP. Default: " + port);
    options.addOption(
        "sp", "ssl-port", true, "SSL (HTTPS) port. Set to 0 to disable SSL, Default: 0");

    options.addOption(
        "ks", "keystore", true, "keystore path. Default: <webroot>/WEB-INF/myna/myna_keystore");
    options.addOption("ksp", "ks-pass", true, "keystore password. Default: " + ksPass);
    options.addOption("ksa", "ks-alias", true, "certificate alias. Default: " + ksAlias);

    modeOptions.add("upgrade");
    modeOptions.add("install");
    options.addOption(
        "m",
        "mode",
        true,
        "Mode: one of "
            + modeOptions.toString()
            + ". \n"
            + "\"upgrade\": Upgrades myna installation in webroot and exits. "
            + "\"install\": Unpacks to webroot, and installs startup files");
    options.addOption(
        "u",
        "user",
        true,
        "User to own and run the Myna installation. Only applies to unix installs. Default: nobody");

    HelpFormatter formatter = new HelpFormatter();

    String cmdSyntax = "java -jar myna-X.war -m <mode> [options]";
    try {
      CommandLine line = parser.parse(options, args);
      Option option;
      if (args.length == 0) {
        formatter.printHelp(cmdSyntax, options);
        response = console.readLine("\nContinue with Interactive Install? (y/N)");
        if (response.toLowerCase().equals("y")) {
          isInteractive = true;

        } else System.exit(1);
      }
      // Help
      if (line.hasOption("help")) {
        formatter.printHelp(cmdSyntax, options);
        System.exit(1);
      }
      // mode
      if (line.hasOption("mode")) {
        mode = line.getOptionValue("mode");
        if (!modeOptions.contains(mode)) {
          System.err.println(
              "Invalid Arguments.  Reason: Mode must be in " + modeOptions.toString());
          formatter.printHelp(cmdSyntax, options);
          System.exit(1);
        }
      } else if (isInteractive) {
        option = options.getOption("mode");
        console.printf("\n" + option.getDescription());

        do {
          response = console.readLine("\nEnter " + option.getLongOpt() + "(" + mode + "): ");
          if (!response.isEmpty()) mode = response;
        } while (!modeOptions.contains(mode));
      }
      // webroot
      if (line.hasOption("webroot")) {
        webroot = line.getOptionValue("webroot");
      } else if (isInteractive) {
        option = options.getOption("webroot");
        console.printf("\n" + option.getDescription());
        response = console.readLine("\nEnter " + option.getLongOpt() + "(" + webroot + "): ");
        if (!response.isEmpty()) webroot = response;
      }
      // port
      if (line.hasOption("port")) {
        port = Integer.parseInt(line.getOptionValue("port"));
      } else if (isInteractive && mode.equals("install")) {
        option = options.getOption("port");
        console.printf("\n" + option.getDescription());
        response = console.readLine("\nEnter " + option.getLongOpt() + "(" + port + "): ");
        if (!response.isEmpty()) port = Integer.parseInt(response);
      }
      // context
      if (line.hasOption("context")) {
        webctx = line.getOptionValue("context");

      } else if (isInteractive && mode.equals("install")) {
        option = options.getOption("context");
        console.printf("\n" + option.getDescription());
        response = console.readLine("\nEnter " + option.getLongOpt() + "(" + webctx + "): ");
        if (!response.isEmpty()) webctx = response;
      }
      if (!webctx.startsWith("/")) {
        webctx = "/" + webctx;
      }
      // servername (depends on context)
      if (!webctx.equals("/")) {
        serverName = webctx.substring(1);
      }
      if (line.hasOption("servername")) {
        serverName = line.getOptionValue("servername");
      } else if (isInteractive && mode.equals("install")) {
        option = options.getOption("servername");
        console.printf("\n" + option.getDescription());
        response = console.readLine("\nEnter " + option.getLongOpt() + "(" + serverName + "): ");
        if (!response.isEmpty()) serverName = response;
      }
      // user
      if (line.hasOption("user")) {
        user = line.getOptionValue("user");
      } else if (isInteractive && mode.equals("install")) {
        option = options.getOption("user");
        console.printf("\n" + option.getDescription());
        response = console.readLine("\nEnter " + option.getLongOpt() + "(" + user + "): ");
        if (!response.isEmpty()) user = response;
      }
      // logfile
      logFile = "myna.log";
      if (!webctx.equals("/")) {
        logFile = webctx.substring(1) + ".log";
      }
      if (line.hasOption("logfile")) {
        logFile = line.getOptionValue("logfile");
      } else if (isInteractive && mode.equals("install")) {
        option = options.getOption("logfile");
        console.printf("\n" + option.getDescription());
        response = console.readLine("\nEnter " + option.getLongOpt() + "path(" + logFile + "): ");
        if (!response.isEmpty()) logFile = response;
      }

      // ssl-port
      if (line.hasOption("ssl-port")) {
        sslPort = Integer.parseInt(line.getOptionValue("ssl-port"));
      } else if (isInteractive && mode.equals("install")) {
        option = options.getOption("ssl-port");
        console.printf("\n" + option.getDescription());
        response = console.readLine("\nEnter " + option.getLongOpt() + "(" + sslPort + "): ");
        if (!response.isEmpty()) sslPort = Integer.parseInt(response);
      }
      // ks-pass
      if (line.hasOption("ks-pass")) {
        ksPass = line.getOptionValue("ks-pass");
      } else if (isInteractive && mode.equals("install")) {
        option = options.getOption("ks-pass");
        console.printf("\n" + option.getDescription());
        response = console.readLine("\nEnter " + option.getLongOpt() + "(" + ksPass + "): ");
        if (!response.isEmpty()) ksPass = response;
      }
      // ks-alias
      if (line.hasOption("ks-alias")) {
        ksAlias = line.getOptionValue("ks-alias");
      } else if (isInteractive && mode.equals("install")) {
        option = options.getOption("ks-alias");
        console.printf("\n" + option.getDescription());
        response = console.readLine("\nEnter " + option.getLongOpt() + "(" + ksAlias + "): ");
        if (!response.isEmpty()) ksAlias = response;
      }
      // keystore
      String appBase = new File(webroot).getCanonicalPath();
      if (keystore == null) {
        keystore = appBase + "/WEB-INF/myna/myna_keystore";
      }
      if (line.hasOption("keystore")) {
        keystore = line.getOptionValue("keystore");
      } else if (isInteractive && mode.equals("install")) {
        option = options.getOption("keystore");
        console.printf("\n" + option.getDescription());
        response = console.readLine("\nEnter " + option.getLongOpt() + "(" + keystore + "): ");
        if (!response.isEmpty()) keystore = response;
      }

      javaOpts = line.getArgList();
    } catch (ParseException exp) {
      System.err.println("Invalid Arguments.	Reason: " + exp.getMessage());

      formatter.printHelp(cmdSyntax, options);
      System.exit(1);
    }

    if (isInteractive) {
      System.out.println("\nProceeed with the following settings?:\n");
      System.out.println("mode        = " + mode);
      System.out.println("webroot     = " + webroot);
      if (mode.equals("install")) {
        System.out.println("port        = " + port);
        System.out.println("context     = " + webctx);
        System.out.println("servername  = " + serverName);
        System.out.println("user        = "******"logfile     = " + logFile);
        System.out.println("ssl-port    = " + sslPort);
        System.out.println("ks-pass     = "******"ks-alias    = " + ksAlias);
        System.out.println("keystore    = " + keystore);
      }
      response = console.readLine("Continue? (Y/n)");
      if (response.toLowerCase().equals("n")) System.exit(1);
    }
    File wrFile = new File(webroot);
    webroot = wrFile.toString();
    if (mode.equals("install")) {
      adminPassword = console.readLine("\nCreate an Admin password for this installation: ");
    }
    // unpack myna if necessary
    if (!wrFile.exists() || mode.equals("upgrade") || mode.equals("install")) {
      upgrade(wrFile);
    }

    if (mode.equals("install")) {
      File propertiesFile = new File(wrFile.toURI().resolve("WEB-INF/classes/general.properties"));
      FileInputStream propertiesFileIS = new FileInputStream(propertiesFile);
      Properties generalProperties = new Properties();
      generalProperties.load(propertiesFileIS);
      propertiesFileIS.close();
      if (!adminPassword.isEmpty()) {
        org.jasypt.util.password.StrongPasswordEncryptor cryptTool =
            new org.jasypt.util.password.StrongPasswordEncryptor();
        generalProperties.setProperty("admin_password", cryptTool.encryptPassword(adminPassword));
      }
      generalProperties.setProperty("instance_id", serverName);

      generalProperties.store(
          new java.io.FileOutputStream(propertiesFile), "Myna General Properties");

      String javaHome = System.getProperty("java.home");
      webroot = new File(webroot).getCanonicalPath();
      if (serverName.length() == 0) serverName = "myna";
      if (java.lang.System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
        if (!new File(logFile).isAbsolute()) {
          logFile = new File(wrFile.toURI().resolve("WEB-INF/" + logFile)).toString();
        }
        File templateFile =
            new File(
                wrFile.toURI().resolve("WEB-INF/myna/install/windows/update_myna_service.cmd"));

        String initScript =
            FileUtils.readFileToString(templateFile)
                .replaceAll("\\{webctx\\}", webctx)
                .replaceAll("\\{server\\}", Matcher.quoteReplacement(serverName))
                .replaceAll("\\{webroot\\}", Matcher.quoteReplacement(webroot))
                .replaceAll("\\{logfile\\}", Matcher.quoteReplacement(logFile))
                .replaceAll("\\{javahome\\}", Matcher.quoteReplacement(javaHome))
                .replaceAll("\\{port\\}", new Integer(port).toString())
                .replaceAll("\\{sslPort\\}", new Integer(sslPort).toString())
                .replaceAll("\\{keystore\\}", Matcher.quoteReplacement(keystore))
                .replaceAll("\\{ksPass\\}", Matcher.quoteReplacement(ksPass))
                .replaceAll("\\{ksAlias\\}", Matcher.quoteReplacement(ksAlias));

        File scriptFile =
            new File(wrFile.toURI().resolve("WEB-INF/myna/install/update_myna_service.cmd"));

        FileUtils.writeStringToFile(scriptFile, initScript);

        // Runtime.getRuntime().exec("cmd /c start " + scriptFile.toString()).waitFor();

        System.out.println(
            "\nInstalled Service 'Myna App Server " + serverName + "' the following settings:\n");
        System.out.println(
            "\nInit script '" + scriptFile + "' created with the following settings:\n");
        System.out.println("memory=256MB");
        System.out.println("serverName=" + serverName);
        System.out.println("javaHome=" + javaHome);
        System.out.println("context=" + webctx);
        System.out.println("port=" + port);
        System.out.println("myna_home=" + webroot);
        System.out.println("logfile=" + logFile);

        System.out.println("sslPort=" + sslPort);
        System.out.println("keyStore=" + keystore);
        System.out.println("ksPass="******"ksAlias=" + ksAlias);

        System.out.println(
            "\nEdit and and run the command file in " + scriptFile + " to update this service");

      } else {
        String curUser = java.lang.System.getProperty("user.name");
        if (!curUser.equals("root")) {
          System.out.println("Install mode must be run as root.");
          System.exit(1);
        }

        if (!new File(logFile).isAbsolute()) {
          logFile = new File(wrFile.toURI().resolve("WEB-INF/" + logFile)).toString();
        }
        File templateFile =
            new File(wrFile.toURI().resolve("WEB-INF/myna/install/linux/init_script"));
        String initScript =
            FileUtils.readFileToString(templateFile)
                .replaceAll("\\{webctx\\}", webctx)
                .replaceAll("\\{server\\}", serverName)
                .replaceAll("\\{user\\}", user)
                .replaceAll("\\{webroot\\}", webroot)
                .replaceAll("\\{javahome\\}", javaHome)
                .replaceAll("\\{logfile\\}", logFile)
                .replaceAll("\\{port\\}", new Integer(port).toString())
                .replaceAll("\\{sslPort\\}", new Integer(sslPort).toString())
                .replaceAll("\\{keystore\\}", keystore)
                .replaceAll("\\{ksPass\\}", ksPass)
                .replaceAll("\\{ksAlias\\}", ksAlias);

        File scriptFile = new File(wrFile.toURI().resolve("WEB-INF/myna/install/" + serverName));

        FileUtils.writeStringToFile(scriptFile, initScript);

        if (new File("/etc/init.d").exists()) {

          exec("chown  -R " + user + " " + webroot);
          exec("chown root " + scriptFile.toString());
          exec("chmod 700 " + scriptFile.toString());
          exec("cp " + scriptFile.toString() + " /etc/init.d/");

          System.out.println(
              "\nInit script '/etc/init.d/"
                  + serverName
                  + "' created with the following settings:\n");
        } else {
          System.out.println(
              "\nInit script '" + scriptFile + "' created with the following settings:\n");
        }

        System.out.println("user="******"memory=256MB");
        System.out.println("server=" + serverName);
        System.out.println("context=" + webctx);
        System.out.println("port=" + port);
        System.out.println("myna_home=" + webroot);
        System.out.println("logfile=" + logFile);

        System.out.println("sslPort=" + sslPort);
        System.out.println("keyStore=" + keystore);
        System.out.println("ksPass="******"ksAlias=" + ksAlias);

        System.out.println("\nEdit this file to customize startup behavior");
      }
    }
  }
Example #19
0
  public static void main(String[] argv)
      throws IOException, CmdLineParser.UnknownOptionException,
          CmdLineParser.IllegalOptionValueException {

    if (argv.length < 4) {
      System.out.println("Usage: hictools pre <options> <inputFile> <outputFile> <genomeID>");
      System.out.println("  <options>: -d only calculate intra chromosome (diagonal) [false]");
      System.out.println(
          "           : -o calculate densities (observed/expected), write to file [false]");
      System.out.println("           : -t <int> only write cells with count above threshold t [0]");
      System.out.println(
          "           : -c <chromosome ID> only calculate map on specific chromosome");
      System.exit(0);
    }

    Globals.setHeadless(true);

    CommandLineParser parser = new CommandLineParser();
    parser.parse(argv);
    String[] args = parser.getRemainingArgs();

    if (args[0].equals("sort")) {
      AlignmentsSorter.sort(args[1], args[2], null);
    } else if (args[0].equals("pairsToBin")) {
      String ifile = args[1];
      String ofile = args[2];
      String genomeId = args[3];
      List<Chromosome> chromosomes = loadChromosomes(genomeId);
      AsciiToBinConverter.convert(ifile, ofile, chromosomes);
    } else if (args[0].equals("binToPairs")) {
      String ifile = args[1];
      String ofile = args[2];
      AsciiToBinConverter.convertBack(ifile, ofile);
    } else if (args[0].equals("printmatrix")) {
      if (args.length < 5) {
        System.err.println(
            "Usage: hictools printmatrix <observed/oe/pearson> hicFile chr1 chr2 binsize");
        System.exit(-1);
      }
      String type = args[1];
      String file = args[2];
      String chr1 = args[3];
      String chr2 = args[4];
      String binSizeSt = args[5];
      int binSize = 0;
      try {
        binSize = Integer.parseInt(binSizeSt);
      } catch (NumberFormatException e) {
        System.err.println("Integer expected.  Found: " + binSizeSt);
        System.exit(-1);
      }

      dumpMatrix(file, chr1, chr2, binSize, type);

    } else if (args[0].equals("eigenvector")) {
      if (args.length < 4) {
        System.err.println("Usage: hictools eigenvector hicFile chr binsize");
      }
      String file = args[1];
      String chr = args[2];
      String binSizeSt = args[3];
      int binSize = 0;
      try {
        binSize = Integer.parseInt(binSizeSt);
      } catch (NumberFormatException e) {
        System.err.println("Integer expected.  Found: " + binSizeSt);
        System.exit(-1);
      }
      calculateEigenvector(file, chr, binSize);
    } else if (args[0].equals("pre")) {
      String genomeId = "";
      try {
        genomeId = args[3];
      } catch (ArrayIndexOutOfBoundsException e) {
        System.err.println("No genome ID given");
        System.exit(0);
      }
      List<Chromosome> chromosomes = loadChromosomes(genomeId);

      long genomeLength = 0;
      for (Chromosome c : chromosomes) {
        if (c != null) genomeLength += c.getSize();
      }
      chromosomes.set(0, new Chromosome(0, "All", (int) (genomeLength / 1000)));

      String[] tokens = args[1].split(",");
      List<String> files = new ArrayList<String>(tokens.length);

      for (String f : tokens) {
        files.add(f);
      }

      Preprocessor preprocessor = new Preprocessor(new File(args[2]), chromosomes);

      preprocessor.setIncludedChromosomes(parser.getChromosomeOption());
      preprocessor.setCountThreshold(parser.getCountThresholdOption());
      preprocessor.setNumberOfThreads(parser.getThreadedOption());
      preprocessor.setDiagonalsOnly(parser.getDiagonalsOption());
      preprocessor.setLoadDensities(parser.getDensitiesOption());
      preprocessor.preprocess(files);
    }
  }