1 18 package org.apache.activemq.web; 19 20 import javax.jms.JMSException ; 21 import javax.jms.MapMessage ; 22 import javax.jms.Message ; 23 import javax.jms.ObjectMessage ; 24 import javax.jms.TextMessage ; 25 import java.util.Enumeration ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 34 public class MessageQuery extends QueueBrowseQuery { 35 36 private String id; 37 private Message message; 38 39 public MessageQuery(BrokerFacade brokerFacade, SessionPool sessionPool) throws JMSException { 40 super(brokerFacade, sessionPool); 41 } 42 43 public String getId() { 44 return id; 45 } 46 47 public void setId(String id) { 48 this.id = id; 49 } 50 51 public void setMessage(Message message) { 52 this.message = message; 53 } 54 55 public Message getMessage() throws JMSException { 56 if (message == null) { 57 if (id != null) { 58 Enumeration iter = getBrowser().getEnumeration(); 59 while (iter.hasMoreElements()) { 60 Message item = (Message ) iter.nextElement(); 61 if (id.equals(item.getJMSMessageID())) { 62 message = item; 63 break; 64 } 65 } 66 } 67 68 } 69 return message; 70 } 71 72 public Object getBody() throws JMSException { 73 Message message = getMessage(); 74 if (message instanceof TextMessage ) { 75 return ((TextMessage ) message).getText(); 76 } 77 if (message instanceof ObjectMessage ) { 78 return ((ObjectMessage ) message).getObject(); 79 } 80 if (message instanceof MapMessage ) { 81 return createMapBody((MapMessage ) message); 82 } 83 return null; 84 } 85 86 public Map getPropertiesMap() throws JMSException { 87 Map answer = new HashMap (); 88 Message aMessage = getMessage(); 89 Enumeration iter = aMessage.getPropertyNames(); 90 while (iter.hasMoreElements()) { 91 String name = (String ) iter.nextElement(); 92 Object value = aMessage.getObjectProperty(name); 93 if (value != null) { 94 answer.put(name, value); 95 } 96 } 97 return answer; 98 } 99 100 protected Map createMapBody(MapMessage mapMessage) throws JMSException { 101 Map answer = new HashMap (); 102 Enumeration iter = mapMessage.getMapNames(); 103 while (iter.hasMoreElements()) { 104 String name = (String ) iter.nextElement(); 105 Object value = mapMessage.getObject(name); 106 if (value != null) { 107 answer.put(name, value); 108 } 109 } 110 return answer; 111 } 112 } 113 | Popular Tags |