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