KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > impl > InMemoryClientStatePersistor


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

4 package com.tc.objectserver.persistence.impl;
5
6 import com.tc.net.protocol.tcm.ChannelID;
7 import com.tc.objectserver.persistence.api.ClientStatePersistor;
8 import com.tc.objectserver.persistence.api.PersistentSequence;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14
15 public class InMemoryClientStatePersistor implements ClientStatePersistor {
16
17   private final Map JavaDoc clients = new HashMap JavaDoc();
18   private final PersistentSequence connectionIDSequence = new InMemorySequenceProvider();
19
20   public PersistentSequence getConnectionIDSequence() {
21     return this.connectionIDSequence;
22   }
23
24   public Set JavaDoc loadClientIDs() {
25     synchronized (clients) {
26       return new HashSet JavaDoc(clients.keySet());
27     }
28   }
29
30   public boolean containsClient(ChannelID id) {
31     synchronized (clients) {
32       return clients.containsKey(id);
33     }
34   }
35
36   public void saveClientState(ChannelID cid) {
37     synchronized (clients) {
38       if (!containsClient(cid)) clients.put(cid, new Object JavaDoc());
39     }
40   }
41
42   public void deleteClientState(ChannelID id) throws ClientNotFoundException {
43     Object JavaDoc removed = null;
44     synchronized (clients) {
45       removed = clients.remove(id);
46     }
47     if (removed == null) throw new ClientNotFoundException("Client not found: " + id);
48   }
49
50 }
51
Popular Tags