Пример #1
0
 /**
  * Applies a config entry to a component. If the entry is not valid, the method prints an
  * exception and returns false.
  *
  * @param <T> Type of the config option.
  * @param component A component object.
  * @param entry The configuration entry to set.
  * @return True if the config entry could be applied succesfully, otherwise false.
  */
 public <T> boolean applyConfigEntry(AbstractComponent component, ConfigEntry<T> entry) {
   try {
     component.applyConfigEntry(entry);
     pool.addConfigEntry(component, entry, true);
     return true;
   } catch (InvalidConfigOptionValueException e) {
     pool.addConfigEntry(component, entry, false);
     e.printStackTrace();
     return false;
   }
 }
Пример #2
0
  /**
   * Convenience method for testing purposes. If you know that the type of the value is correct, it
   * is preferable to create a ConfigEntry object and apply it to the component (no type checking
   * necessary).
   *
   * @param <T> Type of the config option (Integer, String etc.).
   * @param component A component.
   * @param optionName The name of the config option.
   * @param value The value of the config option.
   */
  @SuppressWarnings("unchecked")
  public <T> void applyConfigEntry(AbstractComponent component, String optionName, T value) {
    logger.trace(component);
    logger.trace(optionName);
    logger.trace(value);
    logger.trace(value.getClass());
    // first we look whether the component is registered
    if (components.contains(component.getClass())) {

      // look for a config option with the specified name
      ConfigOption<?> option =
          (ConfigOption<?>) componentOptionsByName.get(component.getClass()).get(optionName);
      if (option != null) {
        // check whether the given object has the correct type
        if (!option.checkType(value)) {
          System.out.println(
              "Warning: value "
                  + value
                  + " is not valid for option "
                  + optionName
                  + " in component "
                  + component
                  + ". It does not have the correct type.");
          return;
        }

        // we have checked the type, hence it should now be safe to
        // typecast and
        // create a ConfigEntry object
        ConfigEntry<T> entry = null;
        try {
          entry = new ConfigEntry<T>((ConfigOption<T>) option, value);
          component.applyConfigEntry(entry);
          pool.addConfigEntry(component, entry, true);
        } catch (InvalidConfigOptionValueException e) {
          pool.addConfigEntry(component, entry, false);
          System.out.println(
              "Warning: value "
                  + value
                  + " is not valid for option "
                  + optionName
                  + " in component "
                  + component);
        }
      } else {
        logger.warn("Warning: undefined option " + optionName + " in component " + component);
      }
    } else {
      logger.warn("Warning: unregistered component " + component);
    }
  }