1 4 package com.tc.objectserver.impl; 5 6 import com.tc.logging.TCLogger; 7 import com.tc.logging.TCLogging; 8 import com.tc.net.protocol.tcm.ChannelID; 9 import com.tc.object.tx.ServerTransactionID; 10 import com.tc.objectserver.api.ObjectManager; 11 import com.tc.objectserver.api.ObjectRequestManager; 12 import com.tc.objectserver.context.ManagedObjectRequestContext; 13 import com.tc.objectserver.tx.ServerTransactionListener; 14 import com.tc.objectserver.tx.ServerTransactionManager; 15 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Collections ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 import java.util.Set ; 24 25 public class ObjectRequestManagerImpl implements ObjectRequestManager, ServerTransactionListener { 26 27 private final static TCLogger logger = TCLogging.getLogger(ObjectRequestManagerImpl.class); 28 29 private final Set resentTransactionIDs = new HashSet (); 30 private final ObjectManager objectManager; 31 private final List pendingRequests = Collections.synchronizedList(new LinkedList ()); 32 33 public ObjectRequestManagerImpl(ObjectManager objectManager, ServerTransactionManager transactionManager) { 34 this.objectManager = objectManager; 35 transactionManager.addTransactionListener(this); 36 37 } 38 39 public void requestObjects(Collection requestedIDs, ManagedObjectRequestContext responseContext, 40 int maxReachableObjects) { 41 synchronized (this) { 42 if (!resentTransactionIDs.isEmpty()) { 43 pendingRequests.add(new PendingRequest(requestedIDs, responseContext, maxReachableObjects)); 44 return; 45 } 46 } 47 objectManager.lookupObjectsAndSubObjectsFor(responseContext.getChannelID(), responseContext, maxReachableObjects); 48 } 49 50 public synchronized void addResentServerTransactionIDs(Collection sTxIDs) { 51 resentTransactionIDs.addAll(sTxIDs); 52 logger.info("resentTransactions = " + resentTransactionIDs); 53 } 54 55 public void transactionCompleted(ServerTransactionID stxID) { 56 return; 57 } 58 59 public void incomingTransactions(ChannelID cid, Set serverTxnIDs) { 60 return; 61 } 62 63 public void clearAllTransactionsFor(ChannelID client) { 64 synchronized (this) { 65 if (resentTransactionIDs.isEmpty()) return; 66 for (Iterator iter = resentTransactionIDs.iterator(); iter.hasNext();) { 67 ServerTransactionID stxID = (ServerTransactionID) iter.next(); 68 if (stxID.getChannelID().equals(client)) { 69 iter.remove(); 70 } 71 } 72 if (!resentTransactionIDs.isEmpty()) return; 73 } 74 processPending(); 75 } 76 77 private void processPending() { 78 List copy; 79 synchronized (pendingRequests) { 80 copy = new ArrayList (pendingRequests); 81 pendingRequests.clear(); 82 } 83 logger.info("Processing Pending Lookups = " + copy.size()); 84 for (Iterator iter = copy.iterator(); iter.hasNext();) { 85 PendingRequest request = (PendingRequest) iter.next(); 86 objectManager.lookupObjectsAndSubObjectsFor(request.getResponseContext().getChannelID(), request.getResponseContext(), request.getMaxReachableObjects()); 87 } 88 } 89 90 public void transactionApplied(ServerTransactionID stxID) { 91 synchronized (this) { 92 if (resentTransactionIDs.isEmpty()) return; 93 if (resentTransactionIDs.remove(stxID) && !resentTransactionIDs.isEmpty()) { return; } 94 } 95 processPending(); 96 } 97 98 public static class PendingRequest { 99 100 private final Collection requestedIDs; 101 private final ManagedObjectRequestContext responseContext; 102 private final int maxReachableObjects; 103 104 public PendingRequest(Collection requestedIDs, ManagedObjectRequestContext responseContext, int maxReachableObjects) { 105 this.requestedIDs = requestedIDs; 106 this.responseContext = responseContext; 107 this.maxReachableObjects = maxReachableObjects; 108 } 109 110 public int getMaxReachableObjects() { 111 return maxReachableObjects; 112 } 113 114 public Collection getRequestedIDs() { 115 return requestedIDs; 116 } 117 118 public ManagedObjectRequestContext getResponseContext() { 119 return responseContext; 120 } 121 } 122 } 123 | Popular Tags |