Search in sources :

Example 1 with MapEventTransformer

use of com.tangosol.util.MapEventTransformer in project coherence-spring by coherence-community.

the class MapListenerRegistrationBean method registerMapListeners.

/**
 * Listen for {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent.Type#CREATED Created}
 * {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent CacheLifecycleEvents}
 * and register relevant map listeners when caches are created.
 * @param event the {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent}
 */
@CoherenceEventListener
@SuppressWarnings({ "rawtypes", "unchecked" })
void registerMapListeners(@Created CacheLifecycleEvent event) {
    String cacheName = event.getCacheName();
    String eventScope = event.getScopeName();
    String eventSession = event.getSessionName();
    String eventService = event.getServiceName();
    Set<AnnotatedMapListener<?, ?>> setListeners = getMapListeners(removeScope(eventService), cacheName);
    Session session = Coherence.findSession(eventSession).orElseThrow(() -> new IllegalStateException("Cannot find a Session with name " + eventSession));
    NamedCache cache = session.getCache(cacheName);
    for (AnnotatedMapListener<?, ?> listener : setListeners) {
        if (listener.hasFilterAnnotation()) {
            // ensure that the listener's filter has been resolved as this
            // was not possible as discovery time.
            listener.setFilter(this.filterService.resolve(listener.getFilterAnnotations()));
        }
        if (listener.hasTransformerAnnotation()) {
            // ensure that the listener's transformer has been resolved as this
            // was not possible at discovery time.
            listener.resolveTransformer(this.mapEventTransformerService);
        }
        String sScope = listener.getScopeName();
        boolean fScopeOK = sScope == null || sScope.equals(eventScope);
        String sSession = listener.getSession();
        boolean fSessionOK = sSession == null || sSession.equals(eventSession);
        if (fScopeOK && fSessionOK) {
            Filter filter = listener.getFilter();
            if (filter != null && !(filter instanceof MapEventFilter)) {
                filter = new MapEventFilter(MapEventFilter.E_ALL, filter);
            }
            MapEventTransformer transformer = listener.getTransformer();
            if (transformer != null) {
                filter = new MapEventTransformerFilter(filter, transformer);
            }
            try {
                boolean fLite = listener.isLiteEvent();
                if (listener.isSynchronous()) {
                    cache.addMapListener(listener.synchronous(), filter, fLite);
                } else {
                    cache.addMapListener(listener, filter, fLite);
                }
            } catch (Exception ex) {
                throw Exceptions.ensureRuntimeException(ex);
            }
        }
    }
}
Also used : MapEventFilter(com.tangosol.util.filter.MapEventFilter) MapEventTransformer(com.tangosol.util.MapEventTransformer) MapEventTransformerFilter(com.tangosol.util.filter.MapEventTransformerFilter) NamedCache(com.tangosol.net.NamedCache) Filter(com.tangosol.util.Filter) MapEventFilter(com.tangosol.util.filter.MapEventFilter) MapEventTransformerFilter(com.tangosol.util.filter.MapEventTransformerFilter) Session(com.tangosol.net.Session) CoherenceEventListener(com.oracle.coherence.spring.event.CoherenceEventListener)

Example 2 with MapEventTransformer

use of com.tangosol.util.MapEventTransformer in project coherence-spring by coherence-community.

the class MapEventTransformerService method resolve.

/**
 * Resolve a {@link MapEventTransformer} from the
 * specified qualifier annotations.
 * @param annotations  the qualifier annotations to use to create the transformer
 * @param <K>          the type of the keys of the entry to be transformed
 * @param <V>          the type of the values of the entry to be transformed
 * @param <U>          the type of the transformed values
 * @return a {@link MapEventTransformer} from the specified qualifier annotations
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public <K, V, U> MapEventTransformer<K, V, U> resolve(Set<Annotation> annotations) {
    Optional<Annotation> optionalTransformer = annotations.stream().filter((annotation) -> annotation.annotationType().isAnnotationPresent(MapEventTransformerBinding.class)).findFirst();
    Optional<Annotation> optionalExtractor = annotations.stream().filter((annotation) -> annotation.annotationType().isAnnotationPresent(ExtractorBinding.class)).findFirst();
    if (optionalTransformer.isPresent()) {
        final Annotation annotation = optionalTransformer.get();
        final Class<? extends Annotation> annotationType = annotation.annotationType();
        final MapEventTransformerFactory factory = CoherenceAnnotationUtils.getSingleBeanWithAnnotation(this.applicationContext, annotationType);
        return factory.create(annotation);
    } else if (optionalExtractor.isPresent()) {
        // there is one or more ExtractorBinding annotations
        ValueExtractor<Object, Object> extractor = this.extractorFactory.resolve(annotations);
        return new ExtractorEventTransformer(extractor);
    }
    // there are no transformer or extractor annotations.
    return null;
}
Also used : ExtractorEventTransformer(com.tangosol.util.transformer.ExtractorEventTransformer) ExtractorBinding(com.oracle.coherence.spring.annotation.ExtractorBinding) MapEventTransformerBinding(com.oracle.coherence.spring.annotation.MapEventTransformerBinding) MapEventTransformerFactory(com.oracle.coherence.spring.annotation.MapEventTransformerFactory) CoherenceAnnotationUtils(com.oracle.coherence.spring.configuration.support.CoherenceAnnotationUtils) InjectionPoint(org.springframework.beans.factory.InjectionPoint) Set(java.util.Set) List(java.util.List) ValueExtractor(com.tangosol.util.ValueExtractor) MapEventTransformer(com.tangosol.util.MapEventTransformer) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) ExtractorEventTransformer(com.tangosol.util.transformer.ExtractorEventTransformer) MapEventTransformerFactory(com.oracle.coherence.spring.annotation.MapEventTransformerFactory) ValueExtractor(com.tangosol.util.ValueExtractor) Annotation(java.lang.annotation.Annotation)

Example 3 with MapEventTransformer

use of com.tangosol.util.MapEventTransformer in project micronaut-coherence by micronaut-projects.

the class MapEventTransformerFactories method resolve.

/**
 * Resolve a {@link com.tangosol.util.MapEventTransformer} from the
 * specified qualifier annotations.
 *
 * @param annotations  the qualifier annotations to use to create the transformer
 * @param <K>          the type of the keys of the entry to be transformed
 * @param <V>          the type of the values of the entry to be transformed
 * @param <U>          the type of the transformed values
 *
 * @return a {@link com.tangosol.util.MapEventTransformer} from the
 *         specified qualifier annotations
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public <K, V, U> MapEventTransformer<K, V, U> resolve(Set<Annotation> annotations) {
    Optional<Annotation> optionalTransformer = annotations.stream().filter(a -> a.annotationType().isAnnotationPresent(MapEventTransformerBinding.class)).findFirst();
    Optional<Annotation> optionalExtractor = annotations.stream().filter(a -> a.annotationType().isAnnotationPresent(ExtractorBinding.class)).findFirst();
    if (optionalTransformer.isPresent()) {
        Annotation annotation = optionalTransformer.get();
        Class<? extends Annotation> type = annotation.annotationType();
        MapEventTransformerFactory factory = ctx.findBean(MapEventTransformerFactory.class, new FactoryQualifier<>(type)).orElse(null);
        if (factory != null) {
            return factory.create(annotation);
        } else {
            throw new IllegalStateException("Unsatisfied dependency - no MapEventTransformerFactory bean found annotated with " + annotation);
        }
    } else if (optionalExtractor.isPresent()) {
        // there is one or more ExtractorBinding annotations
        ValueExtractor<Object, Object> extractor = extractorFactory.resolve(annotations);
        return new ExtractorEventTransformer(extractor);
    }
    // there are no transformer or extractor annotations.
    return null;
}
Also used : ExtractorEventTransformer(com.tangosol.util.transformer.ExtractorEventTransformer) Set(java.util.Set) Inject(javax.inject.Inject) List(java.util.List) ValueExtractor(com.tangosol.util.ValueExtractor) ApplicationContext(io.micronaut.context.ApplicationContext) MapEventTransformer(com.tangosol.util.MapEventTransformer) Factory(io.micronaut.context.annotation.Factory) Annotation(java.lang.annotation.Annotation) Prototype(io.micronaut.context.annotation.Prototype) Optional(java.util.Optional) ExtractorBinding(io.micronaut.coherence.annotation.ExtractorBinding) MapEventTransformerBinding(io.micronaut.coherence.annotation.MapEventTransformerBinding) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) InjectionPoint(io.micronaut.inject.InjectionPoint) ExtractorEventTransformer(com.tangosol.util.transformer.ExtractorEventTransformer) ValueExtractor(com.tangosol.util.ValueExtractor) Annotation(java.lang.annotation.Annotation)

Example 4 with MapEventTransformer

use of com.tangosol.util.MapEventTransformer in project micronaut-coherence by micronaut-projects.

the class CoherenceEventListenerProcessor method registerMapListeners.

/**
 * Listen for {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent.Type#CREATED Created}
 * {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent CacheLifecycleEvents}
 * and register relevant map listeners when caches are created.
 *
 * @param event the {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent}
 */
