/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/

package org.codehaus.activemq;

import org.codehaus.activemq.message.ActiveMQDestination;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import java.util.Enumeration;

/**
 * A client uses a <CODE>QueueBrowser</CODE> object to look at messages on a
 * queue without removing them.
 * <p/>
 * <P>
 * The <CODE>getEnumeration</CODE> method returns a <CODE>
 * java.util.Enumeration</CODE> that is used to scan the queue's messages. It
 * may be an enumeration of the entire content of a queue, or it may contain
 * only the messages matching a message selector.
 * <p/>
 * <P>
 * Messages may be arriving and expiring while the scan is done. The JMS API
 * does not require the content of an enumeration to be a static snapshot of
 * queue content. Whether these changes are visible or not depends on the JMS
 * provider.
 * <p/>
 * <P>
 * A <CODE>QueueBrowser</CODE> can be created from either a <CODE>Session
 * </CODE> or a <CODE>QueueSession</CODE>.
 *
 * @see javax.jms.Session#createBrowser
 * @see javax.jms.QueueSession#createBrowser
 * @see javax.jms.QueueBrowser
 * @see javax.jms.QueueReceiver
 */

public class ActiveMQQueueBrowser extends ActiveMQMessageConsumer implements
        QueueBrowser, Enumeration {

    /**
     * Constructor for an ActiveMQQueueBrowser - used internally
     *
     * @param theSession
     * @param dest
     * @param selector
     * @param cnum
     * @throws JMSException
     */
    protected ActiveMQQueueBrowser(ActiveMQSession theSession,
                                   ActiveMQDestination dest, String selector, int cnum)
            throws JMSException {
        super(theSession, dest, "", selector, cnum, theSession.connection.getPrefetchPolicy().getQueueBrowserPrefetch(), false, true);
    }

    /**
     * Gets the queue associated with this queue browser.
     *
     * @return the queue
     * @throws JMSException if the JMS provider fails to get the queue associated
     *                      with this browser due to some internal error.
     */

    public Queue getQueue() throws JMSException {
        return (Queue) getDestination();
    }

    /**
     * Gets an enumeration for browsing the current queue messages in the order
     * they would be received.
     *
     * @return an enumeration for browsing the messages
     * @throws JMSException if the JMS provider fails to get the enumeration for this
     *                      browser due to some internal error.
     */

    public Enumeration getEnumeration() throws JMSException {
        checkClosed();
        //ok - started browsing - wait for inbound messages
        if (super.messageQueue.size() == 0) {
            try {
                Thread.sleep(1000);
            }
            catch (InterruptedException e) {
            }
        }
        return this;
    }

    /**
     * @return true if more messages to process
     */
    public boolean hasMoreElements() {
        // TODO Auto-generated method stub
        return super.messageQueue.size() > 0;
    }

    /**
     * @return the next message
     */
    public Object nextElement() {
        Object answer = null;
        try {
            answer = super.receiveNoWait();
        }
        catch (JMSException e) {
            e.printStackTrace();
        }
        return answer;
    }

}
