KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > sleepycat > ConnectionIDFactoryImpl


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

5 package com.tc.objectserver.persistence.sleepycat;
6
7 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
8
9 import com.tc.net.protocol.tcm.ChannelID;
10 import com.tc.net.protocol.tcm.MessageChannel;
11 import com.tc.net.protocol.transport.ConnectionID;
12 import com.tc.net.protocol.transport.ConnectionIDFactoryListener;
13 import com.tc.net.protocol.transport.ConnectionIDFactory;
14 import com.tc.object.net.DSOChannelManagerEventListener;
15 import com.tc.objectserver.persistence.api.ClientStatePersistor;
16 import com.tc.objectserver.persistence.api.PersistentSequence;
17 import com.tc.objectserver.persistence.impl.ClientNotFoundException;
18 import com.tc.util.Assert;
19
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24
25 public class ConnectionIDFactoryImpl implements ConnectionIDFactory, DSOChannelManagerEventListener {
26
27   private final ClientStatePersistor clientStateStore;
28   private final PersistentSequence connectionIDSequence;
29   private String JavaDoc uid;
30   private List listeners = new CopyOnWriteArrayList();
31
32   public ConnectionIDFactoryImpl(ClientStatePersistor clientStateStore) {
33     this.clientStateStore = clientStateStore;
34     this.connectionIDSequence = clientStateStore.getConnectionIDSequence();
35     this.uid = connectionIDSequence.getUID();
36   }
37
38   public ConnectionID nextConnectionId() {
39     Assert.assertNotNull(uid);
40     long clientID = connectionIDSequence.next();
41     // Make sure we save the fact that we are giving out this id to someone in the database before giving it out.
42
clientStateStore.saveClientState(new ChannelID(clientID));
43     ConnectionID rv = new ConnectionID(clientID, uid);
44     fireCreationEvent(rv);
45     return rv;
46   }
47
48   private void fireCreationEvent(ConnectionID rv) {
49     for (Iterator JavaDoc i = listeners.iterator(); i.hasNext();) {
50       ConnectionIDFactoryListener listener = (ConnectionIDFactoryListener) i.next();
51       listener.connectionIDCreated(rv);
52     }
53   }
54
55   private void fireDestroyedEvent(ConnectionID connectionID) {
56     for (Iterator JavaDoc i = listeners.iterator(); i.hasNext();) {
57       ConnectionIDFactoryListener listener = (ConnectionIDFactoryListener) i.next();
58       listener.connectionIDDestroyed(connectionID);
59     }
60   }
61
62   public void init(String JavaDoc clusterID, long nextAvailChannelID, Set JavaDoc connections) {
63     this.uid = clusterID;
64     if (nextAvailChannelID >= 0) {
65       this.connectionIDSequence.setNext(nextAvailChannelID);
66     }
67     for (Iterator JavaDoc i = connections.iterator(); i.hasNext();) {
68       ConnectionID cid = (ConnectionID) i.next();
69       Assert.assertEquals(clusterID, cid.getServerID());
70       this.clientStateStore.saveClientState(new ChannelID(cid.getChannelID()));
71     }
72   }
73
74   public Set JavaDoc loadConnectionIDs() {
75     Assert.assertNotNull(uid);
76     Set JavaDoc connections = new HashSet JavaDoc();
77     for (Iterator JavaDoc i = clientStateStore.loadClientIDs().iterator(); i.hasNext();) {
78       connections.add(new ConnectionID(((ChannelID) i.next()).toLong(), uid));
79     }
80     return connections;
81   }
82
83   public void registerForConnectionIDEvents(ConnectionIDFactoryListener listener) {
84     listeners.add(listener);
85   }
86
87   public void channelCreated(MessageChannel channel) {
88     // NOP
89
}
90
91   public void channelRemoved(MessageChannel channel) {
92     ChannelID clientID = channel.getChannelID();
93     try {
94       clientStateStore.deleteClientState(clientID);
95     } catch (ClientNotFoundException e) {
96       throw new AssertionError JavaDoc(e);
97     }
98     fireDestroyedEvent(new ConnectionID(clientID.toLong(), uid));
99   }
100
101 }
102
Popular Tags