001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.console.command; 018 019import java.util.ArrayList; 020import java.util.HashSet; 021import java.util.Iterator; 022import java.util.List; 023import java.util.Set; 024import java.util.StringTokenizer; 025 026import javax.management.ObjectInstance; 027 028import org.apache.activemq.console.util.AmqMessagesUtil; 029import org.apache.activemq.console.util.JmxMBeansUtil; 030 031public class BrowseCommand extends AbstractJmxCommand { 032 033 public static final String QUEUE_PREFIX = "queue:"; 034 public static final String TOPIC_PREFIX = "topic:"; 035 036 public static final String VIEW_GROUP_HEADER = "header:"; 037 public static final String VIEW_GROUP_CUSTOM = "custom:"; 038 public static final String VIEW_GROUP_BODY = "body:"; 039 040 protected String[] helpFile = new String[] { 041 "Task Usage: Main browse [browse-options] <destinations>", "Description: Display selected destination's messages.", 042 "", 043 "Browse Options:", 044 " --msgsel <msgsel1,msglsel2> Add to the search list messages matched by the query similar to", 045 " the messages selector format.", 046 " -V<header|custom|body> Predefined view that allows you to view the message header, custom", 047 " message header, or the message body.", 048 " --view <attr1>,<attr2>,... Select the specific attribute of the message to view.", 049 " --jmxurl <url> Set the JMX URL to connect to.", 050 " --pid <pid> Set the pid to connect to (only on Sun JVM).", 051 " --jmxuser <user> Set the JMX user used for authenticating.", 052 " --jmxpassword <password> Set the JMX password used for authenticating.", 053 " --jmxlocal Use the local JMX server instead of a remote one.", 054 " --version Display the version information.", 055 " -h,-?,--help Display the browse broker help information.", 056 "", 057 "Examples:", 058 " Main browse FOO.BAR", 059 " - Print the message header, custom message header, and message body of all messages in the", 060 " queue FOO.BAR", 061 "", 062 " Main browse -Vheader,body queue:FOO.BAR", 063 " - Print only the message header and message body of all messages in the queue FOO.BAR", 064 "", 065 " Main browse -Vheader --view custom:MyField queue:FOO.BAR", 066 " - Print the message header and the custom field 'MyField' of all messages in the queue FOO.BAR", 067 "", 068 " Main browse --msgsel \"JMSMessageID='*:10',JMSPriority>5\" FOO.BAR", 069 " - Print all the message fields that has a JMSMessageID in the header field that matches the", 070 " wildcard *:10, and has a JMSPriority field > 5 in the queue FOO.BAR.", 071 " SLQ92 syntax is also supported.", 072 " * To use wildcard queries, the field must be a string and the query enclosed in ''", 073 " Use double quotes \"\" around the entire message selector string.", 074 "" 075 }; 076 077 private final List<String> queryAddObjects = new ArrayList<String>(10); 078 private final List<String> querySubObjects = new ArrayList<String>(10); 079 private final Set<String> groupViews = new HashSet<String>(10); 080 private final Set queryViews = new HashSet(10); 081 082 @Override 083 public String getName() { 084 return "browse"; 085 } 086 087 @Override 088 public String getOneLineDescription() { 089 return "Used to browse a destination"; 090 } 091 092 /** 093 * Execute the browse command, which allows you to browse the messages in a 094 * given JMS destination 095 * 096 * @param tokens - command arguments 097 * @throws Exception 098 */ 099 protected void runTask(List<String> tokens) throws Exception { 100 // If there is no queue name specified, let's select all 101 if (tokens.isEmpty()) { 102 tokens.add("*"); 103 } 104 105 // Iterate through the queue names 106 for (Iterator<String> i = tokens.iterator(); i.hasNext(); ) { 107 List queueList = JmxMBeansUtil.queryMBeans(createJmxConnection(), "Type=Queue,Destination=" + i.next() + ",*"); 108 109 // Iterate through the queue result 110 for (Iterator j = queueList.iterator(); j.hasNext(); ) { 111 List messages = JmxMBeansUtil.createMessageQueryFilter(createJmxConnection(), ((ObjectInstance) j.next()).getObjectName()).query(queryAddObjects); 112 context.printMessage(JmxMBeansUtil.filterMessagesView(messages, groupViews, queryViews)); 113 } 114 } 115 } 116 117 /** 118 * Handle the --msgsel, --xmsgsel, --view, -V options. 119 * 120 * @param token - option token to handle 121 * @param tokens - succeeding command arguments 122 * @throws Exception 123 */ 124 protected void handleOption(String token, List<String> tokens) throws Exception { 125 126 // If token is an additive message selector option 127 if (token.startsWith("--msgsel")) { 128 129 // If no message selector is specified, or next token is a new 130 // option 131 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 132 context.printException(new IllegalArgumentException("Message selector not specified")); 133 return; 134 } 135 136 StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 137 while (queryTokens.hasMoreTokens()) { 138 queryAddObjects.add(queryTokens.nextToken()); 139 } 140 } else if (token.startsWith("--xmsgsel")) { 141 // If token is a substractive message selector option 142 143 // If no message selector is specified, or next token is a new 144 // option 145 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 146 context.printException(new IllegalArgumentException("Message selector not specified")); 147 return; 148 } 149 150 StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 151 while (queryTokens.hasMoreTokens()) { 152 querySubObjects.add(queryTokens.nextToken()); 153 } 154 155 } else if (token.startsWith("--view")) { 156 // If token is a view option 157 158 // If no view specified, or next token is a new option 159 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 160 context.printException(new IllegalArgumentException("Attributes to view not specified")); 161 return; 162 } 163 164 // Add the attributes to view 165 StringTokenizer viewTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 166 while (viewTokens.hasMoreTokens()) { 167 String viewToken = viewTokens.nextToken(); 168 169 // If view is explicitly specified to belong to the JMS header 170 if (viewToken.equals(VIEW_GROUP_HEADER)) { 171 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken.substring(VIEW_GROUP_HEADER.length())); 172 173 // If view is explicitly specified to belong to the JMS 174 // custom header 175 } else if (viewToken.equals(VIEW_GROUP_CUSTOM)) { 176 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken.substring(VIEW_GROUP_CUSTOM.length())); 177 178 // If view is explicitly specified to belong to the JMS body 179 } else if (viewToken.equals(VIEW_GROUP_BODY)) { 180 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken.substring(VIEW_GROUP_BODY.length())); 181 182 // If no view explicitly specified, let's check the view for 183 // each group 184 } else { 185 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken); 186 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken); 187 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken); 188 } 189 } 190 } else if (token.startsWith("-V")) { 191 // If token is a predefined group view option 192 String viewGroup = token.substring(2); 193 // If option is a header group view 194 if (viewGroup.equals("header")) { 195 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX); 196 197 // If option is a custom header group view 198 } else if (viewGroup.equals("custom")) { 199 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX); 200 201 // If option is a body group view 202 } else if (viewGroup.equals("body")) { 203 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX); 204 205 // Unknown group view 206 } else { 207 context.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option."); 208 } 209 } else { 210 // Let super class handle unknown option 211 super.handleOption(token, tokens); 212 } 213 } 214 215 /** 216 * Print the help messages for the browse command 217 */ 218 protected void printHelp() { 219 context.printHelp(helpFile); 220 } 221 222}