Exemplo n.º 1
0
  /**
   * Processes the command-line arguments and invokes the export process.
   *
   * @param args The command-line arguments provided to this program.
   * @param initializeServer Indicates whether to initialize the server.
   * @return The error code.
   */
  public int process(String[] args, boolean initializeServer) {
    if (initializeServer) {
      DirectoryServer.bootstrapClient();
    }
    JDKLogging.disableLogging();

    // Create the command-line argument parser for use with this program.
    LDAPConnectionArgumentParser argParser =
        new LDAPConnectionArgumentParser(
            "org.opends.server.tools.TaskInfo",
            INFO_TASKINFO_TOOL_DESCRIPTION.get(),
            false,
            null,
            alwaysSSL);
    argParser.setShortToolDescription(REF_SHORT_DESC_MANAGE_TASKS.get());

    // Initialize all the command-line argument types and register them with the
    // parser.
    try {

      StringArgument propertiesFileArgument =
          new StringArgument(
              "propertiesFilePath",
              null,
              OPTION_LONG_PROP_FILE_PATH,
              false,
              false,
              true,
              INFO_PROP_FILE_PATH_PLACEHOLDER.get(),
              null,
              null,
              INFO_DESCRIPTION_PROP_FILE_PATH.get());
      argParser.addArgument(propertiesFileArgument);
      argParser.setFilePropertiesArgument(propertiesFileArgument);

      BooleanArgument noPropertiesFileArgument =
          new BooleanArgument(
              "noPropertiesFileArgument",
              null,
              OPTION_LONG_NO_PROP_FILE,
              INFO_DESCRIPTION_NO_PROP_FILE.get());
      argParser.addArgument(noPropertiesFileArgument);
      argParser.setNoPropertiesFileArgument(noPropertiesFileArgument);

      task =
          new StringArgument(
              "info",
              'i',
              "info",
              false,
              true,
              INFO_TASK_ID_PLACEHOLDER.get(),
              INFO_TASKINFO_TASK_ARG_DESCRIPTION.get());
      argParser.addArgument(task);

      cancel =
          new StringArgument(
              "cancel",
              'c',
              "cancel",
              false,
              true,
              INFO_TASK_ID_PLACEHOLDER.get(),
              INFO_TASKINFO_TASK_ARG_CANCEL.get());
      argParser.addArgument(cancel);

      summary =
          new BooleanArgument(
              "summary", 's', "summary", INFO_TASKINFO_SUMMARY_ARG_DESCRIPTION.get());
      argParser.addArgument(summary);

      noPrompt = CommonArguments.getNoPrompt();
      argParser.addArgument(noPrompt);

      BooleanArgument displayUsage = CommonArguments.getShowUsage();
      argParser.addArgument(displayUsage);
      argParser.setUsageArgument(displayUsage);
    } catch (ArgumentException ae) {
      LocalizableMessage message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
      println(message);
      return 1;
    }

    try {
      argParser.getArguments().initArgumentsWithConfiguration();
    } catch (ConfigException ce) {
      // Ignore.
    }

    // Parse the command-line arguments provided to this program.
    try {
      argParser.parseArguments(args);
      StaticUtils.checkOnlyOneArgPresent(task, summary, cancel);
    } catch (ArgumentException ae) {
      LocalizableMessage message = ERR_ERROR_PARSING_ARGS.get(ae.getMessage());
      println(message);
      println(argParser.getUsageMessage());
      return 1;
    }

    if (!argParser.usageOrVersionDisplayed()) {
      // Checks the version - if upgrade required, the tool is unusable
      try {
        BuildVersion.checkVersionMismatch();
      } catch (InitializationException e) {
        println(e.getMessageObject());
        return 1;
      }

      try {
        LDAPConnectionConsoleInteraction ui =
            new LDAPConnectionConsoleInteraction(this, argParser.getArguments());

        taskClient = new TaskClient(argParser.connect(ui, getOutputStream(), getErrorStream()));

        if (isMenuDrivenMode()) {

          // Keep prompting the user until they specify quit of
          // there is a fatal exception
          while (true) {
            getOutputStream().println();
            Menu<Void> menu = getSummaryMenu();
            MenuResult<Void> result = menu.run();
            if (result.isQuit()) {
              return 0;
            }
          }

        } else if (task.isPresent()) {
          getOutputStream().println();
          MenuResult<TaskEntry> r = new PrintTaskInfo(task.getValue()).invoke(this);
          if (r.isAgain()) {
            return 1;
          }
        } else if (summary.isPresent()) {
          getOutputStream().println();
          printSummaryTable();
        } else if (cancel.isPresent()) {
          MenuResult<TaskEntry> r = new CancelTask(cancel.getValue()).invoke(this);
          if (r.isAgain()) {
            return 1;
          }
        } else if (!isInteractive()) {
          // no-prompt option
          getOutputStream().println();
          printSummaryTable();
          return 0;
        }

      } catch (LDAPConnectionException lce) {
        println(INFO_TASKINFO_LDAP_EXCEPTION.get(lce.getMessageObject()));
        return 1;
      } catch (Exception e) {
        println(LocalizableMessage.raw(StaticUtils.getExceptionMessage(e)));
        return 1;
      }
    }
    return 0;
  }
