KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > QueueBrowser


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 2000 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): ScalAgent Distributed Technologies
23  */

24 package org.objectweb.joram.client.jms;
25
26 import java.util.*;
27
28 import javax.jms.InvalidSelectorException JavaDoc;
29 import javax.jms.InvalidDestinationException JavaDoc;
30 import javax.jms.IllegalStateException JavaDoc;
31 import javax.jms.JMSException JavaDoc;
32
33 import org.objectweb.joram.shared.client.*;
34 import org.objectweb.joram.shared.selectors.ClientSelector;
35
36 import org.objectweb.util.monolog.api.BasicLevel;
37 import org.objectweb.joram.shared.JoramTracing;
38
39 /**
40  * Implements the <code>javax.jms.QueueBrowser</code> interface.
41  */

42 public class QueueBrowser implements javax.jms.QueueBrowser JavaDoc {
43   /** The session the browser belongs to. */
44   private Session sess;
45
46   /** The queue the browser browses. */
47   private Queue queue;
48
49   /** The selector for filtering messages. */
50   private String JavaDoc selector;
51
52   /** <code>true</code> if the browser is closed. */
53   private boolean closed = false;
54
55   /**
56    * Constructs a browser.
57    *
58    * @param sess The session the browser belongs to.
59    * @param queue The queue the browser browses.
60    * @param selector The selector for filtering messages.
61    *
62    * @exception InvalidSelectorException If the selector syntax is invalid.
63    * @exception IllegalStateException If the connection is broken.
64    * @exception JMSException If the creation fails for any other reason.
65    */

66   QueueBrowser(Session sess, Queue queue, String JavaDoc selector) throws JMSException JavaDoc {
67     if (queue == null)
68       throw new InvalidDestinationException JavaDoc("Invalid queue: " + queue);
69
70     try {
71       ClientSelector.checks(selector);
72     } catch (org.objectweb.joram.shared.excepts.SelectorException sE) {
73       throw new InvalidSelectorException JavaDoc("Invalid selector syntax: " + sE);
74     }
75
76     this.sess = sess;
77     this.queue = queue;
78     this.selector = selector;
79
80     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
81       JoramTracing.dbgClient.log(BasicLevel.DEBUG, this + ": created.");
82   }
83
84   /** Returns a string view of this browser. */
85   public String JavaDoc toString() {
86     return "QueueBrowser:" + sess.getId();
87   }
88
89   /**
90    * API method.
91    *
92    * @exception IllegalStateException If the browser is closed.
93    */

94   public synchronized javax.jms.Queue JavaDoc getQueue() throws JMSException JavaDoc {
95     if (closed)
96       throw new IllegalStateException JavaDoc("Forbidden call on a closed browser.");
97
98     return queue;
99   }
100
101   /**
102    * API method.
103    *
104    * @exception IllegalStateException If the browser is closed.
105    */

106   public synchronized String JavaDoc getMessageSelector() throws JMSException JavaDoc {
107     if (closed)
108       throw new IllegalStateException JavaDoc("Forbidden call on a closed browser.");
109
110     return selector;
111   }
112
113   /**
114    * API method.
115    *
116    * @exception IllegalStateException If the browser is closed, or if the
117    * connection is broken.
118    * @exception JMSSecurityException If the client is not a READER on the
119    * queue.
120    * @exception JMSException If the request fails for any other reason.
121    */

122   public synchronized Enumeration getEnumeration() throws JMSException JavaDoc {
123     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
124       JoramTracing.dbgClient.log(BasicLevel.DEBUG,
125                                  this + ": requests an enumeration.");
126
127     if (closed)
128       throw new IllegalStateException JavaDoc("Forbidden call on a closed browser.");
129
130     // Sending a "browse" request:
131
QBrowseRequest browReq = new QBrowseRequest(queue.getName(), selector);
132     // Expecting an answer:
133
QBrowseReply reply = (QBrowseReply) sess.syncRequest(browReq);
134
135     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
136       JoramTracing.dbgClient.log(BasicLevel.DEBUG,
137                                  this + ": received an enumeration.");
138
139     // Processing the received messages
140
Vector messages = reply.getMessages();
141     // Return an enumeration:
142
return new QueueEnumeration(reply.getMessages());
143   }
144
145   /**
146    * API method.
147    *
148    * @exception JMSException Actually never thrown.
149    */

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

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

175     private QueueEnumeration(Vector messages) {
176       this.messages = messages;
177     }
178
179     /** API method. */
180     public boolean hasMoreElements() {
181       if (messages == null)
182         return false;
183       return (! messages.isEmpty());
184     }
185
186     /** API method. */
187     public Object JavaDoc nextElement() {
188       if (messages == null || messages.isEmpty())
189         throw new NoSuchElementException();
190
191       Message jmsMsg = null;
192       org.objectweb.joram.shared.messages.Message msg = null;
193       try {
194         msg = (org.objectweb.joram.shared.messages.Message) messages.remove(0);
195         jmsMsg = Message.wrapMomMessage(null, msg);
196       } catch (JMSException JavaDoc exc) {
197         JoramTracing.dbgClient.log(BasicLevel.ERROR,
198                                    this + ", bad message: " + msg, exc);
199       }
200       return jmsMsg;
201     }
202   }
203 }
204
Popular Tags