示例#1
0
  /** Initialize all options */
  static {
    logger.info("Initialize all options.");

    Field[] fields = Config.class.getDeclaredFields();
    for (Field field : fields) {
      if (field.isAnnotationPresent(Ora2PgOption.class)) {
        Ora2PgOption option = field.getAnnotation(Ora2PgOption.class);

        Builder builder = null;
        if (StringUtils.isEmpty(option.opt())) {
          builder = Option.builder();
        } else {
          builder = Option.builder(option.opt());
        }
        builder.longOpt(option.longOpt());

        if (option.hasArg()) {
          builder
              .hasArg(true)
              .argName(option.argName())
              .optionalArg(option.optionalArg())
              .type(option.type());
        } else {
          builder.hasArg(false);
        }

        OPTIONS.addOption(builder.desc(option.desc()).build());
      }
    }
  }
示例#2
0
  /**
   * Copy the command line options' value to config instance
   *
   * @param options Command line options' value
   * @param config Config instance
   * @throws Exception
   */
  private static void copyOptionToConfig(CommandLine options, Config config) throws Exception {

    logger.info("Copy options to config instance.");

    Field[] fields = Config.class.getDeclaredFields();
    for (Field field : fields) {
      if (field.isAnnotationPresent(Ora2PgOption.class)) {

        Ora2PgOption option = field.getAnnotation(Ora2PgOption.class);
        String longOpt = option.longOpt();

        // ===If option is not set===
        if (!options.hasOption(longOpt)) {
          continue;
        }

        // ===If option is set===
        PropertyDescriptor pd = null;
        try {
          pd = new PropertyDescriptor(field.getName(), Config.class);
        } catch (IntrospectionException e) {
          logger.error("Get PropertyDescriptor error.", e);
          throw e;
        } catch (Exception e) {
          logger.error("Get PropertyDescriptor error.", e);
          throw e;
        }

        // Get original option value
        Method get = pd.getReadMethod();
        Object originalValue = null;
        try {
          originalValue = get.invoke(config);
        } catch (Exception e) {
          logger.error("Invoke the getter method error.", e);
          throw e;
        }

        // Set command new option value
        Method set = pd.getWriteMethod();
        String value = null;
        try {
          if (option.hasArg()) {
            value = options.getOptionValue(longOpt);

            // Optional option
            if (option.optionalArg() && StringUtils.isEmpty(value)) {
              value = originalValue.toString();
            }

            if (String.class.equals(option.type())) {
              set.invoke(config, value);
            } else if (Integer.class.equals(option.type())) {
              set.invoke(config, Integer.parseInt(value));
            }
          } else {
            set.invoke(config, true);
          }
        } catch (NumberFormatException e) {
          logger.error(
              "Convert option's value error: --"
                  + longOpt
                  + " must be a number. '"
                  + value
                  + "' is not a valid number.",
              e);
          throw e;
        } catch (IllegalAccessException e) {
          logger.error("Invoke the setter method error. (args: " + value + ")", e);
          throw e;
        } catch (IllegalArgumentException e) {
          logger.error("Invoke the setter method error. (args: " + value + ")", e);
          throw e;
        } catch (InvocationTargetException e) {
          logger.error("Invoke the setter method error. (args: " + value + ")", e);
          throw e;
        } catch (Exception e) {
          logger.error("Copy options to config instance error");
          throw e;
        }
      }
    }
  }