KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > filter > MapTransformFilter


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.console.filter;
19
20 import org.apache.activemq.console.formatter.GlobalWriter;
21 import org.apache.activemq.console.util.AmqMessagesUtil;
22 import org.apache.activemq.command.ActiveMQDestination;
23 import org.apache.activemq.command.ActiveMQObjectMessage;
24 import org.apache.activemq.command.ActiveMQBytesMessage;
25 import org.apache.activemq.command.ActiveMQTextMessage;
26 import org.apache.activemq.command.ActiveMQMapMessage;
27 import org.apache.activemq.command.ActiveMQStreamMessage;
28 import org.apache.activemq.command.ActiveMQMessage;
29
30 import javax.management.ObjectInstance JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.AttributeList JavaDoc;
33 import javax.management.Attribute JavaDoc;
34 import javax.management.openmbean.CompositeDataSupport JavaDoc;
35 import javax.jms.JMSException JavaDoc;
36 import javax.jms.DeliveryMode JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Properties JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.lang.reflect.Method JavaDoc;
42
43 public class MapTransformFilter extends ResultTransformFilter {
44     /**
45      * Creates a Map transform filter that is able to transform a variety of objects to a properties map object
46      * @param next - the next query filter
47      */

48     public MapTransformFilter(QueryFilter next) {
49         super(next);
50     }
51
52     /**
53      * Transform the given object to a Map object
54      * @param object - object to transform
55      * @return map object
56      */

57     protected Object JavaDoc transformElement(Object JavaDoc object) throws Exception JavaDoc {
58         // Use reflection to determine how the object should be transformed
59
try {
60             Method JavaDoc method = this.getClass().getDeclaredMethod("transformToMap", new Class JavaDoc[] {object.getClass()});
61             return (Map JavaDoc)method.invoke(this, new Object JavaDoc[] {object});
62         } catch (NoSuchMethodException JavaDoc e) {
63             GlobalWriter.print("Unable to transform mbean of type: " + object.getClass().getName() + ". No corresponding transformToMap method found.");
64             return null;
65         }
66     }
67
68     /**
69      * Transform an ObjectInstance mbean to a Map
70      * @param obj - ObjectInstance format of an mbean
71      * @return map object
72      */

73     protected Map JavaDoc transformToMap(ObjectInstance JavaDoc obj) {
74         return transformToMap(obj.getObjectName());
75     }
76
77     /**
78      * Transform an ObjectName mbean to a Map
79      * @param objname - ObjectName format of an mbean
80      * @return map object
81      */

82     protected Map JavaDoc transformToMap(ObjectName JavaDoc objname) {
83         Properties JavaDoc props = new Properties JavaDoc();
84
85         // Parse object properties
86
Map JavaDoc objProps = objname.getKeyPropertyList();
87         for (Iterator JavaDoc i=objProps.keySet().iterator(); i.hasNext();) {
88             Object JavaDoc key = i.next();
89             Object JavaDoc val = objProps.get(key);
90             if (val != null) {
91                 props.setProperty(key.toString(), val.toString());
92             }
93         }
94
95         return props;
96     }
97
98     /**
99      * Transform an Attribute List format of an mbean to a Map
100      * @param list - AttributeList format of an mbean
101      * @return map object
102      */

103     protected Map JavaDoc transformToMap(AttributeList JavaDoc list) {
104         Properties JavaDoc props = new Properties JavaDoc();
105         for (Iterator JavaDoc i=list.iterator(); i.hasNext();) {
106             Attribute JavaDoc attrib = (Attribute JavaDoc)i.next();
107
108             // If attribute is an ObjectName
109
if (attrib.getName().equals(MBeansAttributeQueryFilter.KEY_OBJECT_NAME_ATTRIBUTE)) {
110                 props.putAll(transformToMap((ObjectName JavaDoc)attrib.getValue()));
111             } else {
112                 if (attrib.getValue() != null) {
113                     props.setProperty(attrib.getName(), attrib.getValue().toString());
114                 }
115             }
116         }
117
118         return props;
119     }
120
121     /**
122      * Transform an ActiveMQTextMessage to a Map
123      * @param msg - text message to trasnform
124      * @return map object
125      * @throws JMSException
126      */

127     protected Map JavaDoc transformToMap(ActiveMQTextMessage msg) throws JMSException JavaDoc {
128         Properties JavaDoc props = new Properties JavaDoc();
129
130         props.putAll(transformToMap((ActiveMQMessage)msg));
131         if (msg.getText() != null) {
132             props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSText", msg.getText());
133         }
134
135         return props;
136     }
137
138     /**
139      * Transform an ActiveMQBytesMessage to a Map
140      * @param msg - bytes message to transform
141      * @return map object
142      * @throws JMSException
143      */

144     protected Map JavaDoc transformToMap(ActiveMQBytesMessage msg) throws JMSException JavaDoc {
145         Properties JavaDoc props = new Properties JavaDoc();
146
147         props.putAll(transformToMap((ActiveMQMessage)msg));
148
149         long bodyLength = msg.getBodyLength();
150         byte[] msgBody;
151         int i=0;
152         // Create separate bytes messages
153
for (i=0; i<(bodyLength/Integer.MAX_VALUE); i++) {
154             msgBody = new byte[Integer.MAX_VALUE];
155             props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSBytes:" + (i+1), new String JavaDoc(msgBody));
156         }
157         msgBody = new byte[(int)(bodyLength % Integer.MAX_VALUE)];
158         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSBytes:" + (i+1), new String JavaDoc(msgBody));
159
160         return props;
161     }
162
163     /**
164      * Transform an ActiveMQMessage to a Map
165      * @param msg - object message to transform
166      * @return map object
167      * @throws JMSException
168      */

169     protected Map JavaDoc transformToMap(ActiveMQObjectMessage msg) throws JMSException JavaDoc {
170         Properties JavaDoc props = new Properties JavaDoc();
171
172         props.putAll(transformToMap((ActiveMQMessage)msg));
173         if (msg.getObject() != null) {
174             // Just add the class name and toString value of the object
175
props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSObjectClass", msg.getObject().getClass().getName());
176             props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSObjectString", msg.getObject().toString());
177         }
178         return props;
179     }
180
181     /**
182      * Transform an ActiveMQMapMessage to a Map
183      * @param msg - map message to transform
184      * @return map object
185      * @throws JMSException
186      */

187     protected Map JavaDoc transformToMap(ActiveMQMapMessage msg) throws JMSException JavaDoc {
188         Properties JavaDoc props = new Properties JavaDoc();
189
190         props.putAll(transformToMap((ActiveMQMessage)msg));
191
192         // Get map properties
193
Enumeration JavaDoc e = msg.getMapNames();
194         while (e.hasMoreElements()) {
195             String JavaDoc key = (String JavaDoc)e.nextElement();
196             Object JavaDoc val = msg.getObject(key);
197             if (val != null) {
198                 props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + key, val.toString());
199             }
200         }
201
202         return props;
203     }
204
205     /**
206      * Transform an ActiveMQStreamMessage to a Map
207      * @param msg - stream message to transform
208      * @return map object
209      * @throws JMSException
210      */

211     protected Map JavaDoc transformToMap(ActiveMQStreamMessage msg) throws JMSException JavaDoc {
212         Properties JavaDoc props = new Properties JavaDoc();
213
214         props.putAll(transformToMap((ActiveMQMessage)msg));
215         // Just set the toString of the message as the body of the stream message
216
props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSStreamMessage", msg.toString());
217
218         return props;
219     }
220
221     /**
222      * Transform an ActiveMQMessage to a Map
223      * @param msg - message to transform
224      * @return map object
225      * @throws JMSException
226      */

227     protected Map JavaDoc transformToMap(ActiveMQMessage msg) throws JMSException JavaDoc {
228         Properties JavaDoc props = new Properties JavaDoc();
229
230         // Get JMS properties
231
if (msg.getJMSCorrelationID() != null) {
232             props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSCorrelationID", msg.getJMSCorrelationID());
233         }
234         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSDeliveryMode", (msg.getJMSDeliveryMode()==DeliveryMode.PERSISTENT) ? "persistent" : "non-persistent");
235         if (msg.getJMSDestination() != null) {
236             props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSDestination", ((ActiveMQDestination)msg.getJMSDestination()).getPhysicalName());
237         }
238         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSExpiration", Long.toString(msg.getJMSExpiration()));
239         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSMessageID", msg.getJMSMessageID());
240         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSPriority", Integer.toString(msg.getJMSPriority()));
241         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSRedelivered", Boolean.toString(msg.getJMSRedelivered()));
242         if (msg.getJMSReplyTo() != null) {
243             props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSReplyTo", ((ActiveMQDestination)msg.getJMSReplyTo()).getPhysicalName());
244         }
245         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSTimestamp", Long.toString(msg.getJMSTimestamp()));
246         if (msg.getJMSType() != null) {
247             props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSType", msg.getJMSType());
248         }
249
250         // Get custom properties
251
Enumeration JavaDoc e = msg.getPropertyNames();
252         while (e.hasMoreElements()) {
253             String JavaDoc name = (String JavaDoc)e.nextElement();
254             if (msg.getObjectProperty(name) != null) {
255                 props.setProperty(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + name, msg.getObjectProperty(name).toString());
256             }
257         }
258
259         return props;
260     }
261
262     /**
263      * Transform an openMBean composite data to a Map
264      * @param data - composite data to transform
265      * @return map object
266      */

