Example #1
0
  /**
   * Gets a list of components associated with the given parameter name
   *
   * @param <T> parent component
   * @param name the parameter name
   * @param tclass the class of the list elements
   * @return the component associated with the name
   * @throws PropertyException if the component does not exist or is of the wrong type.
   */
  public <T> List<T> getComponentList(String name, Class<T> tclass)
      throws InternalConfigurationException {
    getProperty(name, S4ComponentList.class);

    List<?> components = (List<?>) propValues.get(name);

    assert registeredProperties.get(name).getAnnotation() instanceof S4ComponentList;
    S4ComponentList annotation = (S4ComponentList) registeredProperties.get(name).getAnnotation();

    // no components names are available and no component list was yet
    // loaded therefore load the default list of components from the
    // annotation
    if (components == null) {
      List<Class<? extends Configurable>> defClasses = Arrays.asList(annotation.defaultList());

      // if (annotation.mandatory() && defClasses.isEmpty())
      // throw new InternalConfigurationException(getInstanceName(), name,
      // "mandatory property is not set!");

      List<Configurable> defaultComponents = new ArrayList<Configurable>();

      for (Class<? extends Configurable> defClass : defClasses) {
        defaultComponents.add(ConfigurationManager.getInstance(defClass));
      }

      propValues.put(name, defaultComponents);

    } else if (!components.isEmpty() && !(components.get(0) instanceof Configurable)) {

      List<Configurable> resolvedComponents = new ArrayList<Configurable>();

      for (Object componentName : components) {
        Configurable configurable = cm.lookup((String) componentName);

        if (configurable != null) {
          resolvedComponents.add(configurable);
        } else if (!annotation.beTolerant()) {
          throw new InternalConfigurationException(
              name,
              (String) componentName,
              "lookup of list-element '" + componentName + "' failed!");
        }
      }

      propValues.put(name, resolvedComponents);
    }

    List<?> values = (List<?>) propValues.get(name);
    ArrayList<T> result = new ArrayList<T>();
    for (Object obj : values) {
      if (tclass.isInstance(obj)) {
        result.add(tclass.cast(obj));
      } else {
        throw new InternalConfigurationException(
            getInstanceName(),
            name,
            "Not all elements have required type "
                + tclass
                + " Found one of type "
                + obj.getClass());
      }
    }
    return result;
  }