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.Enumeration; 021 import java.util.HashSet; 022 import java.util.List; 023 import java.util.Properties; 024 import java.util.Set; 025 import java.util.StringTokenizer; 026 027 import org.apache.activemq.console.util.JmxMBeansUtil; 028 029 public class QueryCommand extends AbstractJmxCommand { 030 // Predefined type=identifier query 031 private static final Properties PREDEFINED_OBJNAME_QUERY = new Properties(); 032 033 static { 034 PREDEFINED_OBJNAME_QUERY.setProperty("Broker", "Type=Broker,BrokerName=%1,*"); 035 PREDEFINED_OBJNAME_QUERY.setProperty("Connection", "Type=Connection,Connection=%1,*"); 036 PREDEFINED_OBJNAME_QUERY.setProperty("Connector", "Type=Connector,ConnectorName=%1,*"); 037 PREDEFINED_OBJNAME_QUERY.setProperty("NetworkConnector", "Type=NetworkConnector,BrokerName=%1,*"); 038 PREDEFINED_OBJNAME_QUERY.setProperty("Queue", "Type=Queue,Destination=%1,*"); 039 PREDEFINED_OBJNAME_QUERY.setProperty("Topic", "Type=Topic,Destination=%1,*"); 040 }; 041 042 protected String[] helpFile = new String[] { 043 "Task Usage: Main query [query-options]", 044 "Description: Display selected broker component's attributes and statistics.", 045 "", 046 "Query Options:", 047 " -Q<type>=<name> Add to the search list the specific object type matched", 048 " by the defined object identifier.", 049 " -xQ<type>=<name> Remove from the search list the specific object type", 050 " matched by the object identifier.", 051 " --objname <query> Add to the search list objects matched by the query similar", 052 " to the JMX object name format.", 053 " --xobjname <query> Remove from the search list objects matched by the query", 054 " similar to the JMX object name format.", 055 " --view <attr1>,<attr2>,... Select the specific attribute of the object to view.", 056 " By default all attributes will be displayed.", 057 " --jmxurl <url> Set the JMX URL to connect to.", 058 " --jmxuser <user> Set the JMX user used for authenticating.", 059 " --jmxpassword <password> Set the JMX password used for authenticating.", 060 " --jmxlocal Use the local JMX server instead of a remote one.", 061 " --version Display the version information.", 062 " -h,-?,--help Display the query broker help information.", 063 "", "Examples:", 064 " query", 065 " - Print all the attributes of all registered objects queues, topics, connections, etc).", 066 "", 067 " query -QQueue=TEST.FOO", 068 " - Print all the attributes of the queue with destination name TEST.FOO.", 069 "", 070 " query -QTopic=*", 071 " - Print all the attributes of all registered topics.", 072 "", 073 " query --view EnqueueCount,DequeueCount", 074 " - Print the attributes EnqueueCount and DequeueCount of all registered objects.", 075 "", 076 " query -QTopic=* --view EnqueueCount,DequeueCount", 077 " - Print the attributes EnqueueCount and DequeueCount of all registered topics.", 078 "", 079 " query -QTopic=* -QQueue=* --view EnqueueCount,DequeueCount", 080 " - Print the attributes EnqueueCount and DequeueCount of all registered topics and", 081 " queues.", 082 "", 083 " query -QTopic=* -xQTopic=ActiveMQ.Advisory.*", 084 " - Print all attributes of all topics except those that has a name that begins", 085 " with \"ActiveMQ.Advisory\".", 086 "", 087 " query --objname Type=*Connect*,BrokerName=local* -xQNetworkConnector=*", 088 " - Print all attributes of all connectors, connections excluding network connectors", 089 " that belongs to the broker that begins with local.", 090 "", 091 " query -QQueue=* -xQQueue=????", 092 " - Print all attributes of all queues except those that are 4 letters long.", 093 "", 094 }; 095 096 private final List<String> queryAddObjects = new ArrayList<String>(10); 097 private final List<String> querySubObjects = new ArrayList<String>(10); 098 private final Set queryViews = new HashSet(10); 099 100 /** 101 * Queries the mbeans registered in the specified JMX context 102 * 103 * @param tokens - command arguments 104 * @throws Exception 105 */ 106 protected void runTask(List<String> tokens) throws Exception { 107 try { 108 // Query for the mbeans to add 109 List addMBeans = JmxMBeansUtil.queryMBeans(createJmxConnection(), queryAddObjects, queryViews); 110 111 // Query for the mbeans to sub 112 if (querySubObjects.size() > 0) { 113 List subMBeans = JmxMBeansUtil.queryMBeans(createJmxConnection(), querySubObjects, queryViews); 114 addMBeans.removeAll(subMBeans); 115 } 116 117 context.printMBean(JmxMBeansUtil.filterMBeansView(addMBeans, queryViews)); 118 119 } catch (Exception e) { 120 context.printException(new RuntimeException("Failed to execute query task. Reason: " + e)); 121 throw new Exception(e); 122 } 123 } 124 125 /** 126 * Handle the -Q, -xQ, --objname, --xobjname, --view options. 127 * 128 * @param token - option token to handle 129 * @param tokens - succeeding command arguments 130 * @throws Exception 131 */ 132 protected void handleOption(String token, List<String> tokens) throws Exception { 133 // If token is a additive predefined query define option 134 if (token.startsWith("-Q")) { 135 String key = token.substring(2); 136 String value = ""; 137 int pos = key.indexOf("="); 138 if (pos >= 0) { 139 value = key.substring(pos + 1); 140 key = key.substring(0, pos); 141 } 142 143 // If additive query 144 String predefQuery = PREDEFINED_OBJNAME_QUERY.getProperty(key); 145 if (predefQuery == null) { 146 context.printException(new IllegalArgumentException("Unknown query object type: " + key)); 147 return; 148 } 149 String queryStr = JmxMBeansUtil.createQueryString(predefQuery, value); 150 StringTokenizer queryTokens = new StringTokenizer(queryStr, COMMAND_OPTION_DELIMETER); 151 while (queryTokens.hasMoreTokens()) { 152 queryAddObjects.add(queryTokens.nextToken()); 153 } 154 } else if (token.startsWith("-xQ")) { 155 // If token is a substractive predefined query define option 156 String key = token.substring(3); 157 String value = ""; 158 int pos = key.indexOf("="); 159 if (pos >= 0) { 160 value = key.substring(pos + 1); 161 key = key.substring(0, pos); 162 } 163 164 // If subtractive query 165 String predefQuery = PREDEFINED_OBJNAME_QUERY.getProperty(key); 166 if (predefQuery == null) { 167 context.printException(new IllegalArgumentException("Unknown query object type: " + key)); 168 return; 169 } 170 String queryStr = JmxMBeansUtil.createQueryString(predefQuery, value); 171 StringTokenizer queryTokens = new StringTokenizer(queryStr, COMMAND_OPTION_DELIMETER); 172 while (queryTokens.hasMoreTokens()) { 173 querySubObjects.add(queryTokens.nextToken()); 174 } 175 } else if (token.startsWith("--objname")) { 176 // If token is an additive object name query option 177 178 // If no object name query is specified, or next token is a new 179 // option 180 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 181 context.printException(new IllegalArgumentException("Object name query not specified")); 182 return; 183 } 184 185 StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 186 while (queryTokens.hasMoreTokens()) { 187 queryAddObjects.add(queryTokens.nextToken()); 188 } 189 } else if (token.startsWith("--xobjname")) { 190 // If token is a substractive object name query option 191 192 // If no object name query is specified, or next token is a new 193 // option 194 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 195 context.printException(new IllegalArgumentException("Object name query not specified")); 196 return; 197 } 198 199 StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 200 while (queryTokens.hasMoreTokens()) { 201 querySubObjects.add(queryTokens.nextToken()); 202 } 203 } else if (token.startsWith("--view")) { 204 // If token is a view option 205 206 // If no view specified, or next token is a new option 207 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) { 208 context.printException(new IllegalArgumentException("Attributes to view not specified")); 209 return; 210 } 211 212 // Add the attributes to view 213 Enumeration viewTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER); 214 while (viewTokens.hasMoreElements()) { 215 queryViews.add(viewTokens.nextElement()); 216 } 217 } else { 218 // Let super class handle unknown option 219 super.handleOption(token, tokens); 220 } 221 } 222 223 /** 224 * Print the help messages for the browse command 225 */ 226 protected void printHelp() { 227 context.printHelp(helpFile); 228 } 229 230 }