Exemplo n.º 2
0
  private int process(
      String[] args, boolean initializeServer, OutputStream outStream, OutputStream errStream) {

    PrintStream out = NullOutputStream.wrapOrNullStream(outStream);
    PrintStream err = NullOutputStream.wrapOrNullStream(errStream);
    JDKLogging.disableLogging();

    // Create the command-line argument parser for use with this program.
    LDAPConnectionArgumentParser argParser =
        createArgParser(
            "org.opends.server.tools.ExportLDIF", INFO_LDIFEXPORT_TOOL_DESCRIPTION.get());
    argParser.setShortToolDescription(REF_SHORT_DESC_EXPORT_LDIF.get());

    // Initialize all the command-line argument types and register them with the
    // parser.
    try {
      configClass =
          new StringArgument(
              "configclass",
              OPTION_SHORT_CONFIG_CLASS,
              OPTION_LONG_CONFIG_CLASS,
              true,
              false,
              true,
              INFO_CONFIGCLASS_PLACEHOLDER.get(),
              ConfigFileHandler.class.getName(),
              null,
              INFO_DESCRIPTION_CONFIG_CLASS.get());
      configClass.setHidden(true);
      argParser.addArgument(configClass);

      configFile =
          new StringArgument(
              "configfile",
              'f',
              "configFile",
              true,
              false,
              true,
              INFO_CONFIGFILE_PLACEHOLDER.get(),
              null,
              null,
              INFO_DESCRIPTION_CONFIG_FILE.get());
      configFile.setHidden(true);
      argParser.addArgument(configFile);

      ldifFile =
          new StringArgument(
              "ldiffile",
              OPTION_SHORT_LDIF_FILE,
              OPTION_LONG_LDIF_FILE,
              true,
              false,
              true,
              INFO_LDIFFILE_PLACEHOLDER.get(),
              null,
              null,
              INFO_LDIFEXPORT_DESCRIPTION_LDIF_FILE.get());
      argParser.addArgument(ldifFile);

      appendToLDIF =
          new BooleanArgument(
              "appendldif", 'a', "appendToLDIF", INFO_LDIFEXPORT_DESCRIPTION_APPEND_TO_LDIF.get());
      argParser.addArgument(appendToLDIF);

      backendID =
          new StringArgument(
              "backendid",
              'n',
              "backendID",
              true,
              false,
              true,
              INFO_BACKENDNAME_PLACEHOLDER.get(),
              null,
              null,
              INFO_LDIFEXPORT_DESCRIPTION_BACKEND_ID.get());
      argParser.addArgument(backendID);

      includeBranchStrings =
          new StringArgument(
              "includebranch",
              'b',
              "includeBranch",
              false,
              true,
              true,
              INFO_BRANCH_DN_PLACEHOLDER.get(),
              null,
              null,
              INFO_LDIFEXPORT_DESCRIPTION_INCLUDE_BRANCH.get());
      argParser.addArgument(includeBranchStrings);

      excludeBranchStrings =
          new StringArgument(
              "excludebranch",
              'B',
              "excludeBranch",
              false,
              true,
              true,
              INFO_BRANCH_DN_PLACEHOLDER.get(),
              null,
              null,
              INFO_LDIFEXPORT_DESCRIPTION_EXCLUDE_BRANCH.get());
      argParser.addArgument(excludeBranchStrings);

      includeAttributeStrings =
          new StringArgument(
              "includeattribute",
              'i',
              "includeAttribute",
              false,
              true,
              true,
              INFO_ATTRIBUTE_PLACEHOLDER.get(),
              null,
              null,
              INFO_LDIFEXPORT_DESCRIPTION_INCLUDE_ATTRIBUTE.get());
      argParser.addArgument(includeAttributeStrings);

      excludeAttributeStrings =
          new StringArgument(
              "excludeattribute",
              'e',
              "excludeAttribute",
              false,
              true,
              true,
              INFO_ATTRIBUTE_PLACEHOLDER.get(),
              null,
              null,
              INFO_LDIFEXPORT_DESCRIPTION_EXCLUDE_ATTRIBUTE.get());
      argParser.addArgument(excludeAttributeStrings);

      includeFilterStrings =
          new StringArgument(
              "includefilter",
              'I',
              "includeFilter",
              false,
              true,
              true,
              INFO_FILTER_PLACEHOLDER.get(),
              null,
              null,
              INFO_LDIFEXPORT_DESCRIPTION_INCLUDE_FILTER.get());
      argParser.addArgument(includeFilterStrings);

      excludeFilterStrings =
          new StringArgument(
              "excludefilter",
              'E',
              "excludeFilter",
              false,
              true,
              true,
              INFO_FILTER_PLACEHOLDER.get(),
              null,
              null,
              INFO_LDIFEXPORT_DESCRIPTION_EXCLUDE_FILTER.get());
      argParser.addArgument(excludeFilterStrings);

      excludeOperationalAttrs =
          new BooleanArgument(
              "excludeoperational",
              'O',
              "excludeOperational",
              INFO_LDIFEXPORT_DESCRIPTION_EXCLUDE_OPERATIONAL.get());
      argParser.addArgument(excludeOperationalAttrs);

      wrapColumn =
          new IntegerArgument(
              "wrapcolumn",
              null,
              "wrapColumn",
              false,
              false,
              true,
              INFO_WRAP_COLUMN_PLACEHOLDER.get(),
              0,
              null,
              true,
              0,
              false,
              0,
              INFO_LDIFEXPORT_DESCRIPTION_WRAP_COLUMN.get());
      argParser.addArgument(wrapColumn);

      compressLDIF =
          new BooleanArgument(
              "compressldif",
              OPTION_SHORT_COMPRESS,
              OPTION_LONG_COMPRESS,
              INFO_LDIFEXPORT_DESCRIPTION_COMPRESS_LDIF.get());
      argParser.addArgument(compressLDIF);

      encryptLDIF =
          new BooleanArgument(
              "encryptldif", 'y', "encryptLDIF", INFO_LDIFEXPORT_DESCRIPTION_ENCRYPT_LDIF.get());
      encryptLDIF.setHidden(true); // See issue #27
      argParser.addArgument(encryptLDIF);

      signHash =
          new BooleanArgument(
              "signhash", 's', "signHash", INFO_LDIFEXPORT_DESCRIPTION_SIGN_HASH.get());
      signHash.setHidden(true); // See issue #28
      argParser.addArgument(signHash);

      displayUsage = CommonArguments.getShowUsage();
      argParser.addArgument(displayUsage);
      argParser.setUsageArgument(displayUsage);
    } catch (ArgumentException ae) {
      printWrappedText(err, ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage()));
      return 1;
    }

    // Init the default values so that they can appear also on the usage.
    try {
      argParser.getArguments().initArgumentsWithConfiguration();
    } catch (ConfigException ce) {
      // Ignore.
    }

    // Parse the command-line arguments provided to this program.
    try {
      argParser.parseArguments(args);
      validateTaskArgs();
    } catch (ArgumentException ae) {
      printWrappedText(err, ERR_ERROR_PARSING_ARGS.get(ae.getMessage()));
      err.println(argParser.getUsage());
      return 1;
    } catch (ClientException ce) {
      // No need to display the usage since the problem comes with a provided value.
      printWrappedText(err, ce.getMessageObject());
      return 1;
    }

    // If we should just display usage or version information,
    // then print it and exit.
    if (argParser.usageOrVersionDisplayed()) {
      return 0;
    }

    // Checks the version - if upgrade required, the tool is unusable
    try {
      checkVersion();
    } catch (InitializationException e) {
      printWrappedText(err, e.getMessage());
      return 1;
    }

    return process(argParser, initializeServer, out, err);
  }