00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package com.scalagent.kjoram;
00025
00026 import com.scalagent.kjoram.jms.*;
00027
00028 import java.util.*;
00029
00030 import com.scalagent.kjoram.excepts.IllegalStateException;
00031 import com.scalagent.kjoram.excepts.*;
00032
00033
00034 public class QueueBrowser
00035 {
00037 private Session sess;
00039 private Queue queue;
00041 private String selector;
00043 private boolean closed = false;
00044
00056 QueueBrowser(Session sess, Queue queue, String selector) throws JMSException
00057 {
00058 if (queue == null)
00059 throw new InvalidDestinationException("Invalid queue: " + queue);
00060
00061 this.sess = sess;
00062 this.queue = queue;
00063 this.selector = selector;
00064
00065 if (sess.browsers == null)
00066 sess.browsers = new Vector();
00067 sess.browsers.addElement(this);
00068
00069 if (JoramTracing.dbgClient)
00070 JoramTracing.log(JoramTracing.DEBUG, this + ": created.");
00071 }
00072
00074 public String toString()
00075 {
00076 return "QueueBrowser:" + sess.ident;
00077 }
00078
00084 public Queue getQueue() throws JMSException
00085 {
00086 if (closed)
00087 throw new IllegalStateException("Forbidden call on a closed browser.");
00088
00089 return queue;
00090 }
00091
00097 public String getMessageSelector() throws JMSException
00098 {
00099 if (closed)
00100 throw new IllegalStateException("Forbidden call on a closed browser.");
00101
00102 return selector;
00103 }
00104
00114 public Enumeration getEnumeration() throws JMSException
00115 {
00116 if (JoramTracing.dbgClient)
00117 JoramTracing.log(JoramTracing.DEBUG, this
00118 + ": requests an enumeration.");
00119 if (closed)
00120 throw new IllegalStateException("Forbidden call on a closed browser.");
00121
00122
00123 QBrowseRequest browReq = new QBrowseRequest(queue.getName(), selector);
00124
00125 QBrowseReply reply = (QBrowseReply) sess.cnx.syncRequest(browReq);
00126
00127 if (JoramTracing.dbgClient)
00128 JoramTracing.log(JoramTracing.DEBUG, this
00129 + ": received an enumeration.");
00130
00131
00132 Vector momMessages = reply.getMessages();
00133 Vector messages = null;
00134 if (momMessages != null) {
00135 messages = new Vector();
00136 com.scalagent.kjoram.messages.Message momMsg;
00137 for (int i = 0; i < momMessages.size(); i++) {
00138 momMsg = (com.scalagent.kjoram.messages.Message) momMessages.elementAt(i);
00139 messages.addElement(Message.wrapMomMessage(null, momMsg));
00140 }
00141 }
00142
00143
00144 return new QueueEnumeration(messages);
00145 }
00146
00152 public void close() throws JMSException
00153 {
00154
00155 if (closed)
00156 return;
00157
00158 sess.browsers.removeElement(this);
00159 closed = true;
00160
00161 if (JoramTracing.dbgClient)
00162 JoramTracing.log(JoramTracing.DEBUG, this + " closed.");
00163 }
00164
00169 private class QueueEnumeration implements java.util.Enumeration
00170 {
00172 private Vector messages;
00173
00179 private QueueEnumeration(Vector messages)
00180 {
00181 this.messages = messages;
00182 }
00183
00185 public boolean hasMoreElements()
00186 {
00187 if (messages == null)
00188 return false;
00189 return (! messages.isEmpty());
00190 }
00191
00193 public Object nextElement()
00194 {
00195 if (messages == null || messages.isEmpty())
00196 throw new NoSuchElementException();
00197
00198 Object ret = messages.elementAt(0);
00199 messages.removeElementAt(0);
00200 return ret;
00201 }
00202 }
00203 }