1 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 37 private Session sess; 38 39 private Queue queue; 40 41 private String selector; 42 43 private boolean closed = false; 44 45 56 QueueBrowser(Session sess, Queue queue, String 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 74 public String toString() 75 { 76 return "QueueBrowser:" + sess.ident; 77 } 78 79 84 public Queue getQueue() throws JMSException 85 { 86 if (closed) 87 throw new IllegalStateException ("Forbidden call on a closed browser."); 88 89 return queue; 90 } 91 92 97 public String getMessageSelector() throws JMSException 98 { 99 if (closed) 100 throw new IllegalStateException ("Forbidden call on a closed browser."); 101 102 return selector; 103 } 104 105 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 ("Forbidden call on a closed browser."); 121 122 QBrowseRequest browReq = new QBrowseRequest(queue.getName(), selector); 124 QBrowseReply reply = (QBrowseReply) sess.cnx.syncRequest(browReq); 126 127 if (JoramTracing.dbgClient) 128 JoramTracing.log(JoramTracing.DEBUG, this 129 + ": received an enumeration."); 130 131 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 new QueueEnumeration(messages); 145 } 146 147 152 public void close() throws JMSException 153 { 154 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 169 private class QueueEnumeration implements java.util.Enumeration 170 { 171 172 private Vector messages; 173 174 179 private QueueEnumeration(Vector messages) 180 { 181 this.messages = messages; 182 } 183 184 185 public boolean hasMoreElements() 186 { 187 if (messages == null) 188 return false; 189 return (! messages.isEmpty()); 190 } 191 192 193 public Object nextElement() 194 { 195 if (messages == null || messages.isEmpty()) 196 throw new NoSuchElementException(); 197 198 Object ret = messages.elementAt(0); 199 messages.removeElementAt(0); 200 return ret; 201 } 202 } 203 } 204 | Popular Tags |