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 */ 017 package org.apache.activemq.console.command; 018 019 import java.util.ArrayList; 020 import java.util.HashSet; 021 import java.util.Iterator; 022 import java.util.List; 023 import java.util.Set; 024 import java.util.StringTokenizer; 025 026 import javax.management.ObjectInstance; 027 028 import org.apache.activemq.console.util.AmqMessagesUtil; 029 import org.apache.activemq.console.util.JmxMBeansUtil; 030 031 public 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 " --jmxuser <user> Set the JMX user used for authenticating.", 051 " --jmxpassword <password> Set the JMX password used for authenticating.", 052 " --jmxlocal Use the local JMX server instead of a remote one.", 053 " --version Display the version information.", 054 " -h,-?,--help Display the browse broker help information.", 055 "", 056 "Examples:", 057 " Main browse FOO.BAR", 058 " - Print the message header, custom message header, and message body of all messages in the", 059 " queue FOO.BAR", 060 "", 061 " Main browse -Vheader,body queue:FOO.BAR", 062 " - Print only the message header and message body of all messages in the queue FOO.BAR", 063 "", 064 " Main browse -Vheader --view custom:MyField queue:FOO.BAR", 065 " - Print the message header and the custom field 'MyField' of all messages in the queue FOO.BAR", 066 "", 067 " Main browse --msgsel JMSMessageID='*:10',JMSPriority>5 FOO.BAR", 068 " - Print all the message fields that has a JMSMessageID in the header field that matches the", 069 " wildcard *:10, and has a JMSPriority field > 5 in the queue FOO.BAR", 070 " * To use wildcard queries, the field must be a string and the query enclosed in ''", 071 "" 072 }; 073 074 private final List<String> queryAddObjects = new ArrayList<String>(10); 075 private final List<String> querySubObjects = new ArrayList<String>(10); 076 private final Set<String> groupViews = new HashSet<String>(10); 077 private final Set queryViews = new HashSet(10); 078 079 /** 080 * Execute the browse command, which allows you to browse the messages in a 081 * given JMS destination 082 * 083 * @param tokens - command arguments 084 * @throws Exception 085 */ 086 protected void runTask(List<String> tokens) throws Exception { 087 try { 088 // If there is no queue name specified, let's select all 089 if (tokens.isEmpty()) { 090 tokens.add("*"); 091 } 092 093 // Iterate through the queue names 094 for (Iterator<String> i = tokens.iterator(); i.hasNext();) { 095 List queueList = JmxMBeansUtil.queryMBeans(createJmxConnection(), "Type=Queue,Destination=" + i.next() + ",*"); 096 097 // Iterate through the queue result 098 for (Iterator j = queueList.iterator(); j.hasNext();) { 099 List messages = JmxMBeansUtil.createMessageQueryFilter(createJmxConnection(), ((ObjectInstance)j.next()).getObjectName()).query(queryAddObjects); 100 context.printMessage(JmxMBeansUtil.filterMessagesView(messages, groupViews, queryViews)); 101 } 102 } 103 } catch (Exception e) { 104 context.printException(new RuntimeException("Failed to execute browse task. Reason: " + e)); 105 throw new Exception(e); 106 } 107 } 108 109 /** 110 * Handle the --msgsel, --xmsgsel, --view, -V options. 111 * 112 * @param token - option token to handle 113 * @param tokens - succeeding command arguments 114 * @throws Exception 115 */ 116 protected void handleOption(String token, List<String> tokens) throws Exception { 117 118 // If token is an additive message selector option 119 if (token.startsWith("--msgsel")) { 120 121 // If no message selector is specified, or next token is a new 122 // option 123 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 124 context.printException(new IllegalArgumentException("Message selector not specified")); 125 return; 126 } 127 128 StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 129 while (queryTokens.hasMoreTokens()) { 130 queryAddObjects.add(queryTokens.nextToken()); 131 } 132 } else if (token.startsWith("--xmsgsel")) { 133 // If token is a substractive message selector option 134 135 // If no message selector is specified, or next token is a new 136 // option 137 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 138 context.printException(new IllegalArgumentException("Message selector not specified")); 139 return; 140 } 141 142 StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 143 while (queryTokens.hasMoreTokens()) { 144 querySubObjects.add(queryTokens.nextToken()); 145 } 146 147 } else if (token.startsWith("--view")) { 148 // If token is a view option 149 150 // If no view specified, or next token is a new option 151 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 152 context.printException(new IllegalArgumentException("Attributes to view not specified")); 153 return; 154 } 155 156 // Add the attributes to view 157 StringTokenizer viewTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 158 while (viewTokens.hasMoreTokens()) { 159 String viewToken = viewTokens.nextToken(); 160 161 // If view is explicitly specified to belong to the JMS header 162 if (viewToken.equals(VIEW_GROUP_HEADER)) { 163 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken.substring(VIEW_GROUP_HEADER.length())); 164 165 // If view is explicitly specified to belong to the JMS 166 // custom header 167 } else if (viewToken.equals(VIEW_GROUP_CUSTOM)) { 168 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken.substring(VIEW_GROUP_CUSTOM.length())); 169 170 // If view is explicitly specified to belong to the JMS body 171 } else if (viewToken.equals(VIEW_GROUP_BODY)) { 172 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken.substring(VIEW_GROUP_BODY.length())); 173 174 // If no view explicitly specified, let's check the view for 175 // each group 176 } else { 177 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken); 178 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken); 179 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken); 180 } 181 } 182 } else if (token.startsWith("-V")) { 183 // If token is a predefined group view option 184 String viewGroup = token.substring(2); 185 // If option is a header group view 186 if (viewGroup.equals("header")) { 187 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX); 188 189 // If option is a custom header group view 190 } else if (viewGroup.equals("custom")) { 191 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX); 192 193 // If option is a body group view 194 } else if (viewGroup.equals("body")) { 195 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX); 196 197 // Unknown group view 198 } else { 199 context.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option."); 200 } 201 } else { 202 // Let super class handle unknown option 203 super.handleOption(token, tokens); 204 } 205 } 206 207 /** 208 * Print the help messages for the browse command 209 */ 210 protected void printHelp() { 211 context.printHelp(helpFile); 212 } 213 214 }