Ejemplo n.º 1
0
 protected CacheEntryEvent<K, V> convertValue(
     CacheEventConverter<? super K, ? super V, ?> converter, CacheEntryEvent<K, V> event) {
   CacheEntryEvent<K, V> returnedEvent;
   if (converter != null) {
     if (event instanceof EventImpl) {
       // This is a bit hacky to let the C type be passed in for the V type
       EventImpl<K, V> eventImpl = (EventImpl<K, V>) event;
       Object newValue =
           converter.convert(
               eventImpl.getKey(),
               (V) eventImpl.getOldValue(),
               eventImpl.getOldMetadata(),
               (V) eventImpl.getValue(),
               eventImpl.getMetadata(),
               null);
       if (newValue != eventImpl.getValue()) {
         EventImpl<K, V> clone = eventImpl.clone();
         clone.setValue((V) newValue);
         returnedEvent = clone;
       } else {
         returnedEvent = eventImpl;
       }
     } else {
       throw new IllegalArgumentException(
           "Provided event should be org.infinispan.notifications.cachelistener.eventEventImpl "
               + "when a converter is being used!");
     }
   } else {
     returnedEvent = event;
   }
   return returnedEvent;
 }
Ejemplo n.º 2
0
 protected CacheEntryEvent<K, V> shouldInvoke(
     CacheEntryEvent<K, V> event, boolean isLocalNodePrimaryOwner) {
   if (onlyPrimary && !isLocalNodePrimaryOwner) return null;
   if (event instanceof EventImpl) {
     EventImpl<K, V> eventImpl = (EventImpl<K, V>) event;
     // Cluster listeners only get post events
     if (eventImpl.isPre() && clustered) return null;
     EventType eventType;
     // Only use the filter if it was provided and we have an event that we can filter properly
     if (filter != null && (eventType = getEvent(eventImpl)) != null) {
       if (filter == converter && filter instanceof CacheEventFilterConverter) {
         Object newValue =
             ((CacheEventFilterConverter) filter)
                 .filterAndConvert(
                     eventImpl.getKey(),
                     eventImpl.getOldValue(),
                     eventImpl.getOldMetadata(),
                     eventImpl.getValue(),
                     eventImpl.getMetadata(),
                     eventType);
         if (newValue != null) {
           EventImpl<K, V> clone = eventImpl.clone();
           clone.setValue((V) newValue);
           return clone;
         } else {
           return null;
         }
       } else if (!filter.accept(
           eventImpl.getKey(),
           eventImpl.getOldValue(),
           eventImpl.getOldMetadata(),
           eventImpl.getValue(),
           eventImpl.getMetadata(),
           eventType)) {
         return null;
       }
     }
   }
   return event;
 }
    @Override
    public void onFilterResult(
        Object userContext,
        Object eventType,
        Object instance,
        Object[] projection,
        Comparable[] sortProjection) {
      CacheEntryEvent<K, V> event = (CacheEntryEvent<K, V>) userContext;
      if (event.isPre() && isClustered
          || isPrimaryOnly && !clusteringDependentLogic.localNodeIsPrimaryOwner(event.getKey())) {
        return;
      }

      DelegatingCacheEntryListenerInvocation<K, V>[] invocations;
      switch (event.getType()) {
        case CACHE_ENTRY_ACTIVATED:
          invocations = activated_invocations;
          break;
        case CACHE_ENTRY_CREATED:
          invocations = created_invocations;
          break;
        case CACHE_ENTRY_INVALIDATED:
          invocations = invalidated_invocations;
          break;
        case CACHE_ENTRY_LOADED:
          invocations = loaded_invocations;
          break;
        case CACHE_ENTRY_MODIFIED:
          invocations = modified_invocations;
          break;
        case CACHE_ENTRY_PASSIVATED:
          invocations = passivated_invocations;
          break;
        case CACHE_ENTRY_REMOVED:
          invocations = removed_invocations;
          break;
        case CACHE_ENTRY_VISITED:
          invocations = visited_invocations;
          break;
        case CACHE_ENTRY_EVICTED:
          invocations = evicted_invocations;
          break;
        case CACHE_ENTRY_EXPIRED:
          invocations = expired_invocations;
          break;
        default:
          return;
      }

      boolean conversionDone = false;
      for (DelegatingCacheEntryListenerInvocation<K, V> invocation : invocations) {
        if (invocation.getObservation().shouldInvoke(event.isPre())) {
          if (!conversionDone) {
            if (filterAndConvert
                && event
                    instanceof
                    EventImpl) { // todo [anistor] can it not be an EventImpl? can it not be
                                 // filterAndConvert?
              EventImpl<K, V> eventImpl = (EventImpl<K, V>) event;
              EventImpl<K, V> clone = eventImpl.clone();
              clone.setValue(
                  (V)
                      makeFilterResult(
                          userContext,
                          eventType,
                          event.getKey(),
                          projection == null ? instance : null,
                          projection,
                          sortProjection));
              event = clone;
            }
            conversionDone = true;
          }

          invocation.invokeNoChecks(
              new EventWrapper<>(event.getKey(), event), false, filterAndConvert);
        }
      }
    }