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

pl.bristleback.server.bristle.config.MessageContainerDefaultResolver Maven / Gradle / Ivy

// Bristleback plugin - Copyright (c) 2010 bristleback.googlecode.com
// ---------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
// This library is distributed in the hope that it will be useful,
// but without any warranty; without even the implied warranty of merchantability
// or fitness for a particular purpose.
// You should have received a copy of the GNU Lesser General Public License along
// with this program; if not, see .
// ---------------------------------------------------------------------------
package pl.bristleback.server.bristle.config;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import pl.bristleback.server.bristle.exceptions.BristleRuntimeException;
import pl.bristleback.server.bristle.messages.MessageContainer;
import pl.bristleback.server.bristle.messages.MessageDispatcher;
import pl.bristleback.server.bristle.messages.MessageSender;
import pl.bristleback.server.bristle.messages.SingleThreadedMessageDispatcher;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Default implementation of {@link MessageContainerResolver} interface.
 * This implementation takes all message senders from configuration file.
 * Sender instances can be created by plugin or taken from Spring context.
 * {@link pl.bristleback.server.bristle.messages.MessageDispatcher} implementation can be provided by user,
 * otherwise default dispatcher will be used (as of current version, it is {@link SingleThreadedMessageDispatcher} class).
 * To know what names of settings should be used, see {@link pl.bristleback.server.bristle.config.BristleConstants} documentation.
 * 

* Created on: 2010-09-24 23:02:13
* * @author Wojciech Niemiec */ public final class MessageContainerDefaultResolver implements MessageContainerResolver { private static Logger log = Logger.getLogger(MessageContainerDefaultResolver.class.getName()); private static final String SENDER_CLASS_SETTING_PREFIX = "messageSender"; public static final Class DEFAULT_DISPATCHER_CLASS = SingleThreadedMessageDispatcher.class; /** * Creates and returns message senders container. * * @param pluginSettings map of plugin settings taken from jwebsocket configuration. * @return container of message senders and dispatcher. */ public MessageContainer createMessageContainer(Map pluginSettings) { MessageContainer container = new MessageContainer(); MessageDispatcher dispatcher = getDispatcher(pluginSettings); container.setMessageDispatcher(dispatcher); Map senders = getMessageSenders(pluginSettings); container.setSenders(senders); return container; } public void assignDispatcherToSenders(MessageContainer messageContainer) { MessageDispatcher dispatcher = messageContainer.getMessageDispatcher(); for (MessageSender sender : messageContainer.getSenders().values()) { sender.setMessageDispatcher(dispatcher); } } private Map getMessageSenders(Map pluginSettings) { Map senders = new HashMap(); List senderInformationList = getSenderInformationList(pluginSettings); for (ClassConfiguration senderInformation : senderInformationList) { MessageSender sender = getSender(senderInformation); senders.put(senderInformation.getName(), sender); } return senders; } private MessageSender getSender(ClassConfiguration senderInformation) { return ClassConfigurationUtil.getInstanceFromConfiguration(MessageSender.class, senderInformation); } private List getSenderInformationList(Map pluginSettings) { return ClassConfigurationUtil.getClassConfigurationsFromSettingsMap(SENDER_CLASS_SETTING_PREFIX, pluginSettings); } private MessageDispatcher getDispatcher(Map pluginSettings) { String dispatcherClassName = pluginSettings.get(BristleConstants.DISPATCHER_CLASS_NAME_PARAMETER); Class dispatcherClass = getDispatcherClass(dispatcherClassName); try { return (MessageDispatcher) (dispatcherClass.newInstance()); } catch (Exception e) { throw new BristleRuntimeException("Cannot init message dispatcher class instance."); } } private Class getDispatcherClass(String dispatcherClassName) { Class dispatcherClass; if (StringUtils.isEmpty(dispatcherClassName)) { dispatcherClass = getDefaultDispatcher(); } else { dispatcherClass = getDispatcherClassForName(dispatcherClassName); } return dispatcherClass; } private Class getDispatcherClassForName(String dispatcherClassName) { try { Class dispatcherClass = Class.forName(dispatcherClassName); for (Class interfaceClass : dispatcherClass.getInterfaces()) { if (interfaceClass.equals(MessageDispatcher.class)) { return dispatcherClass; } } throw new BristleRuntimeException("Dispatcher class " + dispatcherClass.getName() + " must implement " + MessageDispatcher.class.getSimpleName() + " interface."); } catch (ClassNotFoundException e) { throw new BristleRuntimeException("Cannot find class with name " + dispatcherClassName + "."); } } private Class getDefaultDispatcher() { if (log.isDebugEnabled()) { log.debug("Cannot find message dispatcher class, system will use default dispatcher"); } return DEFAULT_DISPATCHER_CLASS; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy