1 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 ; 31 import javax.management.ObjectName ; 32 import javax.management.AttributeList ; 33 import javax.management.Attribute ; 34 import javax.management.openmbean.CompositeDataSupport ; 35 import javax.jms.JMSException ; 36 import javax.jms.DeliveryMode ; 37 import java.util.Map ; 38 import java.util.Properties ; 39 import java.util.Iterator ; 40 import java.util.Enumeration ; 41 import java.lang.reflect.Method ; 42 43 public class MapTransformFilter extends ResultTransformFilter { 44 48 public MapTransformFilter(QueryFilter next) { 49 super(next); 50 } 51 52 57 protected Object transformElement(Object object) throws Exception { 58 try { 60 Method method = this.getClass().getDeclaredMethod("transformToMap", new Class [] {object.getClass()}); 61 return (Map )method.invoke(this, new Object [] {object}); 62 } catch (NoSuchMethodException 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 73 protected Map transformToMap(ObjectInstance obj) { 74 return transformToMap(obj.getObjectName()); 75 } 76 77 82 protected Map transformToMap(ObjectName objname) { 83 Properties props = new Properties (); 84 85 Map objProps = objname.getKeyPropertyList(); 87 for (Iterator i=objProps.keySet().iterator(); i.hasNext();) { 88 Object key = i.next(); 89 Object val = objProps.get(key); 90 if (val != null) { 91 props.setProperty(key.toString(), val.toString()); 92 } 93 } 94 95 return props; 96 } 97 98 103 protected Map transformToMap(AttributeList list) { 104 Properties props = new Properties (); 105 for (Iterator i=list.iterator(); i.hasNext();) { 106 Attribute attrib = (Attribute )i.next(); 107 108 if (attrib.getName().equals(MBeansAttributeQueryFilter.KEY_OBJECT_NAME_ATTRIBUTE)) { 110 props.putAll(transformToMap((ObjectName )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 127 protected Map transformToMap(ActiveMQTextMessage msg) throws JMSException { 128 Properties props = new Properties (); 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 144 protected Map transformToMap(ActiveMQBytesMessage msg) throws JMSException { 145 Properties props = new Properties (); 146 147 props.putAll(transformToMap((ActiveMQMessage)msg)); 148 149 long bodyLength = msg.getBodyLength(); 150 byte[] msgBody; 151 int i=0; 152 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 (msgBody)); 156 } 157 msgBody = new byte[(int)(bodyLength % Integer.MAX_VALUE)]; 158 props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSBytes:" + (i+1), new String (msgBody)); 159 160 return props; 161 } 162 163 169 protected Map transformToMap(ActiveMQObjectMessage msg) throws JMSException { 170 Properties props = new Properties (); 171 172 props.putAll(transformToMap((ActiveMQMessage)msg)); 173 if (msg.getObject() != null) { 174 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 187 protected Map transformToMap(ActiveMQMapMessage msg) throws JMSException { 188 Properties props = new Properties (); 189 190 props.putAll(transformToMap((ActiveMQMessage)msg)); 191 192 Enumeration e = msg.getMapNames(); 194 while (e.hasMoreElements()) { 195 String key = (String )e.nextElement(); 196 Object 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 211 protected Map transformToMap(ActiveMQStreamMessage msg) throws JMSException { 212 Properties props = new Properties (); 213 214 props.putAll(transformToMap((ActiveMQMessage)msg)); 215 props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSStreamMessage", msg.toString()); 217 218 return props; 219 } 220 221 227 protected Map transformToMap(ActiveMQMessage msg) throws JMSException { 228 Properties props = new Properties (); 229 230 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 Enumeration e = msg.getPropertyNames(); 252 while (e.hasMoreElements()) { 253 String name = (String )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 267 protected Map transformToMap(CompositeDataSupport data) { 268 Properties props = new Properties (); 269 270 String typeName = data.getCompositeType().getTypeName(); 271 272 if (typeName.equals(ActiveMQTextMessage.class.getName())) { 274 props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "Text", data.get("Text").toString()); 275 276 } 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 ((byte[])data.get("BodyPreview"))); 280 281 } else if (typeName.equals(ActiveMQMapMessage.class.getName())) { 283 Map contentMap = (Map )data.get("ContentMap"); 284 for (Iterator i=contentMap.keySet().iterator(); i.hasNext();) { 285 String key = (String )i.next(); 286 props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + key, contentMap.get(key).toString()); 287 } 288 289 } else if (typeName.equals(ActiveMQObjectMessage.class.getName()) || 291 typeName.equals(ActiveMQStreamMessage.class.getName()) || 292 typeName.equals(ActiveMQMessage.class.getName())) { 293 294 } else { 296 throw new IllegalArgumentException ("Unrecognized composite data to transform. composite type: " + typeName); 297 } 298 299 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 props.setProperty(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + "Properties", "" + data.get("Properties")); 313 314 return props; 315 } 316 } 317 | Popular Tags |