1 25 package org.objectweb.joram.mom.proxies; 26 27 import java.io.*; 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import java.util.Vector ; 31 32 import fr.dyade.aaa.agent.AgentId; 33 import org.objectweb.joram.shared.client.AbstractJmsReply; 34 import org.objectweb.joram.shared.client.XACnxPrepare; 35 36 import org.objectweb.joram.shared.JoramTracing; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 43 class ClientContext implements java.io.Serializable { 44 45 private AgentId proxyId; 46 47 48 private int id; 49 50 private Vector tempDestinations; 51 52 private Hashtable deliveringQueues; 53 54 private Hashtable transactionsTable; 55 56 57 private transient boolean started; 58 62 private transient int cancelledRequestId; 63 64 private transient Vector activeSubs; 65 66 private transient Vector repliesBuffer; 67 68 private transient Hashtable commitTable; 69 70 private transient ProxyAgentItf proxy; 71 72 78 ClientContext(AgentId proxyId, int id) 79 { 80 this.proxyId = proxyId; 81 this.id = id; 82 83 tempDestinations = new Vector (); 84 deliveringQueues = new Hashtable (); 85 86 started = false; 87 cancelledRequestId = -1; 88 activeSubs = new Vector (); 89 repliesBuffer = new Vector (); 90 } 91 92 void setProxyAgent(ProxyAgentItf px) { 93 proxy = px; 94 } 95 96 97 int getId() 98 { 99 return id; 100 } 101 102 103 void setActivated(boolean started) { 104 if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG)) 105 JoramTracing.dbgProxy.log( 106 BasicLevel.DEBUG, 107 "ClientContext[" + proxyId + ',' + id + 108 "].setActivated(" + started + ')'); 109 this.started = started; 110 } 111 112 113 boolean getActivated() 114 { 115 return started; 116 } 117 118 119 void addTemporaryDestination(AgentId destId) 120 { 121 tempDestinations.add(destId); 122 proxy.setSave(); 123 } 124 125 126 Enumeration getTempDestinations() 127 { 128 return tempDestinations.elements(); 129 } 130 131 132 void removeTemporaryDestination(AgentId destId) 133 { 134 deliveringQueues.remove(destId); 135 tempDestinations.remove(destId); 136 proxy.setSave(); 137 } 138 139 140 void addPendingDelivery(AbstractJmsReply reply) { 141 if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG)) 142 JoramTracing.dbgProxy.log( 143 BasicLevel.DEBUG, 144 "ClientContext[" + proxyId + ',' + id + 145 "].addPendingDelivery(" + reply + ')'); 146 repliesBuffer.add(reply); 147 } 148 149 150 Enumeration getPendingDeliveries() 151 { 152 return repliesBuffer.elements(); 153 } 154 155 156 void clearPendingDeliveries() 157 { 158 repliesBuffer.clear(); 159 } 160 161 162 void addSubName(String subName) 163 { 164 activeSubs.add(subName); 165 } 166 167 168 Enumeration getActiveSubs() 169 { 170 return activeSubs.elements(); 171 } 172 173 174 void removeSubName(String subName) 175 { 176 activeSubs.remove(subName); 177 } 178 179 180 void cancelReceive(int cancelledRequestId) 181 { 182 if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG)) 183 JoramTracing.dbgProxy.log( 184 BasicLevel.DEBUG, 185 "ClientContext[" + proxyId + ':' + id + 186 "].cancelReceive(" + cancelledRequestId + ')'); 187 this.cancelledRequestId = cancelledRequestId; 188 } 189 190 191 int getCancelledReceive() 192 { 193 return cancelledRequestId; 194 } 195 196 197 void addDeliveringQueue(AgentId queueId) 198 { 199 deliveringQueues.put(queueId, queueId); 200 proxy.setSave(); 201 } 202 203 204 Enumeration getDeliveringQueues() 205 { 206 return deliveringQueues.keys(); 207 } 208 209 216 void addMultiReplyContext(int requestId, int asyncReplyCount) { 217 if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG)) 218 JoramTracing.dbgProxy.log(BasicLevel.DEBUG, 219 "ClientContext[" + proxyId + ':' + id + 220 "].addMultiReplyContext(" + requestId + ',' + 221 asyncReplyCount + ')'); 222 if (commitTable == null) commitTable = new Hashtable (); 223 commitTable.put( 224 new Integer (requestId), 225 new MultiReplyContext(asyncReplyCount)); 226 proxy.setSave(); 227 } 228 229 239 int setReply(int requestId) { 240 if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG)) 241 JoramTracing.dbgProxy.log(BasicLevel.DEBUG, 242 "ClientContext[" + proxyId + ':' + id + 243 "].setReply(" + requestId + ')'); 244 if (commitTable == null) return 0; 245 Integer ctxKey = (Integer )new Integer (requestId); 246 MultiReplyContext ctx = 247 (MultiReplyContext)commitTable.get(ctxKey); 248 if (ctx == null) return 0; 249 else { 250 ctx.counter--; 251 if (ctx.counter == 0) { 252 commitTable.remove(ctxKey); 253 proxy.setSave(); 254 } 255 return ctx.counter; 256 } 257 } 258 259 static class MultiReplyContext { 260 public int counter; 261 262 MultiReplyContext(int c) { 263 counter = c; 264 } 265 } 266 267 268 void registerTxPrepare(Object key, XACnxPrepare prepare) throws Exception { 269 if (transactionsTable == null) 270 transactionsTable = new Hashtable (); 271 272 if (! transactionsTable.containsKey(key)) { 273 transactionsTable.put(key, prepare); 274 proxy.setSave(); 275 } else 276 throw new Exception ("Prepare request already received by " 277 + "TM for this transaction."); 278 } 279 280 281 XACnxPrepare getTxPrepare(Object key) { 282 XACnxPrepare prepare = null; 283 if (transactionsTable != null) { 284 prepare = (XACnxPrepare) transactionsTable.remove(key); 285 proxy.setSave(); 286 } 287 return prepare; 288 } 289 290 291 Enumeration getTxIds() { 292 if (transactionsTable == null) 293 return new Hashtable ().keys(); 294 return transactionsTable.keys(); 295 } 296 297 public void readBag(ObjectInputStream in) 298 throws IOException, ClassNotFoundException { 299 started = in.readBoolean(); 300 cancelledRequestId = in.readInt(); 301 activeSubs = (Vector )in.readObject(); 302 repliesBuffer = (Vector )in.readObject(); 303 } 304 305 public void writeBag(ObjectOutputStream out) throws IOException { 306 out.writeBoolean(started); 307 out.writeInt(cancelledRequestId); 308 out.writeObject(activeSubs); 309 out.writeObject(repliesBuffer); 310 } 311 312 public String toString() { 313 StringBuffer buff = new StringBuffer (); 314 buff.append("ClientContext (proxyId="); 315 buff.append(proxyId); 316 buff.append(",id="); 317 buff.append(id); 318 buff.append(",tempDestinations="); 319 buff.append(tempDestinations); 320 buff.append(",deliveringQueues="); 321 buff.append(deliveringQueues); 322 buff.append(",transactionsTable="); 323 buff.append(transactionsTable); 324 buff.append(",started="); 325 buff.append(started); 326 buff.append(",cancelledRequestId="); 327 buff.append(cancelledRequestId); 328 buff.append(",activeSubs="); 329 buff.append(activeSubs); 330 buff.append(",repliesBuffer="); 331 buff.append(repliesBuffer); 332 buff.append(')'); 333 return buff.toString(); 334 } 335 } 336 | Popular Tags |