KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > mom > proxies > ClientContext


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - 200 ScalAgent Distributed Technologies
4  * Copyright (C) 2004 France Telecom R&D
5  * Copyright (C) 2003 - 2004 Bull SA
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA.
21  *
22  * Initial developer(s): Frederic Maistre (Bull)
23  * Contributor(s): ScalAgent Distributed Technologies
24  */

25 package org.objectweb.joram.mom.proxies;
26
27 import java.io.*;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Vector JavaDoc;
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 /**
40  * The <code>ClientContext</code> class holds the data related to a client
41  * context.
42  */

43 class ClientContext implements java.io.Serializable JavaDoc {
44   /** The proxy's agent identifier. */
45   private AgentId proxyId;
46
47   /** Context identifier. */
48   private int id;
49   /** Vector of temporary destinations. */
50   private Vector JavaDoc tempDestinations;
51   /** Identifiers of queues delivering messages. */
52   private Hashtable JavaDoc deliveringQueues;
53   /** Prepared transactions objects waiting for commit. */
54   private Hashtable JavaDoc transactionsTable;
55
56   /** <code>true</code> if the context is activated. */
57   private transient boolean started;
58   /**
59    * Identifier of a cancelled "receive" request, set when a PTP listener has
60    * been unset.
61    */

62   private transient int cancelledRequestId;
63   /** Vector of active subscriptions' names. */
64   private transient Vector JavaDoc activeSubs;
65   /** Pending replies waiting for the context to be activated. */
66   private transient Vector JavaDoc repliesBuffer;
67   /** Contexts waiting for the replies from some local agents*/
68   private transient Hashtable JavaDoc commitTable;
69   
70   private transient ProxyAgentItf proxy;
71
72   /**
73    * Constructs a <code>ClientContext</code> instance.
74    *
75    * @param proxyId The proxy's agent identifier.
76    * @param id Identifier of the context.
77    */

78   ClientContext(AgentId proxyId, int id)
79   {
80     this.proxyId = proxyId;
81     this.id = id;
82
83     tempDestinations = new Vector JavaDoc();
84     deliveringQueues = new Hashtable JavaDoc();
85
86     started = false;
87     cancelledRequestId = -1;
88     activeSubs = new Vector JavaDoc();
89     repliesBuffer = new Vector JavaDoc();
90   }
91   
92   void setProxyAgent(ProxyAgentItf px) {
93     proxy = px;
94   }
95  
96   /** Returns the identifier of the context. */
97   int getId()
98   {
99     return id;
100   }
101
102   /** Sets the activation status of the context. */
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   /** Returns <code>true</code> if the context is activated. */
113   boolean getActivated()
114   {
115     return started;
116   }
117
118   /** Adds a temporary destination identifier. */
119   void addTemporaryDestination(AgentId destId)
120   {
121     tempDestinations.add(destId);
122     proxy.setSave();
123   }
124    
125   /** Returns the temporary destinations' identifiers. */
126   Enumeration JavaDoc getTempDestinations()
127   {
128     return tempDestinations.elements();
129   }
130
131   /** Removes a temporary destination identifier. */
132   void removeTemporaryDestination(AgentId destId)
133   {
134     deliveringQueues.remove(destId);
135     tempDestinations.remove(destId);
136     proxy.setSave();
137   }
138
139   /** Adds a pending delivery. */
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   /** Returns the pending deliveries. */
150   Enumeration JavaDoc getPendingDeliveries()
151   {
152     return repliesBuffer.elements();
153   }
154
155   /** Clears the pending deliveries buffer. */
156   void clearPendingDeliveries()
157   {
158     repliesBuffer.clear();
159   }
160
161   /** Adds an active subscription name. */
162   void addSubName(String JavaDoc subName)
163   {
164     activeSubs.add(subName);
165   }
166
167   /** Returns the active subscriptions' names. */
168   Enumeration JavaDoc getActiveSubs()
169   {
170     return activeSubs.elements();
171   }
172
173   /** Removes an active subscription name. */
174   void removeSubName(String JavaDoc subName)
175   {
176     activeSubs.remove(subName);
177   }
178   
179   /** Cancels a "receive" request. */
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   /** Returns the cancelled "receive" request identifier. */
191   int getCancelledReceive()
192   {
193     return cancelledRequestId;
194   }
195
196   /** Adds the identifier of a delivering queue. */
197   void addDeliveringQueue(AgentId queueId)
198   {
199     deliveringQueues.put(queueId, queueId);
200     proxy.setSave();
201   }
202
203   /** Returns the identifiers of the delivering queues. */
204   Enumeration JavaDoc getDeliveringQueues()
205   {
206     return deliveringQueues.keys();
207   }
208   
209   /**
210    * Some requests may require to wait for several
211    * SendReplyNot notifications before replying to the client.
212    *
213    * @param requestId
214    * @param asyncReplyCount
215    */

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 JavaDoc();
223     commitTable.put(
224         new Integer JavaDoc(requestId),
225         new MultiReplyContext(asyncReplyCount));
226     proxy.setSave();
227   }
228   
229   /**
230    * Called by UserAgent when a SendReplyNot
231    * arrived.
232    *
233    * @param requestId
234    * @return
235    * > 0 if there are still some pending replies
236    * 0 if all the replies arrived (the context is removed)
237    * or if the context doesn't exist
238    */

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 JavaDoc ctxKey = (Integer JavaDoc)new Integer JavaDoc(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   /** Registers a given transaction "prepare". */
268   void registerTxPrepare(Object JavaDoc key, XACnxPrepare prepare) throws Exception JavaDoc {
269     if (transactionsTable == null)
270       transactionsTable = new Hashtable JavaDoc();
271
272     if (! transactionsTable.containsKey(key)) {
273       transactionsTable.put(key, prepare);
274       proxy.setSave();
275     } else
276       throw new Exception JavaDoc("Prepare request already received by "
277                           + "TM for this transaction.");
278   }
279
280   /** Returns and deletes a given transaction "prepare". */
281   XACnxPrepare getTxPrepare(Object JavaDoc 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   /** Returns the identifiers of the prepared transactions. */
291   Enumeration JavaDoc getTxIds() {
292     if (transactionsTable == null)
293       return new Hashtable JavaDoc().keys();
294     return transactionsTable.keys();
295   }
296
297   public void readBag(ObjectInputStream in)
298     throws IOException, ClassNotFoundException JavaDoc {
299     started = in.readBoolean();
300     cancelledRequestId = in.readInt();
301     activeSubs = (Vector JavaDoc)in.readObject();
302     repliesBuffer = (Vector JavaDoc)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 JavaDoc toString() {
313     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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