Beispiel #1
0
  private void handleClass(Class<?> clazz) {
    if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) return;

    if (Externalizer.class.isAssignableFrom(clazz)) {
      try {
        scannedExternalizers.add(TypeUtil.newInstance(clazz, Externalizer.class));
      } catch (Exception e) {
        log.error(e, "Could not create new instance of: %s", clazz);
      }
    }

    if (ExceptionConverter.class.isAssignableFrom(clazz)) {
      try {
        exceptionConverters.add(TypeUtil.newInstance(clazz, ExceptionConverter.class));
      } catch (Exception e) {
        if (!clazz
            .getName()
            .equals("org.granite.tide.hibernate.HibernateValidatorExceptionConverter")) // GDS-582
        log.error(e, "Could not create new instance of: %s", clazz);
      }
    }
  }
Beispiel #2
0
  @SuppressWarnings("unchecked")
  private <T, C extends Config> T getElementByType(
      String type,
      ConfigurableFactory<T, C> factory,
      ConcurrentHashMap<String, T> elementsByType,
      Map<String, String> elementsByInstanceOf,
      Map<String, String> elementsByAnnotatedWith,
      List<T> scannedConfigurables) {

    // This NULL object is a Java null placeholder: ConcurrentHashMap doesn't allow
    // null values...
    final T NULL = factory.getNullInstance();

    T element = elementsByType.get(type);
    if (element != null) return (NULL == element ? null : element);
    element = NULL;

    Class<?> typeClass = null;
    try {
      typeClass = TypeUtil.forName(type);
    } catch (Exception e) {
      throw new GraniteConfigException("Could not load class: " + type, e);
    }

    if (elementsByAnnotatedWith != null && NULL == element) {
      for (Map.Entry<String, String> entry : elementsByAnnotatedWith.entrySet()) {
        String annotation = entry.getKey();
        try {
          Class<Annotation> annotationClass = TypeUtil.forName(annotation, Annotation.class);
          if (typeClass.isAnnotationPresent(annotationClass)) {
            element = factory.getInstance(entry.getValue(), (C) this);
            break;
          }
        } catch (Exception e) {
          throw new GraniteConfigException("Could not load class: " + annotation, e);
        }
      }
    }

    if (elementsByInstanceOf != null && NULL == element) {
      for (Map.Entry<String, String> entry : elementsByInstanceOf.entrySet()) {
        String instanceOf = entry.getKey();
        try {
          Class<?> instanceOfClass = TypeUtil.forName(instanceOf);
          if (instanceOfClass.isAssignableFrom(typeClass)) {
            element = factory.getInstance(entry.getValue(), (C) this);
            break;
          }
        } catch (Exception e) {
          throw new GraniteConfigException("Could not load class: " + instanceOf, e);
        }
      }
    }

    if (NULL == element)
      element = factory.getInstanceForBean(scannedConfigurables, typeClass, (C) this);

    T previous = elementsByType.putIfAbsent(type, element);
    if (previous != null) element = previous;

    return (NULL == element ? null : element);
  }