KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > web > MessageQuery


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.web;
19
20 import javax.jms.JMSException JavaDoc;
21 import javax.jms.MapMessage JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.ObjectMessage JavaDoc;
24 import javax.jms.TextMessage JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * Allow the user to browse a message on a queue by its ID
31  *
32  * @version $Revision: 504235 $
33  */

34 public class MessageQuery extends QueueBrowseQuery {
35
36     private String JavaDoc id;
37     private Message JavaDoc message;
38
39     public MessageQuery(BrokerFacade brokerFacade, SessionPool sessionPool) throws JMSException JavaDoc {
40         super(brokerFacade, sessionPool);
41     }
42
43     public String JavaDoc getId() {
44         return id;
45     }
46
47     public void setId(String JavaDoc id) {
48         this.id = id;
49     }
50
51     public void setMessage(Message JavaDoc message) {
52         this.message = message;
53     }
54
55     public Message JavaDoc getMessage() throws JMSException JavaDoc {
56         if (message == null) {
57             if (id != null) {
58                 Enumeration JavaDoc iter = getBrowser().getEnumeration();
59                 while (iter.hasMoreElements()) {
60                     Message JavaDoc item = (Message JavaDoc) 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 JavaDoc getBody() throws JMSException JavaDoc {
73         Message JavaDoc message = getMessage();
74         if (message instanceof TextMessage JavaDoc) {
75             return ((TextMessage JavaDoc) message).getText();
76         }
77         if (message instanceof ObjectMessage JavaDoc) {
78             return ((ObjectMessage JavaDoc) message).getObject();
79         }
80         if (message instanceof MapMessage JavaDoc) {
81             return createMapBody((MapMessage JavaDoc) message);
82         }
83         return null;
84     }
85
86     public Map JavaDoc getPropertiesMap() throws JMSException JavaDoc {
87         Map JavaDoc answer = new HashMap JavaDoc();
88         Message JavaDoc aMessage = getMessage();
89         Enumeration JavaDoc iter = aMessage.getPropertyNames();
90         while (iter.hasMoreElements()) {
91             String JavaDoc name = (String JavaDoc) iter.nextElement();
92             Object JavaDoc value = aMessage.getObjectProperty(name);
93             if (value != null) {
94                 answer.put(name, value);
95             }
96         }
97         return answer;
98     }
99
100     protected Map JavaDoc createMapBody(MapMessage JavaDoc mapMessage) throws JMSException JavaDoc {
101         Map JavaDoc answer = new HashMap JavaDoc();
102         Enumeration JavaDoc iter = mapMessage.getMapNames();
103         while (iter.hasMoreElements()) {
104             String JavaDoc name = (String JavaDoc) iter.nextElement();
105             Object JavaDoc value = mapMessage.getObject(name);
106             if (value != null) {
107                 answer.put(name, value);
108             }
109         }
110         return answer;
111     }
112 }
113
Popular Tags