1 /*
2 * $Id: DefaultChannel.java 3046 2011-12-17 15:28:31Z andrewinkler $
3 * ============================================================================
4 * Project swinger-commons
5 * Copyright (c) 2005-2008 by Andre Winkler. All rights reserved.
6 * ============================================================================
7 * GNU LESSER GENERAL PUBLIC LICENSE
8 * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26 package de.gluehloch.swinger.channel.impl;
27
28 import de.gluehloch.swinger.channel.Channel;
29 import de.gluehloch.swinger.channel.Message;
30 import de.gluehloch.swinger.channel.MessageListener;
31 import de.gluehloch.swinger.channel.MessageVetoException;
32 import de.gluehloch.swinger.channel.VetoableMessageListener;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import org.apache.commons.lang.StringUtils;
38 import org.apache.commons.lang.Validate;
39
40 /**
41 * Implementiert einen Nachrichtenkanal.<br/>
42 *
43 * <b>Diese Klasse ist nicht thread-sicher.</b>
44 *
45 * @author by Andre Winkler, $LastChangedBy: andrewinkler $
46 * @version $LastChangedRevision: 3046 $ $LastChangedDate: 2011-12-17 16:28:31 +0100 (Sa, 17 Dez 2011) $
47 */
48 public final class DefaultChannel implements Channel {
49
50 /** Die Liste der registrierten {@link MessageListener}. */
51 private final List<MessageListener> listeners = new ArrayList<MessageListener>();
52
53 /** Ein Schlüssel für diesen Channel. */
54 private final String channelKey;
55
56 /**
57 * Konstruktor. Initialisiert einen neuen Kanal unter der übergebenen ID.
58 *
59 * @param _channelKey Der Schlüssel für diesen Kanal.
60 */
61 public DefaultChannel(final String _channelKey) {
62 Validate.isTrue(StringUtils.isNotBlank(_channelKey));
63 channelKey = _channelKey;
64 }
65
66 /**
67 * Der Schlüssel für diesen Channel.
68 *
69 * @see Channel#getChannelKey()
70 */
71 @Override
72 public String getChannelKey() {
73 return channelKey;
74 }
75
76 /**
77 * Registriert einen Listener.
78 *
79 * @param messageListener Ein Listener.
80 */
81 @Override
82 public void addMessageListener(final MessageListener messageListener) {
83 listeners.add(messageListener);
84 }
85
86 /**
87 * Entfernt einen Listener.
88 *
89 * @param messageListener Ein Listener.
90 */
91 @Override
92 public void removeMessageListener(final MessageListener messageListener) {
93 listeners.remove(messageListener);
94 }
95
96 /**
97 * Versendet eine Nachricht.
98 *
99 * @param message Eine Nachricht.
100 */
101 @Override
102 public void sendMessage(final Message message) {
103 for (MessageListener listener : listeners) {
104 listener.receiveMessage(message);
105 }
106 }
107
108 /**
109 * Versendet eine Nachricht, die evt. abgelehnt werden könnte.
110 *
111 * @param message Eine Nachricht.
112 * @throws MessageVetoException Die Nachricht wird abgelehnt.
113 */
114 @Override
115 public void sendVetoableMessage(final Message message)
116 throws MessageVetoException {
117
118 for (MessageListener listener : listeners) {
119 if (listener instanceof VetoableMessageListener) {
120 ((VetoableMessageListener) listener)
121 .checkVetoableMessage(message);
122 }
123 }
124 sendMessage(message);
125 }
126
127 @Override
128 public int hashCode() {
129 return (channelKey.hashCode());
130 }
131
132 @Override
133 public boolean equals(final Object object) {
134 boolean result = false;
135 if (object instanceof DefaultChannel) {
136 DefaultChannel dc = (DefaultChannel) object;
137 result = this.channelKey.equals(dc.channelKey);
138 }
139 return result;
140 }
141
142 }