1 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 ; 24 import java.util.Properties ; 25 import java.util.ArrayList ; 26 import java.util.Enumeration ; 27 import java.util.StringTokenizer ; 28 import java.util.Set ; 29 import java.util.HashSet ; 30 31 public class QueryCommand extends AbstractJmxCommand { 32 private static final Properties PREDEFINED_OBJNAME_QUERY = new Properties (); 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 queryAddObjects = new ArrayList (10); 45 private final List querySubObjects = new ArrayList (10); 46 private final Set queryViews = new HashSet (10); 47 48 53 protected void runTask(List tokens) throws Exception { 54 try { 55 List addMBeans = JmxMBeansUtil.queryMBeans(useJmxServiceUrl(), queryAddObjects, queryViews); 57 58 if (querySubObjects.size() > 0) { 60 List subMBeans = JmxMBeansUtil.queryMBeans(useJmxServiceUrl(), querySubObjects, queryViews); 61 addMBeans.removeAll(subMBeans); 62 } 63 64 65 GlobalWriter.printMBean(JmxMBeansUtil.filterMBeansView(addMBeans, queryViews)); 66 67 } catch (Exception e) { 68 GlobalWriter.printException(new RuntimeException ("Failed to execute query task. Reason: " + e)); 69 throw new Exception (e); 70 } 71 } 72 73 79 protected void handleOption(String token, List tokens) throws Exception { 80 if (token.startsWith("-Q")) { 82 String key = token.substring(2); 83 String 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 String predefQuery = PREDEFINED_OBJNAME_QUERY.getProperty(key); 92 if (predefQuery == null) { 93 GlobalWriter.printException(new IllegalArgumentException ("Unknown query object type: " + key)); 94 return; 95 } 96 String queryStr = JmxMBeansUtil.createQueryString(predefQuery, value); 97 StringTokenizer queryTokens = new StringTokenizer (queryStr, COMMAND_OPTION_DELIMETER); 98 while (queryTokens.hasMoreTokens()) { 99 queryAddObjects.add(queryTokens.nextToken()); 100 } 101 } 102 103 else if (token.startsWith("-xQ")) { 105 String key = token.substring(3); 106 String 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 String predefQuery = PREDEFINED_OBJNAME_QUERY.getProperty(key); 115 if (predefQuery == null) { 116 GlobalWriter.printException(new IllegalArgumentException ("Unknown query object type: " + key)); 117 return; 118 } 119 String queryStr = JmxMBeansUtil.createQueryString(predefQuery, value); 120 StringTokenizer queryTokens = new StringTokenizer (queryStr, COMMAND_OPTION_DELIMETER); 121 while (queryTokens.hasMoreTokens()) { 122 querySubObjects.add(queryTokens.nextToken()); 123 } 124 } 125 126 else if (token.startsWith("--objname")) { 128 129 if (tokens.isEmpty() || ((String )tokens.get(0)).startsWith("-")) { 131 GlobalWriter.printException(new IllegalArgumentException ("Object name query not specified")); 132 return; 133 } 134 135 StringTokenizer queryTokens = new StringTokenizer ((String )tokens.remove(0), COMMAND_OPTION_DELIMETER); 136 while (queryTokens.hasMoreTokens()) { 137 queryAddObjects.add(queryTokens.nextToken()); 138 } 139 } 140 141 else if (token.startsWith("--xobjname")) { 143 144 if (tokens.isEmpty() || ((String )tokens.get(0)).startsWith("-")) { 146 GlobalWriter.printException(new IllegalArgumentException ("Object name query not specified")); 147 return; 148 } 149 150 StringTokenizer queryTokens = new StringTokenizer ((String )tokens.remove(0), COMMAND_OPTION_DELIMETER); 151 while (queryTokens.hasMoreTokens()) { 152 querySubObjects.add(queryTokens.nextToken()); 153 } 154 } 155 156 else if (token.startsWith("--view")) { 158 159 if (tokens.isEmpty() || ((String )tokens.get(0)).startsWith("-")) { 161 GlobalWriter.printException(new IllegalArgumentException ("Attributes to view not specified")); 162 return; 163 } 164 165 Enumeration viewTokens = new StringTokenizer ((String )tokens.remove(0), COMMAND_OPTION_DELIMETER); 167 while (viewTokens.hasMoreElements()) { 168 queryViews.add(viewTokens.nextElement()); 169 } 170 } 171 172 else { 174 super.handleOption(token, tokens); 175 } 176 } 177 178 181 protected void printHelp() { 182 GlobalWriter.printHelp(helpFile); 183 } 184 185 protected String [] helpFile = new String [] { 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 |