1 24 package org.objectweb.joram.client.jms; 25 26 import java.util.*; 27 28 import javax.jms.InvalidSelectorException ; 29 import javax.jms.InvalidDestinationException ; 30 import javax.jms.IllegalStateException ; 31 import javax.jms.JMSException ; 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 42 public class QueueBrowser implements javax.jms.QueueBrowser { 43 44 private Session sess; 45 46 47 private Queue queue; 48 49 50 private String selector; 51 52 53 private boolean closed = false; 54 55 66 QueueBrowser(Session sess, Queue queue, String selector) throws JMSException { 67 if (queue == null) 68 throw new InvalidDestinationException ("Invalid queue: " + queue); 69 70 try { 71 ClientSelector.checks(selector); 72 } catch (org.objectweb.joram.shared.excepts.SelectorException sE) { 73 throw new InvalidSelectorException ("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 85 public String toString() { 86 return "QueueBrowser:" + sess.getId(); 87 } 88 89 94 public synchronized javax.jms.Queue getQueue() throws JMSException { 95 if (closed) 96 throw new IllegalStateException ("Forbidden call on a closed browser."); 97 98 return queue; 99 } 100 101 106 public synchronized String getMessageSelector() throws JMSException { 107 if (closed) 108 throw new IllegalStateException ("Forbidden call on a closed browser."); 109 110 return selector; 111 } 112 113 122 public synchronized Enumeration getEnumeration() throws JMSException { 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 ("Forbidden call on a closed browser."); 129 130 QBrowseRequest browReq = new QBrowseRequest(queue.getName(), selector); 132 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 Vector messages = reply.getMessages(); 141 return new QueueEnumeration(reply.getMessages()); 143 } 144 145 150 public synchronized void close() throws JMSException { 151 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 166 private class QueueEnumeration implements java.util.Enumeration { 167 168 private Vector messages; 169 170 175 private QueueEnumeration(Vector messages) { 176 this.messages = messages; 177 } 178 179 180 public boolean hasMoreElements() { 181 if (messages == null) 182 return false; 183 return (! messages.isEmpty()); 184 } 185 186 187 public Object 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 exc) { 197 JoramTracing.dbgClient.log(BasicLevel.ERROR, 198 this + ", bad message: " + msg, exc); 199 } 200 return jmsMsg; 201 } 202 } 203 } 204 | Popular Tags |