1 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 ; 11 import java.util.Collections ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 16 class ServerThreadContextFactory { 17 public static final ServerThreadContextFactory DEFAULT_FACTORY = new ServerThreadContextFactory(); 18 public static final String ACTIVITY_MONITOR_NAME = "ServerThreadContextFactoryActivityMonitor"; 19 20 private final Map currentContexts = Collections.synchronizedMap(new HashMap ()); 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 remove(ServerThreadContext context) { 40 return currentContexts.remove(context.getId()); 41 } 42 43 void clear(ChannelID channelID) { 44 synchronized (currentContexts) { 45 for (Iterator 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 getView() { 65 synchronized (currentContexts) { 66 return Collections.unmodifiableCollection(currentContexts.values()); 67 } 68 } 69 } 70 | Popular Tags |