1 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 ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Set ; 24 25 public class ConnectionIDFactoryImpl implements ConnectionIDFactory, DSOChannelManagerEventListener { 26 27 private final ClientStatePersistor clientStateStore; 28 private final PersistentSequence connectionIDSequence; 29 private String 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 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 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 i = listeners.iterator(); i.hasNext();) { 57 ConnectionIDFactoryListener listener = (ConnectionIDFactoryListener) i.next(); 58 listener.connectionIDDestroyed(connectionID); 59 } 60 } 61 62 public void init(String clusterID, long nextAvailChannelID, Set connections) { 63 this.uid = clusterID; 64 if (nextAvailChannelID >= 0) { 65 this.connectionIDSequence.setNext(nextAvailChannelID); 66 } 67 for (Iterator 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 loadConnectionIDs() { 75 Assert.assertNotNull(uid); 76 Set connections = new HashSet (); 77 for (Iterator 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 } 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 (e); 97 } 98 fireDestroyedEvent(new ConnectionID(clientID.toLong(), uid)); 99 } 100 101 } 102 | Popular Tags |