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.jms.Destination;
027
028 import org.apache.activemq.command.ActiveMQQueue;
029 import org.apache.activemq.command.ActiveMQTopic;
030 import org.apache.activemq.console.util.AmqMessagesUtil;
031
032 public class AmqBrowseCommand extends AbstractAmqCommand {
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 --amqurl <broker url> [browse-options] <destinations>",
042 "Description: Display selected destination's messages.",
043 "",
044 "Browse Options:",
045 " --amqurl <url> Set the broker URL to connect to.",
046 " --msgsel <msgsel1,msglsel2> Add to the search list messages matched by the query similar to",
047 " the messages selector format.",
048 " --factory <className> Load className as the javax.jms.ConnectionFactory to use for creating connections.",
049 " --passwordFactory <className> Load className as the org.apache.activemq.console.command.PasswordFactory",
050 " for retrieving the password from a keystore.",
051 " --user <username> Username to use for JMS connections.",
052 " --password <password> Password to use for JMS connections.",
053 " -V<header|custom|body> Predefined view that allows you to view the message header, custom",
054 " message header, or the message body.",
055 " --view <attr1>,<attr2>,... Select the specific attribute of the message to view.",
056 " --version Display the version information.",
057 " -h,-?,--help Display the browse broker help information.",
058 "",
059 "Examples:",
060 " Main browse --amqurl tcp://localhost:61616 FOO.BAR",
061 " - Print the message header, custom message header, and message body of all messages in the",
062 " queue FOO.BAR",
063 "",
064 " Main browse --amqurl tcp://localhost:61616 -Vheader,body queue:FOO.BAR",
065 " - Print only the message header and message body of all messages in the queue FOO.BAR",
066 "",
067 " Main browse --amqurl tcp://localhost:61616 -Vheader --view custom:MyField queue:FOO.BAR",
068 " - Print the message header and the custom field 'MyField' of all messages in the queue FOO.BAR",
069 "",
070 " Main browse --amqurl tcp://localhost:61616 --msgsel JMSMessageID='*:10',JMSPriority>5 FOO.BAR",
071 " - Print all the message fields that has a JMSMessageID in the header field that matches the",
072 " wildcard *:10, and has a JMSPriority field > 5 in the queue FOO.BAR",
073 " * To use wildcard queries, the field must be a string and the query enclosed in ''",
074 "",
075 " Main browse --amqurl tcp://localhost:61616 --user someUser --password somePass FOO.BAR",
076 " - Print the message header, custom message header, and message body of all messages in the",
077 " queue FOO.BAR, using someUser as the user name, and somePass as the password",
078 "",
079 " Main browse --amqurl tcp://localhost:61616 --user someUser --password somePass --factory org.apache.activemq.ActiveMQConnectionFactory --passwordFactory org.apache.activemq.AMQPasswordFactory FOO.BAR",
080 " - Print the message header, custom message header, and message body of all messages in the",
081 " queue FOO.BAR, using someUser as the user name, org.apache.activemq.AMQFactorySubClass to create JMS connections,",
082 " and org.apache.activemq.console.command.DefaultPasswordFactory to turn somePass into the password to be used.",
083 "",
084 };
085
086 private final List<String> queryAddObjects = new ArrayList<String>(10);
087 private final List<String> querySubObjects = new ArrayList<String>(10);
088 private final Set<String> groupViews = new HashSet<String>(10);
089 private final Set queryViews = new HashSet(10);
090
091 /**
092 * Execute the browse command, which allows you to browse the messages in a
093 * given JMS destination
094 *
095 * @param tokens - command arguments
096 * @throws Exception
097 */
098 protected void runTask(List tokens) throws Exception {
099 try {
100 // If no destination specified
101 if (tokens.isEmpty()) {
102 context.printException(new IllegalArgumentException("No JMS destination specified."));
103 return;
104 }
105
106 // If no broker url specified
107 if (getBrokerUrl() == null) {
108 context.printException(new IllegalStateException("No broker url specified. Use the --amqurl option to specify a broker url."));
109 return;
110 }
111
112 // Display the messages for each destination
113 for (Iterator i = tokens.iterator(); i.hasNext();) {
114 String destName = (String)i.next();
115 Destination dest;
116
117 // If destination has been explicitly specified as a queue
118 if (destName.startsWith(QUEUE_PREFIX)) {
119 dest = new ActiveMQQueue(destName.substring(QUEUE_PREFIX.length()));
120
121 // If destination has been explicitly specified as a topic
122 } else if (destName.startsWith(TOPIC_PREFIX)) {
123 dest = new ActiveMQTopic(destName.substring(TOPIC_PREFIX.length()));
124
125 // By default destination is assumed to be a queue
126 } else {
127 dest = new ActiveMQQueue(destName);
128 }
129
130 // Query for the messages to view
131 List addMsgs = AmqMessagesUtil.getMessages(getConnectionFactory(), dest, queryAddObjects);
132
133 // Query for the messages to remove from view
134 if (querySubObjects.size() > 0) {
135 List subMsgs = AmqMessagesUtil.getMessages(getConnectionFactory(), dest, querySubObjects);
136 addMsgs.removeAll(subMsgs);
137 }
138
139 // Display the messages
140 context.printMessage(AmqMessagesUtil.filterMessagesView(addMsgs, groupViews, queryViews));
141 }
142
143 } catch (Exception e) {
144 context.printException(new RuntimeException("Failed to execute browse task. Reason: " + e));
145 throw new Exception(e);
146 }
147 }
148
149 /**
150 * Handle the --msgsel, --xmsgsel, --view, -V options.
151 *
152 * @param token - option token to handle
153 * @param tokens - succeeding command arguments
154 * @throws Exception
155 */
156 protected void handleOption(String token, List tokens) throws Exception {
157
158 // If token is an additive message selector option
159 if (token.startsWith("--msgsel")) {
160
161 // If no message selector is specified, or next token is a new
162 // option
163 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
164 context.printException(new IllegalArgumentException("Message selector not specified"));
165 return;
166 }
167
168 StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER);
169 while (queryTokens.hasMoreTokens()) {
170 queryAddObjects.add(queryTokens.nextToken());
171 }
172 } else if (token.startsWith("--xmsgsel")) {
173 // If token is a substractive message selector option
174
175 // If no message selector is specified, or next token is a new option
176 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
177 context.printException(new IllegalArgumentException("Message selector not specified"));
178 return;
179 }
180
181 StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER);
182 while (queryTokens.hasMoreTokens()) {
183 querySubObjects.add(queryTokens.nextToken());
184 }
185
186 } else if (token.startsWith("--view")) {
187 // If token is a view option
188
189 // If no view specified, or next token is a new option
190 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
191 context.printException(new IllegalArgumentException("Attributes to view not specified"));
192 return;
193 }
194
195 // Add the attributes to view
196 StringTokenizer viewTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER);
197 while (viewTokens.hasMoreTokens()) {
198 String viewToken = viewTokens.nextToken();
199
200 // If view is explicitly specified to belong to the JMS header
201 if (viewToken.equals(VIEW_GROUP_HEADER)) {
202 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken.substring(VIEW_GROUP_HEADER.length()));
203
204 // If view is explicitly specified to belong to the JMS
205 // custom header
206 } else if (viewToken.equals(VIEW_GROUP_CUSTOM)) {
207 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken.substring(VIEW_GROUP_CUSTOM.length()));
208
209 // If view is explicitly specified to belong to the JMS body
210 } else if (viewToken.equals(VIEW_GROUP_BODY)) {
211 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken.substring(VIEW_GROUP_BODY.length()));
212
213 // If no view explicitly specified, let's check the view for
214 // each group
215 } else {
216 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken);
217 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken);
218 queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken);
219 }
220 }
221 } else if (token.startsWith("-V")) {
222 // If token is a predefined group view option
223 String viewGroup = token.substring(2);
224 // If option is a header group view
225 if (viewGroup.equals("header")) {
226 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX);
227
228 // If option is a custom header group view
229 } else if (viewGroup.equals("custom")) {
230 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX);
231
232 // If option is a body group view
233 } else if (viewGroup.equals("body")) {
234 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX);
235
236 // Unknown group view
237 } else {
238 context.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option.");
239 }
240 } else {
241 // Let super class handle unknown option
242 super.handleOption(token, tokens);
243 }
244 }
245
246 /**
247 * Print the help messages for the browse command
248 */
249 protected void printHelp() {
250 context.printHelp(helpFile);
251 }
252
253 }