1 5 package com.tc.net.protocol.tcm; 6 7 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList; 8 9 import com.tc.logging.TCLogger; 10 import com.tc.logging.TCLogging; 11 import com.tc.util.Assert; 12 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 25 class ChannelManagerImpl implements ChannelManager, ChannelEventListener, ServerMessageChannelFactory { 26 private static final TCLogger logger = TCLogging.getLogger(ChannelManager.class); 27 private static final MessageChannelInternal[] EMPTY_CHANNEL_ARARY = new MessageChannelInternal[] {}; 28 29 private final Map channels; 30 private final boolean transportDisconnectRemovesChannel; 31 private final ServerMessageChannelFactory channelFactory; 32 private final List eventListeners = new CopyOnWriteArrayList(); 33 34 public ChannelManagerImpl(boolean transportDisconnectRemovesChannel, ServerMessageChannelFactory channelFactory) { 35 this.channels = new HashMap (); 36 this.transportDisconnectRemovesChannel = transportDisconnectRemovesChannel; 37 this.channelFactory = channelFactory; 38 } 39 40 public MessageChannelInternal createNewChannel(ChannelID id) { 41 MessageChannelInternal channel = channelFactory.createNewChannel(id); 42 synchronized (this) { 43 channels.put(channel.getChannelID(), channel); 44 channel.addListener(this); 45 } 46 return channel; 47 } 48 49 private void fireChannelCreatedEvent(MessageChannel channel) { 50 for (Iterator iter = eventListeners.iterator(); iter.hasNext();) { 51 ChannelManagerEventListener eventListener = (ChannelManagerEventListener) iter.next(); 52 eventListener.channelCreated(channel); 53 } 54 } 55 56 private void fireChannelRemovedEvent(MessageChannel channel) { 57 for (Iterator iter = eventListeners.iterator(); iter.hasNext();) { 58 ChannelManagerEventListener eventListener = (ChannelManagerEventListener) iter.next(); 59 eventListener.channelRemoved(channel); 60 } 61 } 62 63 public synchronized MessageChannelInternal getChannel(ChannelID id) { 64 return (MessageChannelInternal) channels.get(id); 65 } 66 67 public synchronized MessageChannelInternal[] getChannels() { 68 return (MessageChannelInternal[]) channels.values().toArray(EMPTY_CHANNEL_ARARY); 69 } 70 71 public synchronized void closeAllChannels() { 72 MessageChannelInternal[] channelsCopy = getChannels(); 73 for (int i = 0; i < channelsCopy.length; i++) { 74 channelsCopy[i].close(); 75 } 76 Assert.assertEquals(0, channels.size()); 77 } 78 79 public synchronized Collection getAllChannelIDs() { 80 return new HashSet (channels.keySet()); 81 } 82 83 public synchronized boolean isValidID(ChannelID channelID) { 84 if (channelID == null) { return false; } 85 86 final MessageChannel channel = getChannel(channelID); 87 88 if (channel == null) { 89 logger.warn("no channel found for " + channelID); 90 return false; 91 } 92 93 return true; 94 } 95 96 public void notifyChannelEvent(ChannelEvent event) { 97 MessageChannel channel = event.getChannel(); 98 99 if (ChannelEventType.CHANNEL_CLOSED_EVENT.matches(event)) { 100 removeChannel(channel); 101 } else if (ChannelEventType.TRANSPORT_DISCONNECTED_EVENT.matches(event)) { 102 if (this.transportDisconnectRemovesChannel) { 103 channel.close(); 104 } 105 } else if (ChannelEventType.TRANSPORT_CONNECTED_EVENT.matches(event)) { 106 fireChannelCreatedEvent(channel); 107 } 108 } 109 110 private void removeChannel(MessageChannel channel) { 111 synchronized (this) { 112 channels.remove(channel.getChannelID()); 113 } 114 fireChannelRemovedEvent(channel); 115 } 116 117 public synchronized void addEventListener(ChannelManagerEventListener listener) { 118 if (listener == null) { throw new IllegalArgumentException ("listener must be non-null"); } 119 this.eventListeners.add(listener); 120 } 121 122 } | Popular Tags |