001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.jms;
018    
019    import java.util.ArrayList;
020    import java.util.Enumeration;
021    import java.util.List;
022    
023    import javax.jms.JMSException;
024    import javax.jms.Message;
025    import javax.jms.QueueBrowser;
026    import javax.jms.Session;
027    
028    import org.apache.camel.Exchange;
029    import org.springframework.jms.core.BrowserCallback;
030    import org.springframework.jms.core.JmsOperations;
031    
032    /**
033     * A default implementation of queue browsing using the Spring 2.5.x {@link BrowserCallback}
034     * @version $Revision: 13749 $
035     */
036    public class DefaultQueueBrowseStrategy implements QueueBrowseStrategy {
037    
038        public List<Exchange> browse(JmsOperations template, String queue, final JmsQueueEndpoint endpoint) {
039            return  (List<Exchange>) template.browse(queue, new BrowserCallback() {
040    
041                public Object doInJms(Session session, QueueBrowser browser) throws JMSException {
042                    // TODO not the best implementation in the world as we have to browse
043                    // the entire queue, which could be massive
044    
045                    List<Exchange> answer = new ArrayList<Exchange>();
046                    Enumeration iter = browser.getEnumeration();
047                    while (iter.hasMoreElements()) {
048                        Message message = (Message) iter.nextElement();
049                        Exchange exchange = endpoint.createExchange(message);
050                        answer.add(exchange);
051                    }
052                    return answer;
053                }
054            });
055        }
056    }