1 18 package org.apache.activemq.console.command; 19 20 import org.apache.activemq.console.util.AmqMessagesUtil; 21 import org.apache.activemq.console.formatter.GlobalWriter; 22 import org.apache.activemq.command.ActiveMQQueue; 23 import org.apache.activemq.command.ActiveMQTopic; 24 25 import javax.jms.Destination ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.Set ; 29 import java.util.HashSet ; 30 import java.util.StringTokenizer ; 31 import java.util.Iterator ; 32 33 public class AmqBrowseCommand extends AbstractAmqCommand { 34 public static final String QUEUE_PREFIX = "queue:"; 35 public static final String TOPIC_PREFIX = "topic:"; 36 37 public static final String VIEW_GROUP_HEADER = "header:"; 38 public static final String VIEW_GROUP_CUSTOM = "custom:"; 39 public static final String VIEW_GROUP_BODY = "body:"; 40 41 private final List queryAddObjects = new ArrayList (10); 42 private final List querySubObjects = new ArrayList (10); 43 private final Set groupViews = new HashSet (10); 44 private final Set queryViews = new HashSet (10); 45 46 51 protected void runTask(List tokens) throws Exception { 52 try { 53 if (tokens.isEmpty()) { 55 GlobalWriter.printException(new IllegalArgumentException ("No JMS destination specified.")); 56 return; 57 } 58 59 if (getBrokerUrl() == null) { 61 GlobalWriter.printException(new IllegalStateException ("No broker url specified. Use the --amqurl option to specify a broker url.")); 62 return; 63 } 64 65 for (Iterator i=tokens.iterator(); i.hasNext();) { 67 String destName = (String )i.next(); 68 Destination dest; 69 70 if (destName.startsWith(QUEUE_PREFIX)) { 72 dest = new ActiveMQQueue(destName.substring(QUEUE_PREFIX.length())); 73 74 } else if (destName.startsWith(TOPIC_PREFIX)) { 76 dest = new ActiveMQTopic(destName.substring(TOPIC_PREFIX.length())); 77 78 } else { 80 dest = new ActiveMQQueue(destName); 81 } 82 83 List addMsgs = AmqMessagesUtil.getMessages(getBrokerUrl(), dest, queryAddObjects); 85 86 if (querySubObjects.size() > 0) { 88 List subMsgs = AmqMessagesUtil.getMessages(getBrokerUrl(), dest, querySubObjects); 89 addMsgs.removeAll(subMsgs); 90 } 91 92 GlobalWriter.printMessage(AmqMessagesUtil.filterMessagesView(addMsgs, groupViews, queryViews)); 94 } 95 96 } catch (Exception e) { 97 GlobalWriter.printException(new RuntimeException ("Failed to execute browse task. Reason: " + e)); 98 throw new Exception (e); 99 } 100 } 101 102 108 protected void handleOption(String token, List tokens) throws Exception { 109 110 if (token.startsWith("--msgsel")) { 112 113 if (tokens.isEmpty() || ((String )tokens.get(0)).startsWith("-")) { 115 GlobalWriter.printException(new IllegalArgumentException ("Message selector not specified")); 116 return; 117 } 118 119 StringTokenizer queryTokens = new StringTokenizer ((String )tokens.remove(0), COMMAND_OPTION_DELIMETER); 120 while (queryTokens.hasMoreTokens()) { 121 queryAddObjects.add(queryTokens.nextToken()); 122 } 123 } 124 125 else if (token.startsWith("--xmsgsel")) { 127 128 if (tokens.isEmpty() || ((String )tokens.get(0)).startsWith("-")) { 130 GlobalWriter.printException(new IllegalArgumentException ("Message selector not specified")); 131 return; 132 } 133 134 StringTokenizer queryTokens = new StringTokenizer ((String )tokens.remove(0), COMMAND_OPTION_DELIMETER); 135 while (queryTokens.hasMoreTokens()) { 136 querySubObjects.add(queryTokens.nextToken()); 137 } 138 139 } 140 141 else if (token.startsWith("--view")) { 143 144 if (tokens.isEmpty() || ((String )tokens.get(0)).startsWith("-")) { 146 GlobalWriter.printException(new IllegalArgumentException ("Attributes to view not specified")); 147 return; 148 } 149 150 StringTokenizer viewTokens = new StringTokenizer ((String )tokens.remove(0), COMMAND_OPTION_DELIMETER); 152 while (viewTokens.hasMoreTokens()) { 153 String viewToken = viewTokens.nextToken(); 154 155 if (viewToken.equals(VIEW_GROUP_HEADER)) { 157 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken.substring(VIEW_GROUP_HEADER.length())); 158 159 } else if (viewToken.equals(VIEW_GROUP_CUSTOM)) { 161 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken.substring(VIEW_GROUP_CUSTOM.length())); 162 163 } else if (viewToken.equals(VIEW_GROUP_BODY)) { 165 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken.substring(VIEW_GROUP_BODY.length())); 166 167 } else { 169 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken); 170 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken); 171 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken); 172 } 173 } 174 } 175 176 else if (token.startsWith("-V")) { 178 String viewGroup = token.substring(2); 179 if (viewGroup.equals("header")) { 181 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX); 182 183 } else if (viewGroup.equals("custom")) { 185 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX); 186 187 } else if (viewGroup.equals("body")) { 189 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX); 190 191 } else { 193 GlobalWriter.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option."); 194 } 195 } 196 197 else { 199 super.handleOption(token, tokens); 200 } 201 } 202 203 206 protected void printHelp() { 207 GlobalWriter.printHelp(helpFile); 208 } 209 210 protected String [] helpFile = new String [] { 211 "Task Usage: Main browse --amqurl <broker url> [browse-options] <destinations>", 212 "Description: Display selected destination's messages.", 213 "", 214 "Browse Options:", 215 " --amqurl <url> Set the broker URL to connect to.", 216 " --msgsel <msgsel1,msglsel2> Add to the search list messages matched by the query similar to", 217 " the messages selector format.", 218 " -V<header|custom|body> Predefined view that allows you to view the message header, custom", 219 " message header, or the message body.", 220 " --view <attr1>,<attr2>,... Select the specific attribute of the message to view.", 221 " --version Display the version information.", 222 " -h,-?,--help Display the browse broker help information.", 223 "", 224 "Examples:", 225 " Main browse --amqurl tcp://localhost:61616 FOO.BAR", 226 " - Print the message header, custom message header, and message body of all messages in the", 227 " queue FOO.BAR", 228 "", 229 " Main browse --amqurl tcp://localhost:61616 -Vheader,body queue:FOO.BAR", 230 " - Print only the message header and message body of all messages in the queue FOO.BAR", 231 "", 232 " Main browse --amqurl tcp://localhost:61616 -Vheader --view custom:MyField queue:FOO.BAR", 233 " - Print the message header and the custom field 'MyField' of all messages in the queue FOO.BAR", 234 "", 235 " Main browse --amqurl tcp://localhost:61616 --msgsel JMSMessageID='*:10',JMSPriority>5 FOO.BAR", 236 " - Print all the message fields that has a JMSMessageID in the header field that matches the", 237 " wildcard *:10, and has a JMSPriority field > 5 in the queue FOO.BAR", 238 " * To use wildcard queries, the field must be a string and the query enclosed in ''", 239 "", 240 }; 241 } 242 | Popular Tags |