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

com.github.vindell.websocket.session.handler.AbstractRouteableEventHandler Maven / Gradle / Ivy

The newest version!
package com.github.vindell.websocket.session.handler;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.vindell.websocket.event.WebSocketMessageEvent;
import com.github.vindell.websocket.exception.EventHandleException;
import com.github.vindell.websocket.session.handler.chain.HandlerChain;
import com.github.vindell.websocket.session.handler.chain.HandlerChainResolver;

public class AbstractRouteableEventHandler extends AbstractEnabledEventHandler {

	private static final Logger LOG = LoggerFactory.getLogger(AbstractRouteableEventHandler.class);

	/**
	 * 用来判定使用那个HandlerChian
	 */
	protected HandlerChainResolver handlerChainResolver;

	public AbstractRouteableEventHandler() {
		super();
	}

	public AbstractRouteableEventHandler(HandlerChainResolver handlerChainResolver) {
		super();
		this.handlerChainResolver = handlerChainResolver;
	}

	@Override
	protected void doHandlerInternal(T event, HandlerChain handlerChain) throws Exception {
		Throwable t = null;
		try {
			this.executeChain(event, handlerChain);
		} catch (Throwable throwable) {
			t = throwable;
		}
		if (t != null) {
			if (t instanceof IOException) {
				throw (IOException) t;
			}
			String msg = "Handlered event failed.";
			throw new EventHandleException(msg, t);
		}
	}

	protected HandlerChain getExecutionChain(T event, HandlerChain origChain) {
		HandlerChain chain = origChain;

		HandlerChainResolver resolver = getHandlerChainResolver();
		if (resolver == null) {
			LOG.debug("No HandlerChainResolver configured.  Returning original HandlerChain.");
			return origChain;
		}

		HandlerChain resolved = resolver.getChain(event, origChain);
		if (resolved != null) {
			LOG.trace("Resolved a configured HandlerChain for the current event.");
			chain = resolved;
		} else {
			LOG.trace("No HandlerChain configured for the current event.  Using the default.");
		}
		
		return chain;
	}

	protected void executeChain(T event, HandlerChain origChain) throws Exception {
		HandlerChain chain = getExecutionChain(event, origChain);
		chain.doHandler(event);
	}

	public HandlerChainResolver getHandlerChainResolver() {
		return handlerChainResolver;
	}

	public void setHandlerChainResolver(HandlerChainResolver handlerChainResolver) {
		this.handlerChainResolver = handlerChainResolver;
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy