01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.objectserver.lockmanager.impl;
06:
07: import com.tc.net.groups.NodeID;
08: import com.tc.object.lockmanager.api.ServerThreadID;
09: import com.tc.object.lockmanager.api.ThreadID;
10:
11: import java.util.Collection;
12: import java.util.Collections;
13: import java.util.HashMap;
14: import java.util.Iterator;
15: import java.util.Map;
16:
17: class ServerThreadContextFactory {
18: public static final ServerThreadContextFactory DEFAULT_FACTORY = new ServerThreadContextFactory();
19: public static final String ACTIVITY_MONITOR_NAME = "ServerThreadContextFactoryActivityMonitor";
20:
21: private final Map currentContexts = Collections
22: .synchronizedMap(new HashMap());
23:
24: int getCount() {
25: return currentContexts.size();
26: }
27:
28: ServerThreadContext getOrCreate(NodeID nid, ThreadID threadID) {
29: ServerThreadID id = new ServerThreadID(nid, threadID);
30:
31: synchronized (currentContexts) {
32: ServerThreadContext threadContext = (ServerThreadContext) this .currentContexts
33: .get(id);
34: if (threadContext == null) {
35: threadContext = new ServerThreadContext(id);
36: this .currentContexts.put(id, threadContext);
37: }
38: return threadContext;
39: }
40: }
41:
42: Object remove(ServerThreadContext context) {
43: return currentContexts.remove(context.getId());
44: }
45:
46: void clear(NodeID nid) {
47: synchronized (currentContexts) {
48: for (Iterator i = currentContexts.values().iterator(); i
49: .hasNext();) {
50: ServerThreadContext threadContext = (ServerThreadContext) i
51: .next();
52: if (threadContext.getId().getNodeID().equals(nid)) {
53: i.remove();
54: }
55: }
56: }
57: }
58:
59: void clear() {
60: currentContexts.clear();
61: }
62:
63: void removeIfClear(ServerThreadContext threadCtx) {
64: if (threadCtx.isClear()) {
65: remove(threadCtx);
66: }
67: }
68:
69: Collection getView() {
70: synchronized (currentContexts) {
71: return Collections.unmodifiableCollection(currentContexts
72: .values());
73: }
74: }
75: }
|