267     protected Map JavaDoc transformToMap(CompositeDataSupport JavaDoc data) {
268         Properties JavaDoc props = new Properties JavaDoc();
269
270         String JavaDoc typeName = data.getCompositeType().getTypeName();
271
272         // Retrieve text message
273
if (typeName.equals(ActiveMQTextMessage.class.getName())) {
274             props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "Text", data.get("Text").toString());
275
276         // Retrieve byte preview
277
} else if (typeName.equals(ActiveMQBytesMessage.class.getName())) {
278             props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "BodyLength", data.get("BodyLength").toString());
279             props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "BodyPreview", new String JavaDoc((byte[])data.get("BodyPreview")));
280
281         // Expand content map
282
} else if (typeName.equals(ActiveMQMapMessage.class.getName())) {
283             Map JavaDoc contentMap = (Map JavaDoc)data.get("ContentMap");
284             for (Iterator JavaDoc i=contentMap.keySet().iterator(); i.hasNext();) {
285                 String JavaDoc key = (String JavaDoc)i.next();
286                 props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + key, contentMap.get(key).toString());
287             }
288
289         // Do nothing
290
} else if (typeName.equals(ActiveMQObjectMessage.class.getName()) ||
291                    typeName.equals(ActiveMQStreamMessage.class.getName()) ||
292                    typeName.equals(ActiveMQMessage.class.getName())) {
293
294         // Unrecognized composite data. Throw exception.
295
} else {
296             throw new IllegalArgumentException JavaDoc("Unrecognized composite data to transform. composite type: " + typeName);
297         }
298
299         // Process the JMS message header values
300
props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSCorrelationID", "" + data.get("JMSCorrelationID"));
301         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSDestination", "" + data.get("JMSDestination"));
302         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSMessageID", "" + data.get("JMSMessageID"));
303         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSReplyTo", "" + data.get("JMSReplyTo"));
304         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSType", "" + data.get("JMSType"));
305         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSDeliveryMode", "" + data.get("JMSDeliveryMode"));
306         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSExpiration", "" + data.get("JMSExpiration"));
307         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSPriority", "" + data.get("JMSPriority"));
308         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSRedelivered", "" + data.get("JMSRedelivered"));
309         props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSTimestamp", "" + data.get("JMSTimestamp"));
310
311         // Process the JMS custom message properties
312
props.setProperty(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + "Properties", "" + data.get("Properties"));
313
314         return props;
315     }
316 }
317
Popular Tags