Esempio n. 1
0
 /** Try and initialise this class really early. */
 static {
   try {
     Message.localise();
   } catch (MessageException e) {
     DBC.IGNORED_EXCEPTION(e);
   }
   init();
 }
Esempio n. 2
0
 /**
  * Convenience routine for getting the value of an {@link Option} which is known to be a {@link
  * Integer}.
  *
  * @param optionName The name of the {@link Option} to fetch.
  * @return A {@link Integer} if the option holds a {@link Integer}, null otherwise.
  */
 public static Integer getInteger(String optionName) {
   String number = getString(optionName);
   if (number != null) {
     try {
       return Integer.valueOf(number);
     } catch (NumberFormatException e) {
       // Ignore
       DBC.IGNORED_EXCEPTION(e);
     }
   }
   return null;
 }
Esempio n. 3
0
 /**
  * Get the value associated with a particular option.
  *
  * @param optionName The name of the option to return the value for.
  * @return The value associated with the given option name.
  */
 private static Option<?> get(String optionName) {
   if (optionName != null) {
     init();
     if (isLegal(optionName)) {
       Option<?> ret = options.get(optionName);
       if (ret != null) {
         DBC.ASSERT(optionName.equals(ret.getName()));
         return ret;
       }
     }
   }
   return null;
 }
Esempio n. 4
0
 /** Private constructor to make sure that nobody can instantiate this class. */
 private Options() {
   DBC.UNREACHABLE_CODE();
 }
