KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > impl > ObjectRequestManagerImpl


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.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 JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24
25 public class ObjectRequestManagerImpl implements ObjectRequestManager, ServerTransactionListener {
26
27   private final static TCLogger logger = TCLogging.getLogger(ObjectRequestManagerImpl.class);
28
29   private final Set JavaDoc resentTransactionIDs = new HashSet JavaDoc();
30   private final ObjectManager objectManager;
31   private final List JavaDoc pendingRequests = Collections.synchronizedList(new LinkedList JavaDoc());
32
33   public ObjectRequestManagerImpl(ObjectManager objectManager, ServerTransactionManager transactionManager) {
34     this.objectManager = objectManager;
35     transactionManager.addTransactionListener(this);
36
37   }
38
39   public void requestObjects(Collection JavaDoc 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 JavaDoc 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 JavaDoc serverTxnIDs) {
60     return;
61   }
62
63   public void clearAllTransactionsFor(ChannelID client) {
64     synchronized (this) {
65       if (resentTransactionIDs.isEmpty()) return;
66       for (Iterator JavaDoc 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 JavaDoc copy;
79     synchronized (pendingRequests) {
80       copy = new ArrayList JavaDoc(pendingRequests);
81       pendingRequests.clear();
82     }
83     logger.info("Processing Pending Lookups = " + copy.size());
84     for (Iterator JavaDoc 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 JavaDoc requestedIDs;
101     private final ManagedObjectRequestContext responseContext;
102     private final int maxReachableObjects;
103
104     public PendingRequest(Collection JavaDoc 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 JavaDoc getRequestedIDs() {
115       return requestedIDs;
116     }
117
118     public ManagedObjectRequestContext getResponseContext() {
119       return responseContext;
120     }
121   }
122 }
123
Popular Tags