001    package com.mockrunner.mock.jms;
002    
003    import java.io.Serializable;
004    import java.util.ArrayList;
005    import java.util.Enumeration;
006    import java.util.List;
007    import java.util.Vector;
008    
009    import javax.jms.InvalidSelectorException;
010    import javax.jms.JMSException;
011    import javax.jms.Message;
012    import javax.jms.Queue;
013    import javax.jms.QueueBrowser;
014    
015    import org.activemq.filter.mockrunner.Filter;
016    import org.activemq.selector.mockrunner.SelectorParser;
017    
018    /**
019     * Mock implementation of JMS <code>QueueBrowser</code>.
020     */
021    public class MockQueueBrowser implements QueueBrowser, Serializable
022    {
023        private MockConnection connection;
024        private MockQueue queue;
025        private boolean closed;
026        private String messageSelector;
027        private Filter messageSelectorFilter;
028        
029        public MockQueueBrowser(MockConnection connection, MockQueue queue)
030        {
031            this(connection, queue, null);
032        }
033    
034        public MockQueueBrowser(MockConnection connection, MockQueue queue, String messageSelector)
035        {
036            this.connection = connection;
037            this.queue = queue;
038            closed = false;
039            this.messageSelector = messageSelector;
040            parseMessageSelector();
041        }
042        
043        private void parseMessageSelector()
044        {
045            if(null == messageSelector || messageSelector.length() == 0)
046            {
047                this.messageSelectorFilter = null;
048            }
049            else
050            {
051                try
052                {
053                    this.messageSelectorFilter = new SelectorParser().parse(messageSelector);
054                }
055                catch(InvalidSelectorException exc)
056                {
057                    throw new RuntimeException("Error parsing message selector: " + exc.getMessage());
058                }
059            }
060        }
061        
062        /**
063         * Returns if this browser was closed.
064         * @return <code>true</code> if this browser is closed
065         */
066        public boolean isClosed()
067        {
068            return closed;
069        }
070    
071        public Queue getQueue() throws JMSException
072        {
073            connection.throwJMSException();
074            return queue;
075        }
076    
077        public String getMessageSelector() throws JMSException
078        {
079            connection.throwJMSException();
080            return messageSelector;
081        }
082    
083        public Enumeration getEnumeration() throws JMSException
084        {
085            connection.throwJMSException();
086            if(isClosed())
087            {
088                throw new JMSException("Browser is closed");
089            } 
090            return new Vector(getFilteredMessageList()).elements();
091        }
092    
093        public void close() throws JMSException
094        {
095            connection.throwJMSException();
096            closed = true;
097        }
098        
099        private List getFilteredMessageList()
100        {
101            List messages = queue.getCurrentMessageList();
102            if(null == messageSelectorFilter) return messages;
103            if(!connection.getConfigurationManager().getUseMessageSelectors()) return messages;
104            List filteredMessages = new ArrayList();
105            for(int ii = 0; ii < messages.size(); ii++)
106            {
107                Message nextMessage = (Message)messages.get(ii);
108                try
109                {
110                    if(messageSelectorFilter.matches(nextMessage))
111                    {
112                        filteredMessages.add(nextMessage);
113                    }
114                } 
115                catch(JMSException exc)
116                {
117                    throw new RuntimeException(exc.getMessage());
118                }
119            }
120            return filteredMessages;
121        }
122    }