001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq;
023:
024: import java.util.Enumeration;
025: import java.util.NoSuchElementException;
026:
027: import javax.jms.InvalidSelectorException;
028: import javax.jms.JMSException;
029: import javax.jms.Queue;
030: import org.jboss.mq.selectors.Selector;
031:
032: import javax.jms.QueueBrowser;
033:
034: /**
035: * This class implements javax.jms.QueueBrowser
036: *
037: * @author Norbert Lataille (Norbert.Lataille@m4x.org)
038: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
039: * @version $Revision: 57198 $
040: */
041: public class SpyQueueBrowser implements QueueBrowser {
042: // Constants -----------------------------------------------------
043:
044: // Attributes ----------------------------------------------------
045:
046: /** Whether we are closed */
047: boolean closed;
048: /** The destination this browser will browse messages from */
049: Queue destination;
050: /** String Selector */
051: String messageSelector;
052: /** The Session this was created with */
053: SpySession session;
054:
055: // Static --------------------------------------------------------
056:
057: // Constructors --------------------------------------------------
058:
059: /**
060: * Create a new SpyQueueBrowser
061: *
062: * @param session the session
063: * @param destination the destination
064: * @param messageSelector the message selector
065: * @throws InvalidSelectorException for an invalid selector
066: */
067: SpyQueueBrowser(SpySession session, Queue destination,
068: String messageSelector) throws InvalidSelectorException {
069: this .destination = destination;
070: this .session = session;
071: this .messageSelector = messageSelector;
072:
073: // If the selector is set, try to build it, throws an
074: // InvalidSelectorException
075: // if it is not valid.
076: if (messageSelector != null)
077: new Selector(messageSelector);
078: }
079:
080: // Public --------------------------------------------------------
081:
082: // QueueBrowser implementation -----------------------------------
083:
084: public Queue getQueue() throws JMSException {
085: return destination;
086: }
087:
088: public String getMessageSelector() throws JMSException {
089: return messageSelector;
090: }
091:
092: public Enumeration getEnumeration() throws JMSException {
093: if (closed)
094: throw new JMSException("The QueueBrowser was closed");
095:
096: final SpyMessage data[] = session.connection.browse(
097: destination, messageSelector);
098: return new Enumeration() {
099: int i = 0;
100:
101: public boolean hasMoreElements() {
102: return i < data.length;
103: }
104:
105: public Object nextElement() {
106: if (!hasMoreElements())
107: throw new NoSuchElementException();
108: return data[i++];
109: }
110: };
111: }
112:
113: public void close() throws JMSException {
114: closed = true;
115: return;
116: }
117:
118: // Package protected ---------------------------------------------
119:
120: // Protected -----------------------------------------------------
121:
122: // Private -------------------------------------------------------
123:
124: // Inner classes -------------------------------------------------
125:
126: }
|