KickJava   Java API By Example, From Geeks To Geeks.

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


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.JmxMBeansUtil;
21 import org.apache.activemq.console.formatter.GlobalWriter;
22
23 import java.util.List JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30
31 public class QueryCommand extends AbstractJmxCommand {
32     // Predefined type=identifier query
33
private static final Properties JavaDoc PREDEFINED_OBJNAME_QUERY = new Properties JavaDoc();
34
35     static {
36         PREDEFINED_OBJNAME_QUERY.setProperty("Broker", "Type=Broker,BrokerName=%1,*");
37         PREDEFINED_OBJNAME_QUERY.setProperty("Connection", "Type=Connection,Connection=%1,*");
38         PREDEFINED_OBJNAME_QUERY.setProperty("Connector", "Type=Connector,ConnectorName=%1,*");
39         PREDEFINED_OBJNAME_QUERY.setProperty("NetworkConnector", "Type=NetworkConnector,BrokerName=%1,*");
40         PREDEFINED_OBJNAME_QUERY.setProperty("Queue", "Type=Queue,Destination=%1,*");
41         PREDEFINED_OBJNAME_QUERY.setProperty("Topic", "Type=Topic,Destination=%1,*");
42     };
43
44     private final List JavaDoc queryAddObjects = new ArrayList JavaDoc(10);
45     private final List JavaDoc querySubObjects = new ArrayList JavaDoc(10);
46     private final Set JavaDoc queryViews = new HashSet JavaDoc(10);
47
48     /**
49      * Queries the mbeans registered in the specified JMX context
50      * @param tokens - command arguments
51      * @throws Exception
52      */

53     protected void runTask(List JavaDoc tokens) throws Exception JavaDoc {
54         try {
55             // Query for the mbeans to add
56
List JavaDoc addMBeans = JmxMBeansUtil.queryMBeans(useJmxServiceUrl(), queryAddObjects, queryViews);
57
58             // Query for the mbeans to sub
59
if (querySubObjects.size() > 0) {
60                 List JavaDoc subMBeans = JmxMBeansUtil.queryMBeans(useJmxServiceUrl(), querySubObjects, queryViews);
61                 addMBeans.removeAll(subMBeans);
62             }
63
64
65             GlobalWriter.printMBean(JmxMBeansUtil.filterMBeansView(addMBeans, queryViews));
66
67         } catch (Exception JavaDoc e) {
68             GlobalWriter.printException(new RuntimeException JavaDoc("Failed to execute query task. Reason: " + e));
69             throw new Exception JavaDoc(e);
70         }
71     }
72
73     /**
74      * Handle the -Q, -xQ, --objname, --xobjname, --view 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         // If token is a additive predefined query define option
81
if (token.startsWith("-Q")) {
82             String JavaDoc key = token.substring(2);
83             String JavaDoc value = "";
84             int pos = key.indexOf("=");
85             if (pos >= 0) {
86                 value = key.substring(pos + 1);
87                 key = key.substring(0, pos);
88             }
89
90             // If additive query
91
String JavaDoc predefQuery = PREDEFINED_OBJNAME_QUERY.getProperty(key);
92             if (predefQuery == null) {
93                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Unknown query object type: " + key));
94                 return;
95             }
96             String JavaDoc queryStr = JmxMBeansUtil.createQueryString(predefQuery, value);
97             StringTokenizer JavaDoc queryTokens = new StringTokenizer JavaDoc(queryStr, COMMAND_OPTION_DELIMETER);
98             while (queryTokens.hasMoreTokens()) {
99                 queryAddObjects.add(queryTokens.nextToken());
100             }
101         }
102
103         // If token is a substractive predefined query define option
104
else if (token.startsWith("-xQ")) {
105             String JavaDoc key = token.substring(3);
106             String JavaDoc value = "";
107             int pos = key.indexOf("=");
108             if (pos >= 0) {
109                 value = key.substring(pos + 1);
110                 key = key.substring(0, pos);
111             }
112
113             // If subtractive query
114
String JavaDoc predefQuery = PREDEFINED_OBJNAME_QUERY.getProperty(key);
115             if (predefQuery == null) {
116                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Unknown query object type: " + key));
117                 return;
118             }
119             String JavaDoc queryStr = JmxMBeansUtil.createQueryString(predefQuery, value);
120             StringTokenizer JavaDoc queryTokens = new StringTokenizer JavaDoc(queryStr, COMMAND_OPTION_DELIMETER);
121             while (queryTokens.hasMoreTokens()) {
122                 querySubObjects.add(queryTokens.nextToken());
123             }
124         }
125
126         // If token is an additive object name query option
127
else if (token.startsWith("--objname")) {
128
129             // If no object name query is specified, or next token is a new option
130
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
131                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Object name query not specified"));
132                 return;
133             }
134
135             StringTokenizer JavaDoc queryTokens = new StringTokenizer JavaDoc((String JavaDoc)tokens.remove(0), COMMAND_OPTION_DELIMETER);
136             while (queryTokens.hasMoreTokens()) {
137                 queryAddObjects.add(queryTokens.nextToken());
138             }
139         }
140
141         // If token is a substractive object name query option
142
else if (token.startsWith("--xobjname")) {
143
144             // If no object name query is specified, or next token is a new option
145
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
146                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Object name query not specified"));
147                 return;
148             }
149
150             StringTokenizer JavaDoc queryTokens = new StringTokenizer JavaDoc((String JavaDoc)tokens.remove(0), COMMAND_OPTION_DELIMETER);
151             while (queryTokens.hasMoreTokens()) {
152                 querySubObjects.add(queryTokens.nextToken());
153             }
154         }
155
156         // If token is a view option
157
else if (token.startsWith("--view")) {
158
159             // If no view specified, or next token is a new option
160
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
161                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Attributes to view not specified"));
162                 return;
163             }
164
165             // Add the attributes to view
166
Enumeration JavaDoc viewTokens = new StringTokenizer JavaDoc((String JavaDoc)tokens.remove(0), COMMAND_OPTION_DELIMETER);
167             while (viewTokens.hasMoreElements()) {
168                 queryViews.add(viewTokens.nextElement());
169             }
170         }
171
172         // Let super class handle unknown option
173
else {
174             super.handleOption(token, tokens);
175         }
176     }
177
178     /**
179      * Print the help messages for the browse command
180      */

181     protected void printHelp() {
182         GlobalWriter.printHelp(helpFile);
183     }
184     
185     protected String JavaDoc[] helpFile = new String JavaDoc[] {
186         "Task Usage: Main query [query-options]",
187         "Description: Display selected broker component's attributes and statistics.",
188         "",
189         "Query Options:",
190         " -Q<type>=<name> Add to the search list the specific object type matched",
191         " by the defined object identifier.",
192         " -xQ<type>=<name> Remove from the search list the specific object type",
193         " matched by the object identifier.",
194         " --objname <query> Add to the search list objects matched by the query similar",
195         " to the JMX object name format.",
196         " --xobjname <query> Remove from the search list objects matched by the query",
197         " similar to the JMX object name format.",
198         " --view <attr1>,<attr2>,... Select the specific attribute of the object to view.",
199         " By default all attributes will be displayed.",
200         " --jmxurl <url> Set the JMX URL to connect to.",
201         " --version Display the version information.",
202         " -h,-?,--help Display the query broker help information.",
203         "",
204         "Examples:",
205         " query",
206         " - Print all the attributes of all registered objects queues, topics, connections, etc).",
207         "",
208         " query -QQueue=TEST.FOO",
209         " - Print all the attributes of the queue with destination name TEST.FOO.",
210         "",
211         " query -QTopic=*",
212         " - Print all the attributes of all registered topics.",
213         "",
214         " query --view EnqueueCount,DequeueCount",
215         " - Print the attributes EnqueueCount and DequeueCount of all registered objects.",
216         "",
217         " query -QTopic=* --view EnqueueCount,DequeueCount",
218         " - Print the attributes EnqueueCount and DequeueCount of all registered topics.",
219         "",
220         " query -QTopic=* -QQueue=* --view EnqueueCount,DequeueCount",
221         " - Print the attributes EnqueueCount and DequeueCount of all registered topics and",
222         " queues.",
223         "",
224         " query -QTopic=* -xQTopic=ActiveMQ.Advisory.*",
225         " - Print all attributes of all topics except those that has a name that begins",
226         " with \"ActiveMQ.Advisory\".",
227         "",
228         " query --objname Type=*Connect*,BrokerName=local* -xQNetworkConnector=*",
229         " - Print all attributes of all connectors, connections excluding network connectors",
230         " that belongs to the broker that begins with local.",
231         "",
232         " query -QQueue=* -xQQueue=????",
233         " - Print all attributes of all queues except those that are 4 letters long.",
234         "",
235     };
236 }
237
Popular Tags