KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > command > AmqBrowseCommand


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.console.command;
19
20 import org.apache.activemq.console.util.AmqMessagesUtil;
21 import org.apache.activemq.console.formatter.GlobalWriter;
22 import org.apache.activemq.command.ActiveMQQueue;
23 import org.apache.activemq.command.ActiveMQTopic;
24
25 import javax.jms.Destination JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 public class AmqBrowseCommand extends AbstractAmqCommand {
34     public static final String JavaDoc QUEUE_PREFIX = "queue:";
35     public static final String JavaDoc TOPIC_PREFIX = "topic:";
36
37     public static final String JavaDoc VIEW_GROUP_HEADER = "header:";
38     public static final String JavaDoc VIEW_GROUP_CUSTOM = "custom:";
39     public static final String JavaDoc VIEW_GROUP_BODY = "body:";
40
41     private final List JavaDoc queryAddObjects = new ArrayList JavaDoc(10);
42     private final List JavaDoc querySubObjects = new ArrayList JavaDoc(10);
43     private final Set JavaDoc groupViews = new HashSet JavaDoc(10);
44     private final Set JavaDoc queryViews = new HashSet JavaDoc(10);
45
46     /**
47      * Execute the browse command, which allows you to browse the messages in a given JMS destination
48      * @param tokens - command arguments
49      * @throws Exception
50      */

51     protected void runTask(List JavaDoc tokens) throws Exception JavaDoc {
52         try {
53             // If no destination specified
54
if (tokens.isEmpty()) {
55                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("No JMS destination specified."));
56                 return;
57             }
58
59             // If no broker url specified
60
if (getBrokerUrl() == null) {
61                 GlobalWriter.printException(new IllegalStateException JavaDoc("No broker url specified. Use the --amqurl option to specify a broker url."));
62                 return;
63             }
64
65             // Display the messages for each destination
66
for (Iterator JavaDoc i=tokens.iterator(); i.hasNext();) {
67                 String JavaDoc destName = (String JavaDoc)i.next();
68                 Destination JavaDoc dest;
69
70                 // If destination has been explicitly specified as a queue
71
if (destName.startsWith(QUEUE_PREFIX)) {
72                     dest = new ActiveMQQueue(destName.substring(QUEUE_PREFIX.length()));
73
74                 // If destination has been explicitly specified as a topic
75
} else if (destName.startsWith(TOPIC_PREFIX)) {
76                     dest = new ActiveMQTopic(destName.substring(TOPIC_PREFIX.length()));
77
78                 // By default destination is assumed to be a queue
79
} else {
80                     dest = new ActiveMQQueue(destName);
81                 }
82
83                 // Query for the messages to view
84
List JavaDoc addMsgs = AmqMessagesUtil.getMessages(getBrokerUrl(), dest, queryAddObjects);
85
86                 // Query for the messages to remove from view
87
if (querySubObjects.size() > 0) {
88                     List JavaDoc subMsgs = AmqMessagesUtil.getMessages(getBrokerUrl(), dest, querySubObjects);
89                     addMsgs.removeAll(subMsgs);
90                 }
91
92                 // Display the messages
93
GlobalWriter.printMessage(AmqMessagesUtil.filterMessagesView(addMsgs, groupViews, queryViews));
94             }
95
96         } catch (Exception JavaDoc e) {
97             GlobalWriter.printException(new RuntimeException JavaDoc("Failed to execute browse task. Reason: " + e));
98             throw new Exception JavaDoc(e);
99         }
100     }
101
102     /**
103      * Handle the --msgsel, --xmsgsel, --view, -V options.
104      * @param token - option token to handle
105      * @param tokens - succeeding command arguments
106      * @throws Exception
107      */

108     protected void handleOption(String JavaDoc token, List JavaDoc tokens) throws Exception JavaDoc {
109
110         // If token is an additive message selector option
111
if (token.startsWith("--msgsel")) {
112
113             // If no message selector is specified, or next token is a new option
114
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
115                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Message selector not specified"));
116                 return;
117             }
118
119             StringTokenizer JavaDoc queryTokens = new StringTokenizer JavaDoc((String JavaDoc)tokens.remove(0), COMMAND_OPTION_DELIMETER);
120             while (queryTokens.hasMoreTokens()) {
121                 queryAddObjects.add(queryTokens.nextToken());
122             }
123         }
124
125         // If token is a substractive message selector option
126
else if (token.startsWith("--xmsgsel")) {
127
128             // If no message selector is specified, or next token is a new option
129
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
130                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Message selector not specified"));
131                 return;
132             }
133
134             StringTokenizer JavaDoc queryTokens = new StringTokenizer JavaDoc((String JavaDoc)tokens.remove(0), COMMAND_OPTION_DELIMETER);
135             while (queryTokens.hasMoreTokens()) {
136                 querySubObjects.add(queryTokens.nextToken());
137             }
138
139         }
140
141         // If token is a view option
142
else if (token.startsWith("--view")) {
143
144             // If no view specified, or next token is a new option
145
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
146                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Attributes to view not specified"));
147                 return;
148             }
149
150             // Add the attributes to view
151
StringTokenizer JavaDoc viewTokens = new StringTokenizer JavaDoc((String JavaDoc)tokens.remove(0), COMMAND_OPTION_DELIMETER);
152             while (viewTokens.hasMoreTokens()) {
153                 String JavaDoc viewToken = viewTokens.nextToken();
154
155                 // If view is explicitly specified to belong to the JMS header
156
if (viewToken.equals(VIEW_GROUP_HEADER)) {
157                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken.substring(VIEW_GROUP_HEADER.length()));
158
159                 // If view is explicitly specified to belong to the JMS custom header
160
} else if (viewToken.equals(VIEW_GROUP_CUSTOM)) {
161                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken.substring(VIEW_GROUP_CUSTOM.length()));
162
163                 // If view is explicitly specified to belong to the JMS body
164
} else if (viewToken.equals(VIEW_GROUP_BODY)) {
165                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken.substring(VIEW_GROUP_BODY.length()));
166
167                 // If no view explicitly specified, let's check the view for each group
168
} else {
169                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken);
170                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken);
171                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken);
172                 }
173             }
174         }
175
176         // If token is a predefined group view option
177
else if (token.startsWith("-V")) {
178             String JavaDoc viewGroup = token.substring(2);
179             // If option is a header group view
180
if (viewGroup.equals("header")) {
181                 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX);
182
183             // If option is a custom header group view
184
} else if (viewGroup.equals("custom")) {
185                 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX);
186
187             // If option is a body group view
188
} else if (viewGroup.equals("body")) {
189                 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX);
190
191             // Unknown group view
192
} else {
193                 GlobalWriter.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option.");
194             }
195         }
196
197         // Let super class handle unknown option
198
else {
199             super.handleOption(token, tokens);
200         }
201     }
202
203     /**
204      * Print the help messages for the browse command
205      */

206     protected void printHelp() {
207         GlobalWriter.printHelp(helpFile);
208     }
209
210     protected String JavaDoc[] helpFile = new String JavaDoc[] {
211         "Task Usage: Main browse --amqurl <broker url> [browse-options] <destinations>",
212         "Description: Display selected destination's messages.",
213         "",
214         "Browse Options:",
215         " --amqurl <url> Set the broker URL to connect to.",
216         " --msgsel <msgsel1,msglsel2> Add to the search list messages matched by the query similar to",
217         " the messages selector format.",
218         " -V<header|custom|body> Predefined view that allows you to view the message header, custom",
219         " message header, or the message body.",
220         " --view <attr1>,<attr2>,... Select the specific attribute of the message to view.",
221         " --version Display the version information.",
222         " -h,-?,--help Display the browse broker help information.",
223         "",
224         "Examples:",
225         " Main browse --amqurl tcp://localhost:61616 FOO.BAR",
226         " - Print the message header, custom message header, and message body of all messages in the",
227         " queue FOO.BAR",
228         "",
229         " Main browse --amqurl tcp://localhost:61616 -Vheader,body queue:FOO.BAR",
230         " - Print only the message header and message body of all messages in the queue FOO.BAR",
231         "",
232         " Main browse --amqurl tcp://localhost:61616 -Vheader --view custom:MyField queue:FOO.BAR",
233         " - Print the message header and the custom field 'MyField' of all messages in the queue FOO.BAR",
234         "",
235         " Main browse --amqurl tcp://localhost:61616 --msgsel JMSMessageID='*:10',JMSPriority>5 FOO.BAR",
236         " - Print all the message fields that has a JMSMessageID in the header field that matches the",
237         " wildcard *:10, and has a JMSPriority field > 5 in the queue FOO.BAR",
238         " * To use wildcard queries, the field must be a string and the query enclosed in ''",
239         "",
240     };
241 }
242
Popular Tags