KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > tcm > ChannelManagerImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * provides the sessionIDs
22  *
23  * @author steve
24  */

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 JavaDoc 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 JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc getAllChannelIDs() {
80     return new HashSet JavaDoc(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 JavaDoc("listener must be non-null"); }
119     this.eventListeners.add(listener);
120   }
121
122 }
Popular Tags