Esempio n. 5
0
  /**
   * Initialisation routine to get the options Map synced up with the .properties file which defines
   * the legal options and their values. To avoid having to throw exceptions for nearly every method
   * this routine guarantees that the options stash will be initialised, however it does NOT
   * guarantee that there will be anything in it.
   */
  private static void init() {
    // Make sure this only really runs if needed.
    if (options != null && !options.isEmpty()) {
      return;
    }
    Log.debug(Message.OPTIONS_INITIALISING());

    // The store for the options stash
    Map<String, Option<?>> initOptions = new HashMap<String, Option<?>>();

    // Load the options definitions from the properties file.
    Map<String, String> optionsDefinitions = Resources.get(Options.class);

    // Get the option names
    Set<String> optionNames = new HashSet<String>();
    for (String optionProp : optionsDefinitions.keySet()) {
      optionNames.add(optionProp.substring(0, optionProp.indexOf((int) FULL_STOP)));
    }

    // Go through the options by name and create Option objects for each of
    // them. Then store the Option objects in the stash.
    for (String optionName : optionNames) {
      // Get the type of the option
      String optionType =
          optionsDefinitions.get(Strings.join(FULL_STOP, optionName, TYPE_PROPERTY));

      // Get the description of the option
      String optionDesc =
          optionsDefinitions.get(Strings.join(FULL_STOP, optionName, DESCRIPTION_PROPERTY));

      // Get the default value(s) for the option
      String optionDefault =
          optionsDefinitions.get(Strings.join(FULL_STOP, optionName, DEFAULT_PROPERTY));

      // Find out if the option is mandatory or not.
      boolean optionMandatory =
          Resources.getBoolean(
              Options.class, Strings.join(FULL_STOP, optionName, MANDATORY_PROPERTY), false);

      // Find out if the option takes multiple values or not
      boolean optionMultiValue =
          Resources.getBoolean(
              Options.class, Strings.join(FULL_STOP, optionName, MULTIVALUE_PROPERTY), false);

      // Find out if the option is case sensitive or not.
      boolean optionCaseSensitive =
          Resources.getBoolean(
              Options.class, Strings.join(FULL_STOP, optionName, CASESENSITIVE_PROPERTY), true);

      Option<?> newOption = null;
      if (optionType.equalsIgnoreCase(ENUM)) {
        // If the Enum is set from a method this is set with the name
        // of the method and its non-null status is used as a flag to
        // indicate the fact that the Enum is dynamic.
        String dynamicMethod = null;

        // Get the allowed values
        Map<String, String> allowedValues;
        allowedValues = new HashMap<String, String>();
        for (String entry : optionsDefinitions.keySet()) {
          String prefix = Strings.join(FULL_STOP, optionName, VALUE_PROPERTY);
          if (entry.startsWith(prefix)) {
            if (entry.equals(prefix)) {
              // This is a dynamically set method
              dynamicMethod = optionsDefinitions.get(entry);
            } else {
              // Get the value
              String entryValue = optionsDefinitions.get(entry);
              entry = entry.substring(prefix.length() + 1);
              allowedValues.put(entry, entryValue);
            }
          }
        }
        if (dynamicMethod != null) {
          if (allowedValues.isEmpty()) {
            String dynamicMethodClass =
                dynamicMethod.substring(0, dynamicMethod.lastIndexOf((int) FULL_STOP));
            String dynamicMethodMethod = dynamicMethod.substring(dynamicMethodClass.length() + 1);

            try {
              // Get the class and method to call to get the
              // values
              Class<?> dMClass;
              dMClass = Class.forName(dynamicMethodClass);
              Method dMMethod = dMClass.getMethod(dynamicMethodMethod, (Class<?>[]) null);

              // Ensure the method is public static
              int modifiers = dMMethod.getModifiers();
              DBC.ASSERT(Modifier.isPublic(modifiers));
              DBC.ASSERT(Modifier.isStatic(modifiers));

              // Ensure the method returns a SortedMap
              DBC.ASSERT(SortedMap.class.equals(dMMethod.getReturnType()));

              // Ensure that the method requires no parameters.
              DBC.ASSERT(dMMethod.getParameterTypes().length == 0);

              // Now call the method
              try {
                SortedMap<?, ?> dynValues;
                dynValues = SortedMap.class.cast(dMMethod.invoke(null));
                for (Object key : dynValues.keySet()) {
                  allowedValues.put(key.toString(), dynValues.get(key).toString());
                }
              } catch (IllegalAccessException e) {
                DBC.IGNORED_EXCEPTION(e);
                DBC.UNREACHABLE_CODE();
              } catch (InvocationTargetException e) {
                DBC.IGNORED_EXCEPTION(e);
                DBC.UNREACHABLE_CODE();
              }
            } catch (ClassNotFoundException e) {
              DBC.IGNORED_EXCEPTION(e);
            } catch (NoSuchMethodException e) {
              DBC.IGNORED_EXCEPTION(e);
            }
          } else {
            // Should never get here.
            DBC.UNREACHABLE_CODE();
          }
        }

        // Get the default values
        Set<String> defaultValues = null;
        if (optionDefault != null) {
          String[] defVal = optionDefault.split(String.valueOf(COMMA));
          defaultValues = new HashSet<String>(defVal.length, 1.0f);
          for (String defValue : defVal) {
            if (allowedValues.containsKey(defValue)) {
              defaultValues.add(defValue);
            }
          }
        }

        // Create the Enum
        newOption =
            new Enum(
                optionName,
                optionDesc,
                allowedValues,
                optionMandatory,
                optionMultiValue,
                optionCaseSensitive,
                defaultValues);
      } else if (optionType.equalsIgnoreCase(SWITCH)) {
        // Convert the default value to a boolean
        boolean defaultValue = Boolean.valueOf(optionDefault);

        // Create the new Switch
        newOption = new Switch(optionName, optionDesc, defaultValue);
      } else if (optionType.equalsIgnoreCase(ARGUMENT)) {
        // Create the new Argument
        newOption =
            new Argument(
                optionName, optionDesc, optionMandatory, optionCaseSensitive, optionDefault);
      }

      if (newOption != null) {
        initOptions.put(optionName, newOption);
      }
    }

    // Make the option stash creation an "almost atomic" operation.
    options = initOptions;
  }