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.activemq.web;
018    
019    import java.util.Enumeration;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import javax.jms.JMSException;
024    import javax.jms.MapMessage;
025    import javax.jms.Message;
026    import javax.jms.ObjectMessage;
027    import javax.jms.QueueBrowser;
028    import javax.jms.TextMessage;
029    
030    /**
031     * Allow the user to browse a message on a queue by its ID
032     * 
033     * @version $Revision: 692172 $
034     */
035    public class MessageQuery extends QueueBrowseQuery {
036    
037        private String id;
038        private Message message;
039    
040        public MessageQuery(BrokerFacade brokerFacade, SessionPool sessionPool) throws JMSException {
041            super(brokerFacade, sessionPool);
042        }
043    
044        public String getId() {
045            return id;
046        }
047    
048        public void setId(String id) {
049            this.id = id;
050        }
051    
052        public void setMessage(Message message) {
053            this.message = message;
054        }
055    
056        public Message getMessage() throws JMSException {
057            if (message == null) {
058                if (id != null) {
059                    QueueBrowser tempBrowser=getBrowser();
060                    Enumeration iter = tempBrowser.getEnumeration();
061                    while (iter.hasMoreElements()) {
062                        Message item = (Message) iter.nextElement();
063                        if (id.equals(item.getJMSMessageID())) {
064                            message = item;
065                            break;
066                        }
067                    }
068                    tempBrowser.close();
069                }
070    
071            }
072            return message;
073        }
074    
075        public Object getBody() throws JMSException {
076            Message message = getMessage();
077            if (message instanceof TextMessage) {
078                return ((TextMessage) message).getText();
079            }
080            if (message instanceof ObjectMessage) {
081                return ((ObjectMessage) message).getObject();
082            }
083            if (message instanceof MapMessage) {
084                return createMapBody((MapMessage) message);
085            }
086            return null;
087        }
088    
089        public Map<String, Object> getPropertiesMap() throws JMSException {
090            Map<String, Object> answer = new HashMap<String, Object>();
091            Message aMessage = getMessage();
092            Enumeration iter = aMessage.getPropertyNames();
093            while (iter.hasMoreElements()) {
094                String name = (String) iter.nextElement();
095                Object value = aMessage.getObjectProperty(name);
096                if (value != null) {
097                    answer.put(name, value);
098                }
099            }
100            return answer;
101        }
102    
103        protected Map<String, Object> createMapBody(MapMessage mapMessage) throws JMSException {
104            Map<String, Object> answer = new HashMap<String, Object>();
105            Enumeration iter = mapMessage.getMapNames();
106            while (iter.hasMoreElements()) {
107                String name = (String) iter.nextElement();
108                Object value = mapMessage.getObject(name);
109                if (value != null) {
110                    answer.put(name, value);
111                }
112            }
113            return answer;
114        }
115    }