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 " * To use wildcard queries, the field must be a string and the query enclosed in ''", 072 "" 073 }; 074 075 private final List<String> queryAddObjects = new ArrayList<String>(10); 076 private final List<String> querySubObjects = new ArrayList<String>(10); 077 private final Set<String> groupViews = new HashSet<String>(10); 078 private final Set queryViews = new HashSet(10); 079 080 /** 081 * Execute the browse command, which allows you to browse the messages in a 082 * given JMS destination 083 * 084 * @param tokens - command arguments 085 * @throws Exception 086 */ 087 protected void runTask(List<String> tokens) throws Exception { 088 try { 089 // If there is no queue name specified, let's select all 090 if (tokens.isEmpty()) { 091 tokens.add("*"); 092 } 093 094 // Iterate through the queue names 095 for (Iterator<String> i = tokens.iterator(); i.hasNext();) { 096 List queueList = JmxMBeansUtil.queryMBeans(createJmxConnection(), "Type=Queue,Destination=" + i.next() + ",*"); 097 098 // Iterate through the queue result 099 for (Iterator j = queueList.iterator(); j.hasNext();) { 100 List messages = JmxMBeansUtil.createMessageQueryFilter(createJmxConnection(), ((ObjectInstance)j.next()).getObjectName()).query(queryAddObjects); 101 context.printMessage(JmxMBeansUtil.filterMessagesView(messages, groupViews, queryViews)); 102 } 103 } 104 } catch (Exception e) { 105 context.printException(new RuntimeException("Failed to execute browse task. Reason: " + e)); 106 throw new Exception(e); 107 } 108 } 109 110 /** 111 * Handle the --msgsel, --xmsgsel, --view, -V options. 112 * 113 * @param token - option token to handle 114 * @param tokens - succeeding command arguments 115 * @throws Exception 116 */ 117 protected void handleOption(String token, List<String> tokens) throws Exception { 118 119 // If token is an additive message selector option 120 if (token.startsWith("--msgsel")) { 121 122 // If no message selector is specified, or next token is a new 123 // option 124 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 125 context.printException(new IllegalArgumentException("Message selector not specified")); 126 return; 127 } 128 129 StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 130 while (queryTokens.hasMoreTokens()) { 131 queryAddObjects.add(queryTokens.nextToken()); 132 } 133 } else if (token.startsWith("--xmsgsel")) { 134 // If token is a substractive message selector option 135 136 // If no message selector is specified, or next token is a new 137 // option 138 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 139 context.printException(new IllegalArgumentException("Message selector not specified")); 140 return; 141 } 142 143 StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 144 while (queryTokens.hasMoreTokens()) { 145 querySubObjects.add(queryTokens.nextToken()); 146 } 147 148 } else if (token.startsWith("--view")) { 149 // If token is a view option 150 151 // If no view specified, or next token is a new option 152 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 153 context.printException(new IllegalArgumentException("Attributes to view not specified")); 154 return; 155 } 156 157 // Add the attributes to view 158 StringTokenizer viewTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 159 while (viewTokens.hasMoreTokens()) { 160 String viewToken = viewTokens.nextToken(); 161 162 // If view is explicitly specified to belong to the JMS header 163 if (viewToken.equals(VIEW_GROUP_HEADER)) { 164 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken.substring(VIEW_GROUP_HEADER.length())); 165 166 // If view is explicitly specified to belong to the JMS 167 // custom header 168 } else if (viewToken.equals(VIEW_GROUP_CUSTOM)) { 169 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken.substring(VIEW_GROUP_CUSTOM.length())); 170 171 // If view is explicitly specified to belong to the JMS body 172 } else if (viewToken.equals(VIEW_GROUP_BODY)) { 173 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken.substring(VIEW_GROUP_BODY.length())); 174 175 // If no view explicitly specified, let's check the view for 176 // each group 177 } else { 178 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken); 179 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken); 180 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken); 181 } 182 } 183 } else if (token.startsWith("-V")) { 184 // If token is a predefined group view option 185 String viewGroup = token.substring(2); 186 // If option is a header group view 187 if (viewGroup.equals("header")) { 188 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX); 189 190 // If option is a custom header group view 191 } else if (viewGroup.equals("custom")) { 192 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX); 193 194 // If option is a body group view 195 } else if (viewGroup.equals("body")) { 196 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX); 197 198 // Unknown group view 199 } else { 200 context.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option."); 201 } 202 } else { 203 // Let super class handle unknown option 204 super.handleOption(token, tokens); 205 } 206 } 207 208 /** 209 * Print the help messages for the browse command 210 */ 211 protected void printHelp() { 212 context.printHelp(helpFile); 213 } 214 215}