KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > SpyQueueBrowser


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq;
23
24 import java.util.Enumeration JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26
27 import javax.jms.InvalidSelectorException JavaDoc;
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Queue JavaDoc;
30 import org.jboss.mq.selectors.Selector;
31
32 import javax.jms.QueueBrowser JavaDoc;
33
34 /**
35  * This class implements javax.jms.QueueBrowser
36  *
37  * @author Norbert Lataille (Norbert.Lataille@m4x.org)
38  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
39  * @version $Revision: 37459 $
40  */

41 public class SpyQueueBrowser implements QueueBrowser JavaDoc
42 {
43    // Constants -----------------------------------------------------
44

45    // Attributes ----------------------------------------------------
46

47    /** Whether we are closed */
48    boolean closed;
49    /** The destination this browser will browse messages from */
50    Queue JavaDoc destination;
51    /** String Selector */
52    String JavaDoc messageSelector;
53    /** The Session this was created with */
54    SpySession session;
55    // Static --------------------------------------------------------
56

57    // Constructors --------------------------------------------------
58

59    /**
60     * Create a new SpyQueueBrowser
61     *
62     * @param session the session
63     * @param destination the destination
64     * @param messageSelector the message selector
65     * @throws InvalidSelectorException for an invalid selector
66     */

67    SpyQueueBrowser(SpySession session, Queue JavaDoc destination, String JavaDoc messageSelector) throws InvalidSelectorException JavaDoc
68    {
69       this.destination = destination;
70       this.session = session;
71       this.messageSelector = messageSelector;
72
73       // If the selector is set, try to build it, throws an
74
// InvalidSelectorException
75
// if it is not valid.
76
if (messageSelector != null)
77          new Selector(messageSelector);
78    }
79    
80    // Public --------------------------------------------------------
81

82    // QueueBrowser implementation -----------------------------------
83

84    public Queue JavaDoc getQueue() throws JMSException JavaDoc
85    {
86       return destination;
87    }
88
89    public String JavaDoc getMessageSelector() throws JMSException JavaDoc
90    {
91       return messageSelector;
92    }
93
94    public Enumeration JavaDoc getEnumeration() throws JMSException JavaDoc
95    {
96       if (closed)
97          throw new JMSException JavaDoc("The QueueBrowser was closed");
98
99       final SpyMessage data[] = session.connection.browse(destination, messageSelector);
100       return new Enumeration JavaDoc()
101       {
102          int i = 0;
103          public boolean hasMoreElements()
104          {
105             return i < data.length;
106          }
107          public Object JavaDoc nextElement()
108          {
109             if (!hasMoreElements())
110                throw new NoSuchElementException JavaDoc();
111             return data[i++];
112          }
113       };
114    }
115
116    public void close() throws JMSException JavaDoc
117    {
118       closed = true;
119       return;
120    }
121    
122    // Package protected ---------------------------------------------
123

124    // Protected -----------------------------------------------------
125

126    // Private -------------------------------------------------------
127

128    // Inner classes -------------------------------------------------
129

130 }
Popular Tags