KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > lockmanager > impl > ServerThreadContextFactory


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.lockmanager.impl;
5
6 import com.tc.net.protocol.tcm.ChannelID;
7 import com.tc.object.lockmanager.api.ServerThreadID;
8 import com.tc.object.lockmanager.api.ThreadID;
9
10 import java.util.Collection JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 class ServerThreadContextFactory {
17   public static final ServerThreadContextFactory DEFAULT_FACTORY = new ServerThreadContextFactory();
18   public static final String JavaDoc ACTIVITY_MONITOR_NAME = "ServerThreadContextFactoryActivityMonitor";
19
20   private final Map JavaDoc currentContexts = Collections.synchronizedMap(new HashMap JavaDoc());
21
22   int getCount() {
23     return currentContexts.size();
24   }
25
26   ServerThreadContext getOrCreate(ChannelID channelID, ThreadID threadID) {
27     ServerThreadID id = new ServerThreadID(channelID, threadID);
28
29     synchronized (currentContexts) {
30       ServerThreadContext threadContext = (ServerThreadContext) this.currentContexts.get(id);
31       if (threadContext == null) {
32         threadContext = new ServerThreadContext(id);
33         this.currentContexts.put(id, threadContext);
34       }
35       return threadContext;
36     }
37   }
38
39   Object JavaDoc remove(ServerThreadContext context) {
40     return currentContexts.remove(context.getId());
41   }
42
43   void clear(ChannelID channelID) {
44     synchronized (currentContexts) {
45       for (Iterator JavaDoc i = currentContexts.values().iterator(); i.hasNext();) {
46         ServerThreadContext threadContext = (ServerThreadContext) i.next();
47         if (threadContext.getId().getChannelID().equals(channelID)) {
48           i.remove();
49         }
50       }
51     }
52   }
53
54   void clear() {
55     currentContexts.clear();
56   }
57
58   void removeIfClear(ServerThreadContext threadCtx) {
59     if (threadCtx.isClear()) {
60       remove(threadCtx);
61     }
62   }
63
64   Collection JavaDoc getView() {
65     synchronized (currentContexts) {
66       return Collections.unmodifiableCollection(currentContexts.values());
67     }
68   }
69 }
70
Popular Tags