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

org.appng.api.messaging.EventRegistry Maven / Gradle / Ivy

There is a newer version: 1.24.5
Show newest version
/*
 * Copyright 2011-2019 the original author or authors.
 *
 * 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 org.appng.api.messaging;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.appng.api.BusinessException;
import org.appng.api.Environment;
import org.appng.api.InvalidConfigurationException;
import org.appng.api.model.Site;

/**
 * A registry for {@link EventHandler}s to be used by {@link Receiver} implementations.
 * 
 * @author @author Matthias Müller
 * 
 * @see Receiver#setDefaultHandler(EventHandler)
 * @see Receiver#registerHandler(EventHandler)
 */
public class EventRegistry {

	private Map, List>> handlers = new ConcurrentHashMap<>();
	private EventHandler defaultHandler;

	/**
	 * Registers the given {@link EventHandler}
	 * 
	 * @param handler
	 *            the {@link EventHandler} to be registered
	 */
	public > void register(H handler) {
		if (!handlers.containsKey(handler.getEventClass())) {
			handlers.put(handler.getEventClass(), new ArrayList>());
		}
		handlers.get(handler.getEventClass()).add(handler);
	}

	/**
	 * Retrieves a list of {@link EventHandler}s that have been registered for the given type of {@link Event}. If no
	 * such handlers have been registered, the list will only contain the default handler.
	 * 
	 * @param event
	 *            the {@link Event} to retrieve the {@link EventHandler}s for
	 * @return a list of {@link EventHandler}s
	 * @see #setDefaultHandler(EventHandler)
	 */
	@SuppressWarnings("unchecked")
	public > List getHandlers(E event) {
		List handlerList = (List) handlers.get(event.getClass());
		if (null == handlerList || handlerList.isEmpty()) {
			return (List) Arrays.asList(null == defaultHandler ? new DefaultEventHandler() : defaultHandler);
		}
		return handlerList;
	}

	/**
	 * Set the default {@link EventHandler}
	 * 
	 * @param defaultHandler
	 *            the default {@link EventHandler}
	 */
	public void setDefaultHandler(EventHandler defaultHandler) {
		this.defaultHandler = defaultHandler;
	}

	class DefaultEventHandler implements EventHandler {

		public void onEvent(Event event, Environment environment, Site site)
				throws InvalidConfigurationException, BusinessException {
			event.perform(environment, site);
		}

		public Class getEventClass() {
			return Event.class;
		}

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy