KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > QueueBrowser


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

24 package com.scalagent.kjoram;
25
26 import com.scalagent.kjoram.jms.*;
27
28 import java.util.*;
29
30 import com.scalagent.kjoram.excepts.IllegalStateException;
31 import com.scalagent.kjoram.excepts.*;
32
33
34 public class QueueBrowser
35 {
36   /** The session the browser belongs to. */
37   private Session sess;
38   /** The queue the browser browses. */
39   private Queue queue;
40   /** The selector for filtering messages. */
41   private String JavaDoc selector;
42   /** <code>true</code> if the browser is closed. */
43   private boolean closed = false;
44
45   /**
46    * Constructs a browser.
47    *
48    * @param sess The session the browser belongs to.
49    * @param queue The queue the browser browses.
50    * @param selector The selector for filtering messages.
51    *
52    * @exception InvalidSelectorException If the selector syntax is invalid.
53    * @exception IllegalStateException If the connection is broken.
54    * @exception JMSException If the creation fails for any other reason.
55    */

56   QueueBrowser(Session sess, Queue queue, String JavaDoc selector) throws JMSException
57   {
58     if (queue == null)
59       throw new InvalidDestinationException("Invalid queue: " + queue);
60
61     this.sess = sess;
62     this.queue = queue;
63     this.selector = selector;
64
65     if (sess.browsers == null)
66       sess.browsers = new Vector();
67     sess.browsers.addElement(this);
68
69     if (JoramTracing.dbgClient)
70       JoramTracing.log(JoramTracing.DEBUG, this + ": created.");
71   }
72
73   /** Returns a string view of this browser. */
74   public String JavaDoc toString()
75   {
76     return "QueueBrowser:" + sess.ident;
77   }
78
79   /**
80    * API method.
81    *
82    * @exception IllegalStateException If the browser is closed.
83    */

84   public Queue getQueue() throws JMSException
85   {
86     if (closed)
87       throw new IllegalStateException JavaDoc("Forbidden call on a closed browser.");
88
89     return queue;
90   }
91
92   /**
93    * API method.
94    *
95    * @exception IllegalStateException If the browser is closed.
96    */

97   public String JavaDoc getMessageSelector() throws JMSException
98   {
99     if (closed)
100       throw new IllegalStateException JavaDoc("Forbidden call on a closed browser.");
101
102     return selector;
103   }
104
105   /**
106    * API method.
107    *
108    * @exception IllegalStateException If the browser is closed, or if the
109    * connection is broken.
110    * @exception JMSSecurityException If the client is not a READER on the
111    * queue.
112    * @exception JMSException If the request fails for any other reason.
113    */

114   public Enumeration getEnumeration() throws JMSException
115   {
116     if (JoramTracing.dbgClient)
117       JoramTracing.log(JoramTracing.DEBUG, this
118                        + ": requests an enumeration.");
119     if (closed)
120       throw new IllegalStateException JavaDoc("Forbidden call on a closed browser.");
121
122     // Sending a "browse" request:
123
QBrowseRequest browReq = new QBrowseRequest(queue.getName(), selector);
124     // Expecting an answer:
125
QBrowseReply reply = (QBrowseReply) sess.cnx.syncRequest(browReq);
126
127     if (JoramTracing.dbgClient)
128       JoramTracing.log(JoramTracing.DEBUG, this
129                        + ": received an enumeration.");
130
131     // Processing the received messages:
132
Vector momMessages = reply.getMessages();
133     Vector messages = null;
134     if (momMessages != null) {
135       messages = new Vector();
136       com.scalagent.kjoram.messages.Message momMsg;
137       for (int i = 0; i < momMessages.size(); i++) {
138         momMsg = (com.scalagent.kjoram.messages.Message) momMessages.elementAt(i);
139         messages.addElement(Message.wrapMomMessage(null, momMsg));
140       }
141     }
142                           
143     // Return an enumeration:
144
return new QueueEnumeration(messages);
145   }
146
147   /**
148    * API method.
149    *
150    * @exception JMSException Actually never thrown.
151    */

152   public void close() throws JMSException
153   {
154     // Ignoring the call if the browser is already closed:
155
if (closed)
156       return;
157
158     sess.browsers.removeElement(this);
159     closed = true;
160
161     if (JoramTracing.dbgClient)
162       JoramTracing.log(JoramTracing.DEBUG, this + " closed.");
163   }
164
165   /**
166    * The <code>QueueEnumeration</code> class is used to enumerate the browses
167    * sent by queues.
168    */

169   private class QueueEnumeration implements java.util.Enumeration JavaDoc
170   {
171     /** The vector of messages. */
172     private Vector messages;
173
174     /**
175      * Constructs a <code>QueueEnumeration</code> instance.
176      *
177      * @param messages The vector of messages to enumerate.
178      */

179     private QueueEnumeration(Vector messages)
180     {
181       this.messages = messages;
182     }
183
184     /** API method. */
185     public boolean hasMoreElements()
186     {
187       if (messages == null)
188         return false;
189       return (! messages.isEmpty());
190     }
191
192     /** API method. */
193     public Object JavaDoc nextElement()
194     {
195       if (messages == null || messages.isEmpty())
196         throw new NoSuchElementException();
197
198       Object JavaDoc ret = messages.elementAt(0);
199       messages.removeElementAt(0);
200       return ret;
201     }
202   }
203 }
204
Popular Tags