Exemplo n.º 1
0
  /**
   * Get or create the list of EntityListeners for an object. If a list is created, it will be kept
   * around and re-used for later persistence operations.
   *
   * @param theObj the object to get EntityLIsteners for
   * @return the list of EntityListeners for the object, or null if they do not exist
   */
  private Collection<Object> getEntityListeners(Object theObj) {
    Collection<Object> aListeners = mManagedEntityListeners.get(theObj);

    if (aListeners == null) {
      EntityListeners aEntityListeners =
          BeanReflectUtil.getAnnotation(theObj.getClass(), EntityListeners.class);

      if (aEntityListeners != null) {
        // if there are entity listeners, lets create them
        aListeners = new HashSet<Object>();
        for (Class<?> aClass : aEntityListeners.value()) {
          try {
            aListeners.add(Empire.get().instance(aClass));
          } catch (Exception e) {
            LOGGER.error("There was an error instantiating an EntityListener. ", e);
          }
        }

        mManagedEntityListeners.put(theObj, aListeners);
      } else {
        aListeners = Collections.emptyList();
      }
    }

    return aListeners;
  }
Exemplo n.º 2
0
  @SuppressWarnings("unchecked")
  private void putEntityListeners(AnnotationInfo ai, EntityListeners entityListeners) {
    Class[] entityListenerClasses = entityListeners.value();
    if (entityListenerClasses == null) return;

    Map<Class, List<ClassMethodEntry>> listeners = ai.getEntityListeners();

    List<Class<? extends Annotation>> annotations =
        Arrays.asList(
            PrePersist.class,
            PreUpdate.class,
            PreRemove.class,
            PostLoad.class,
            PostPersist.class,
            PostUpdate.class,
            PostRemove.class);
    // TODO: More than one listener per event cannot be handled like this...

    for (Class clazz : entityListenerClasses) {
      //            System.out.println("class=" + clazz);
      for (Method method : clazz.getMethods()) {
        //                System.out.println("method=" + method.getName());
        for (Class<? extends Annotation> annotationClass : annotations) {
          Annotation annotation = method.getAnnotation(annotationClass);
          addListener(listeners, clazz, method, annotation, annotationClass);
        }
      }
    }
  }
 private static void getListeners(XClass currentClazz, List<Class> orderedListeners) {
   EntityListeners entityListeners = currentClazz.getAnnotation(EntityListeners.class);
   if (entityListeners != null) {
     Class[] classes = entityListeners.value();
     int size = classes.length;
     for (int index = size - 1; index >= 0; index--) {
       orderedListeners.add(classes[index]);
     }
   }
   if (useAnnotationAnnotatedByListener) {
     Annotation[] annotations = currentClazz.getAnnotations();
     for (Annotation annot : annotations) {
       entityListeners = annot.getClass().getAnnotation(EntityListeners.class);
       if (entityListeners != null) {
         Class[] classes = entityListeners.value();
         int size = classes.length;
         for (int index = size - 1; index >= 0; index--) {
           orderedListeners.add(classes[index]);
         }
       }
     }
   }
 }