private static void configure()

in uimafit-core/src/main/java/org/apache/uima/fit/component/initialize/ExternalResourceInitializer.java [91:166]


  private static <T> void configure(UimaContext context, Class<?> baseCls, Class<?> cls, T object)
          throws ResourceInitializationException {
    if (cls.getSuperclass() != null) {
      configure(context, baseCls, cls.getSuperclass(), object);
    } else {
      // Try to initialize the external resources only once, not for each step of the
      // class hierarchy of a component.
      initializeNestedResources(context);
    }

    for (Field field : cls.getDeclaredFields()) {
      if (!ReflectionUtil.isAnnotationPresent(field, ExternalResource.class)) {
        continue;
      }

      ExternalResource era = ReflectionUtil.getAnnotation(field, ExternalResource.class);

      // Get the resource key. If it is a nested resource, also get the prefix.
      String key = era.key();
      if (key.length() == 0) {
        key = field.getName();
      }
      if (object instanceof ExternalResourceAware) {
        String prefix = ((ExternalResourceAware) object).getResourceName();
        if (prefix != null) {
          key = prefix + PREFIX_SEPARATOR + key;
        }
      }

      // Obtain the resource
      Object value = getResourceObject(context, key);
      if (value instanceof ExternalResourceLocator) {
        value = ((ExternalResourceLocator) value).getResource();
      }

      // Sanity checks
      if (value == null && era.mandatory()) {
        throw new ResourceInitializationException(new IllegalStateException(
                "Mandatory resource [" + key + "] is not set on [" + baseCls + "]"));
      }

      // Now record the setting and optionally apply it to the given
      // instance.
      if (value != null) {
        field.setAccessible(true);

        try {
          if (value instanceof ResourceList) {
            // Value is a multi-valued resource
            ResourceList resList = (ResourceList) value;

            // We cannot do this in ResourceList because the resource doesn't have access to
            // the UIMA context we use here. Resources are initialize with their own contexts
            // by the UIMA framework!
            List<Object> elements = new ArrayList<Object>();
            for (int i = 0; i < resList.getSize(); i++) {
              Object elementValue = getResourceObject(context, resList.getResourceName()
                      + PREFIX_SEPARATOR + ResourceList.ELEMENT_KEY + "[" + i + "]");
              elements.add(elementValue);
            }

            SimpleTypeConverter converter = new SimpleTypeConverter();
            value = converter.convertIfNecessary(elements, field.getType());
          }

          try {
            field.set(object, value);
          } catch (IllegalAccessException e) {
            throw new ResourceInitializationException(e);
          }
        } finally {
          field.setAccessible(false);
        }
      }
    }
  }