PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/event/SimpleEventDispatcher.java

http://mobicents.googlecode.com/
Java | 106 lines | 37 code | 10 blank | 59 comment | 2 complexity | a20c338ba4b930b916b409133e1e2930 MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.smpp.event;
 24
 25import org.slf4j.Logger;
 26import org.slf4j.LoggerFactory;
 27
 28import org.mobicents.protocols.smpp.Session;
 29import org.mobicents.protocols.smpp.message.SMPPPacket;
 30
 31/**
 32 * A simple implementation of the event dispatcher interface. This
 33 * implementation simply iterates over the set of registered observers and
 34 * notifies each of the event in turn. This means that whatever thread the
 35 * <code>Connection</code> object uses to call into this object will be
 36 * blocked, from the <code>Connection</code>'s point of view, until every
 37 * observer has successfully processed the event.
 38 * 
 39 * <p>
 40 * Adding and removing observers from an instance of this class is protected
 41 * against multi-threaded access. However, event and packet notification
 42 * is not. If an event notification is currently in progress and another
 43 * thread modifies the set of registered observers, then it is possible for
 44 * the new observer to receive events before the call to <code>addObserver
 45 * </code> is complete.
 46 * </p>
 47 * 
 48 * @version $Id: SimpleEventDispatcher.java 457 2009-01-15 17:37:42Z orank $
 49 * @see com.adenki.smpp.event.EventDispatcher
 50 */
 51public class SimpleEventDispatcher extends AbstractEventDispatcher {
 52    private static final Logger LOG = LoggerFactory.getLogger(SimpleEventDispatcher.class);
 53    
 54    /**
 55     * Create a new SimpleEventDispatcher.
 56     */
 57    public SimpleEventDispatcher() {
 58    }
 59
 60    /**
 61     * Create a new SimpleEventDispatcher and register one observer on it.
 62     */
 63    public SimpleEventDispatcher(SessionObserver observer) {
 64        addObserver(observer);
 65    }
 66
 67    public void init() {
 68        // nothing to do.
 69    }
 70
 71    public void destroy() {
 72        // nothing to do.
 73    }
 74
 75    /**
 76     * Notify registered observers of an SMPP event.
 77     * @param conn the Connection with which the event is associated.
 78     * @param event the SMPP event to notify observers of.
 79     */
 80    public void notifyObservers(Session conn, SMPPEvent event) {
 81        SessionObserver[] observerList = getObserverList();
 82        for (SessionObserver observer : observerList) {
 83            try {
 84                observer.update(conn, event);
 85            } catch (Exception x) {
 86                LOG.error("An observer threw an exception during event processing", x);
 87            }
 88        }
 89    }
 90
 91    /**
 92     * Notify registered observers of an incoming SMPP packet.
 93     * @param conn  the Connection which the packet was received on.
 94     * @param packet the received packet to notify observers of.
 95     */
 96    public void notifyObservers(Session conn, SMPPPacket packet) {
 97        SessionObserver[] observerList = getObserverList();
 98        for (SessionObserver observer : observerList) {
 99            try {
100                observer.packetReceived(conn, packet);
101            } catch (Exception x) {
102                LOG.error("An observer threw an exception during packet processing", x);
103            }
104        }
105    }
106}