@CoherenceEventListener
@SuppressWarnings({ "rawtypes", "unchecked" })
void registerMapListeners(@Created CacheLifecycleEvent event) {
    String cacheName = event.getCacheName();
    String eventScope = event.getScopeName();
    String eventSession = event.getSessionName();
    String eventService = event.getServiceName();
    Set<AnnotatedMapListener<?, ?>> setListeners = getMapListeners(removeScope(eventService), cacheName);
    Session session = Coherence.findSession(eventSession).orElseThrow(() -> new IllegalStateException("Cannot find a Session with name " + eventSession));
    NamedCache cache = session.getCache(cacheName);
    for (AnnotatedMapListener<?, ?> listener : setListeners) {
        if (listener.hasFilterAnnotation()) {
            // ensure that the listener's filter has been resolved as this
            // was not possible as discovery time.
            listener.resolveFilter(filterProducer);
        }
        if (listener.hasTransformerAnnotation()) {
            // ensure that the listener's transformer has been resolved as this
            // was not possible as discovery time.
            listener.resolveTransformer(transformerProducer);
        }
        String sScope = listener.getScopeName();
        boolean fScopeOK = sScope == null || sScope.equals(eventScope);
        String sSession = listener.getSessionName();
        boolean fSessionOK = sSession == null || sSession.equals(eventSession);
        if (fScopeOK && fSessionOK) {
            Filter filter = listener.getFilter();
            if (filter != null && !(filter instanceof MapEventFilter)) {
                filter = new MapEventFilter(MapEventFilter.E_ALL, filter);
            }
            MapEventTransformer transformer = listener.getTransformer();
            if (transformer != null) {
                filter = new MapEventTransformerFilter(filter, transformer);
            }
            try {
                boolean fLite = listener.isLite();
                if (listener.isSynchronous()) {
                    cache.addMapListener(listener.synchronous(), filter, fLite);
                } else {
                    cache.addMapListener(listener, filter, fLite);
                }
            } catch (Exception e) {
                throw Exceptions.ensureRuntimeException(e);
            }
        }
    }
}
Also used : MapEventFilter(com.tangosol.util.filter.MapEventFilter) MapEventTransformer(com.tangosol.util.MapEventTransformer) MapEventTransformerFilter(com.tangosol.util.filter.MapEventTransformerFilter) NamedCache(com.tangosol.net.NamedCache) Filter(com.tangosol.util.Filter) MapEventFilter(com.tangosol.util.filter.MapEventFilter) MapEventTransformerFilter(com.tangosol.util.filter.MapEventTransformerFilter) Session(com.tangosol.net.Session) CoherenceEventListener(io.micronaut.coherence.annotation.CoherenceEventListener)

Aggregations

MapEventTransformer (com.tangosol.util.MapEventTransformer)4 NamedCache (com.tangosol.net.NamedCache)2 Session (com.tangosol.net.Session)2 Filter (com.tangosol.util.Filter)2 ValueExtractor (com.tangosol.util.ValueExtractor)2 MapEventFilter (com.tangosol.util.filter.MapEventFilter)2 MapEventTransformerFilter (com.tangosol.util.filter.MapEventTransformerFilter)2 ExtractorEventTransformer (com.tangosol.util.transformer.ExtractorEventTransformer)2 Annotation (java.lang.annotation.Annotation)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 ExtractorBinding (com.oracle.coherence.spring.annotation.ExtractorBinding)1 MapEventTransformerBinding (com.oracle.coherence.spring.annotation.MapEventTransformerBinding)1 MapEventTransformerFactory (com.oracle.coherence.spring.annotation.MapEventTransformerFactory)1 CoherenceAnnotationUtils (com.oracle.coherence.spring.configuration.support.CoherenceAnnotationUtils)1 CoherenceEventListener (com.oracle.coherence.spring.event.CoherenceEventListener)1 CoherenceEventListener (io.micronaut.coherence.annotation.CoherenceEventListener)1 ExtractorBinding (io.micronaut.coherence.annotation.ExtractorBinding)1 MapEventTransformerBinding (io.micronaut.coherence.annotation.MapEventTransformerBinding)1