Ejemplo n.º 1
0
  /**
   * Gets a component associated with the given parameter name. First search the component in
   * property table, then try to get component by name from the manager, then creates component with
   * default properties.
   *
   * @param name the parameter name
   * @return the component associated with the name
   * @throws edu.cmu.sphinx.util.props.PropertyException if the component does not exist or is of
   *     the wrong type.
   */
  public Configurable getComponent(String name) throws PropertyException {
    S4PropWrapper s4PropWrapper = getProperty(name, S4Component.class);
    Configurable configurable = null;

    S4Component s4Component = (S4Component) s4PropWrapper.getAnnotation();
    Class<? extends Configurable> expectedType = s4Component.type();

    Object propVal = propValues.get(name);

    if (propVal != null && propVal instanceof Configurable) {
      return (Configurable) propVal;
    }

    if (propVal != null && propVal instanceof String) {
      PropertySheet ps = cm.getPropertySheet(flattenProp(name));
      if (ps != null) configurable = ps.getOwner();
      else
        throw new InternalConfigurationException(
            getInstanceName(), name, "component '" + flattenProp(name) + "' is missing");
    }

    if (configurable != null && !expectedType.isInstance(configurable))
      throw new InternalConfigurationException(
          getInstanceName(), name, "mismatch between annotation and component type");

    if (configurable != null) {
      propValues.put(name, configurable);
      return configurable;
    }

    configurable = getComponentFromAnnotation(name, s4Component);

    propValues.put(name, configurable);
    return configurable;
  }
Ejemplo n.º 2
0
  /**
   * Returns the class of of a registered component property without instantiating it.
   *
   * @param propName the name of the property
   * @return class of the component corresponding to that property
   */
  public Class<? extends Configurable> getComponentClass(String propName) {
    Class<? extends Configurable> defClass = null;

    if (propValues.get(propName) != null)
      try {
        Class<?> objClass = Class.forName((String) propValues.get(propName));
        defClass = objClass.asSubclass(Configurable.class);
      } catch (ClassNotFoundException e) {
        PropertySheet ps = cm.getPropertySheet(flattenProp(propName));
        defClass = ps.ownerClass;
      }
    else {
      S4Component comAnno = (S4Component) registeredProperties.get(propName).getAnnotation();
      defClass = comAnno.defaultClass();
      if (comAnno.mandatory()) defClass = null;
    }

    return defClass;
  }