001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jms.connection;
031:
032: import com.caucho.jms.JmsRuntimeException;
033: import com.caucho.jms.message.*;
034: import com.caucho.jms.queue.*;
035: import com.caucho.jms.selector.*;
036: import com.caucho.log.Log;
037: import com.caucho.util.*;
038:
039: import javax.jms.*;
040: import java.util.ArrayList;
041: import java.util.Enumeration;
042: import java.util.logging.Logger;
043: import java.util.logging.Level;
044:
045: /**
046: * A basic message consumer.
047: */
048: public class MessageBrowserImpl implements QueueBrowser {
049: static final Logger log = Logger.getLogger(MessageBrowserImpl.class
050: .getName());
051: static final L10N L = new L10N(MessageBrowserImpl.class);
052:
053: private JmsSession _session;
054: private AbstractQueue _queue;
055: private String _messageSelector;
056: private Selector _selector;
057:
058: public MessageBrowserImpl(JmsSession session, AbstractQueue queue,
059: String messageSelector) throws JMSException {
060: _session = session;
061: _queue = queue;
062: _messageSelector = messageSelector;
063:
064: if (messageSelector != null)
065: _selector = new SelectorParser().parse(messageSelector);
066: }
067:
068: public Queue getQueue() throws JMSException {
069: return _queue;
070: }
071:
072: public String getMessageSelector() throws JMSException {
073: return _messageSelector;
074: }
075:
076: public Enumeration getEnumeration() throws JMSException {
077: ArrayList<MessageImpl> list;
078:
079: if (_session.isActive())
080: list = _queue.getBrowserList();
081: else
082: list = new ArrayList<MessageImpl>(0);
083:
084: return new BrowserEnumeration(list, _selector);
085: }
086:
087: public void close() throws JMSException {
088: }
089:
090: public String toString() {
091: return "MessageBrowserImpl[" + _queue + "]";
092: }
093:
094: public static class BrowserEnumeration implements Enumeration {
095: private ArrayList<MessageImpl> _messages;
096: private Selector _selector;
097:
098: private int _index;
099:
100: BrowserEnumeration(ArrayList<MessageImpl> messages,
101: Selector selector) {
102: _messages = messages;
103: _selector = selector;
104:
105: nextValidMessage();
106: }
107:
108: public boolean hasMoreElements() {
109: return _index < _messages.size();
110: }
111:
112: public Object nextElement() {
113: if (_index < _messages.size()) {
114: MessageImpl msg = _messages.get(_index);
115:
116: _index++;
117: nextValidMessage();
118:
119: return msg;
120: } else
121: return null;
122: }
123:
124: private void nextValidMessage() {
125: try {
126: for (; _index < _messages.size(); _index++) {
127: MessageImpl msg = _messages.get(_index);
128:
129: if (_selector == null || _selector.isMatch(msg))
130: return;
131: }
132: } catch (RuntimeException e) {
133: throw e;
134: } catch (Exception e) {
135: throw new JmsRuntimeException(e);
136: }
137: }
138: }
139: }
|