All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gravitee.am.gateway.event.EventManagerImpl Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.am.gateway.event;

import com.google.common.collect.Lists;
import io.gravitee.am.common.event.DomainEvent;
import io.gravitee.am.common.event.EventManager;
import io.gravitee.am.gateway.handler.common.auth.AuthenticationDetails;
import io.gravitee.am.model.Domain;
import io.gravitee.am.model.ReferenceType;
import io.gravitee.am.model.common.event.Payload;
import io.gravitee.common.event.Event;
import io.gravitee.common.event.EventListener;
import io.gravitee.common.event.impl.SimpleEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

import static java.util.Optional.ofNullable;
import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * Override default event manager to enable concurrent access
 *
 * @author Titouan COMPIEGNE (titouan.compiegne at graviteesource.com)
 * @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
 * @author GraviteeSource Team
 */
public class EventManagerImpl implements EventManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(io.gravitee.common.event.impl.EventManagerImpl.class);

    private ConcurrentMap> listenersMap = new ConcurrentHashMap<>();

    @Override
    public void publishEvent(Enum type, Object content) {
        this.publishEvent(new SimpleEvent(type, content));
    }

    @Override
    public void publishEvent(Event event) {
        if(event == null){
            LOGGER.debug("Cannot publish event with event null");
            return;
        }

        if(event.type() == null){
            LOGGER.debug("Cannot publish event with event.type() null");
            return;
        }

        LOGGER.debug("Publish event {} - {}", event.type(), event.content());

        String domain = null;
        if (event.content() != null) {
            Object content = event.content();
            if (content instanceof Payload && ((Payload)content).getReferenceType() == ReferenceType.DOMAIN) {
                domain = ((Payload)content).getReferenceId();
            } else if (content instanceof Domain) {
                domain = ((Domain)content).getId();
            } else if (content instanceof AuthenticationDetails && ((AuthenticationDetails)content).getDomain() != null) {
                domain = ((AuthenticationDetails)content).getDomain().getId();
            }
        }
        List listeners = getEventListeners(event.type().getClass(), domain);
        List safeConcurrentListeners = Lists.newArrayList(listeners.iterator());

        if (isEmpty(safeConcurrentListeners)) {
            LOGGER.warn("Event received but no listeners available (Domain: {}, contentClass: {}, eventType: {})",
                    domain,
                    ofNullable(event.content()).map(obj -> obj.getClass().getSimpleName()).orElse("null content"),
                    event.type());
        }

        for(EventListenerWrapper listener : safeConcurrentListeners) {
            listener.eventListener().onEvent(event);
        }
    }

    @Override
    public  void subscribeForEvents(EventListener eventListener, T... events) {
        for( T event : events) {
            addEventListener(eventListener, (Class) event.getClass(), Arrays.asList(events), null);
        }
    }

    @Override
    public  void subscribeForEvents(EventListener eventListener, Class events) {
        addEventListener(eventListener, events, EnumSet.allOf(events), null);
    }

    @Override
    public  void subscribeForEvents(EventListener eventListener, Class events, String domain) {
        addEventListener(eventListener, events, EnumSet.allOf(events), domain);
    }

    @Override
    public  void unsubscribeForEvents(EventListener eventListener, Class events, String domain) {
        this.listenersMap.remove(new ComparableEventType(events, domain));
    }

    @Override
    public  void unsubscribeForCrossEvents(EventListener eventListener, Class events, String domain) {
        List listeners = this.listenersMap.get(new ComparableEventType(events, domain));
        if (listeners != null) {
            List filteredList = listeners
                    .stream()
                    .filter(listenerWrapper -> !eventListener.equals(listenerWrapper.eventListener()))
                    .collect(Collectors.toList());
            this.listenersMap.put(new ComparableEventType(events, domain), filteredList);
        }
    }

    private  void addEventListener(EventListener eventListener, Class enumClass, Collection events, String domain) {
        LOGGER.info("Register new listener {} for event type {}", eventListener.getClass().getSimpleName(), enumClass);

        List listeners = getEventListeners(enumClass, domain);
        listeners.add(new EventListenerWrapper(eventListener, events));
    }

    private  List getEventListeners(Class eventType, String domain) {
        List listeners;

        if (eventType.equals(DomainEvent.class)) {
            listeners = this.listenersMap.get(new ComparableEventType(eventType, null));
        } else {
            listeners = this.listenersMap.get(new ComparableEventType(eventType, domain));
        }

        if (listeners == null) {
            listeners = Collections.synchronizedList(new ArrayList<>());
            this.listenersMap.put(new ComparableEventType(eventType, domain), listeners);
        }

        return listeners;
    }

    private class EventListenerWrapper {

        private final EventListener eventListener;
        private final Set events;

        public EventListenerWrapper(EventListener eventListener, Collection events) {
            this.eventListener = eventListener;
            this.events = new HashSet(events);
        }

        public EventListener eventListener() {
            return eventListener;
        }

        public Set events() {
            return events;
        }
    }

    private class ComparableEventType implements Comparable> {

        private static final int HASH = 7 * 89;
        private final Class wrappedClass;
        private final String domain;

        public ComparableEventType(Class wrappedClass, String domain) {
            this.wrappedClass = wrappedClass;
            this.domain = domain;
        }

        @Override
        public int compareTo(ComparableEventType o) {
            if (domain != null) {
                return (wrappedClass.getCanonicalName() + domain).compareTo(o.wrappedClass.getCanonicalName() + o.domain);
            }

            return wrappedClass.getCanonicalName().compareTo(o.wrappedClass.getCanonicalName());
        }

        @Override
        public int hashCode() {
            return HASH + (this.wrappedClass != null ? this.wrappedClass.hashCode() : 0);
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof ComparableEventType)) {
                return false;
            }

            return compareTo((ComparableEventType) o) == 0;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy