KickJava   Java API By Example, From Geeks To Geeks.

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


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

50     protected void runTask(List JavaDoc tokens) throws Exception JavaDoc {
51         try {
52             // If there is no queue name specified, let's select all
53
if (tokens.isEmpty()) {
54                 tokens.add("*");
55             }
56
57             // Iterate through the queue names
58
for (Iterator JavaDoc i=tokens.iterator(); i.hasNext();) {
59                 List JavaDoc queueList = JmxMBeansUtil.queryMBeans(useJmxServiceUrl(), "Type=Queue,Destination=" + i.next() + ",*");
60
61                 // Iterate through the queue result
62
for (Iterator JavaDoc j=queueList.iterator(); j.hasNext();) {
63                     List JavaDoc messages = JmxMBeansUtil.createMessageQueryFilter(useJmxServiceUrl(), ((ObjectInstance JavaDoc)j.next()).getObjectName()).query(queryAddObjects);
64                     GlobalWriter.printMessage(JmxMBeansUtil.filterMessagesView(messages, groupViews, queryViews));
65                 }
66             }
67         } catch (Exception JavaDoc e) {
68             GlobalWriter.printException(new RuntimeException JavaDoc("Failed to execute browse task. Reason: " + e));
69             throw new Exception JavaDoc(e);
70         }
71     }
72
73     /**
74      * Handle the --msgsel, --xmsgsel, --view, -V options.
75      * @param token - option token to handle
76      * @param tokens - succeeding command arguments
77      * @throws Exception
78      */

79     protected void handleOption(String JavaDoc token, List JavaDoc tokens) throws Exception JavaDoc {
80
81         // If token is an additive message selector option
82
if (token.startsWith("--msgsel")) {
83
84             // If no message selector is specified, or next token is a new option
85
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
86                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Message selector not specified"));
87                 return;
88             }
89
90             StringTokenizer JavaDoc queryTokens = new StringTokenizer JavaDoc((String JavaDoc)tokens.remove(0), COMMAND_OPTION_DELIMETER);
91             while (queryTokens.hasMoreTokens()) {
92                 queryAddObjects.add(queryTokens.nextToken());
93             }
94         }
95
96         // If token is a substractive message selector option
97
else if (token.startsWith("--xmsgsel")) {
98
99             // If no message selector is specified, or next token is a new option
100
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
101                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Message selector not specified"));
102                 return;
103             }
104
105             StringTokenizer JavaDoc queryTokens = new StringTokenizer JavaDoc((String JavaDoc)tokens.remove(0), COMMAND_OPTION_DELIMETER);
106             while (queryTokens.hasMoreTokens()) {
107                 querySubObjects.add(queryTokens.nextToken());
108             }
109
110         }
111
112         // If token is a view option
113
else if (token.startsWith("--view")) {
114
115             // If no view specified, or next token is a new option
116
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
117                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Attributes to view not specified"));
118                 return;
119             }
120
121             // Add the attributes to view
122
StringTokenizer JavaDoc viewTokens = new StringTokenizer JavaDoc((String JavaDoc)tokens.remove(0), COMMAND_OPTION_DELIMETER);
123             while (viewTokens.hasMoreTokens()) {
124                 String JavaDoc viewToken = viewTokens.nextToken();
125
126                 // If view is explicitly specified to belong to the JMS header
127
if (viewToken.equals(VIEW_GROUP_HEADER)) {
128                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken.substring(VIEW_GROUP_HEADER.length()));
129
130                 // If view is explicitly specified to belong to the JMS custom header
131
} else if (viewToken.equals(VIEW_GROUP_CUSTOM)) {
132                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken.substring(VIEW_GROUP_CUSTOM.length()));
133
134                 // If view is explicitly specified to belong to the JMS body
135
} else if (viewToken.equals(VIEW_GROUP_BODY)) {
136                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken.substring(VIEW_GROUP_BODY.length()));
137
138                 // If no view explicitly specified, let's check the view for each group
139
} else {
140                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + viewToken);
141                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + viewToken);
142                     queryViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + viewToken);
143                 }
144             }
145         }
146
147         // If token is a predefined group view option
148
else if (token.startsWith("-V")) {
149             String JavaDoc viewGroup = token.substring(2);
150             // If option is a header group view
151
if (viewGroup.equals("header")) {
152                 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX);
153
154             // If option is a custom header group view
155
} else if (viewGroup.equals("custom")) {
156                 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX);
157
158             // If option is a body group view
159
} else if (viewGroup.equals("body")) {
160                 groupViews.add(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX);
161
162             // Unknown group view
163
} else {
164                 GlobalWriter.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option.");
165             }
166         }
167
168         // Let super class handle unknown option
169
else {
170             super.handleOption(token, tokens);
171         }
172     }
173
174     /**
175      * Print the help messages for the browse command
176      */

177     protected void printHelp() {
178         GlobalWriter.printHelp(helpFile);
179     }
180
181     protected String JavaDoc[] helpFile = new String JavaDoc[] {
182         "Task Usage: Main browse [browse-options] <destinations>",
183         "Description: Display selected destination's messages.",
184         "",
185         "Browse Options:",
186         " --msgsel <msgsel1,msglsel2> Add to the search list messages matched by the query similar to",
187         " the messages selector format.",
188         " -V<header|custom|body> Predefined view that allows you to view the message header, custom",
189         " message header, or the message body.",
190         " --view <attr1>,<attr2>,... Select the specific attribute of the message to view.",
191         " --jmxurl <url> Set the JMX URL to connect to.",
192         " --version Display the version information.",
193         " -h,-?,--help Display the browse broker help information.",
194         "",
195         "Examples:",
196         " Main browse FOO.BAR",
197         " - Print the message header, custom message header, and message body of all messages in the",
198         " queue FOO.BAR",
199         "",
200         " Main browse -Vheader,body queue:FOO.BAR",
201         " - Print only the message header and message body of all messages in the queue FOO.BAR",
202         "",
203         " Main browse -Vheader --view custom:MyField queue:FOO.BAR",
204         " - Print the message header and the custom field 'MyField' of all messages in the queue FOO.BAR",
205         "",
206         " Main browse --msgsel JMSMessageID='*:10',JMSPriority>5 FOO.BAR",
207         " - Print all the message fields that has a JMSMessageID in the header field that matches the",
208         " wildcard *:10, and has a JMSPriority field > 5 in the queue FOO.BAR",
209         " * To use wildcard queries, the field must be a string and the query enclosed in ''",
210         "",
211     };
212 }
213
Popular Tags