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.filter; 018 019 import java.io.IOException; 020 import java.util.ArrayList; 021 import java.util.Iterator; 022 import java.util.List; 023 024 import javax.management.MBeanServerConnection; 025 import javax.management.MalformedObjectNameException; 026 import javax.management.ObjectName; 027 import javax.management.QueryExp; 028 029 public class MBeansObjectNameQueryFilter extends AbstractQueryFilter { 030 031 public static final String DEFAULT_JMX_DOMAIN = "org.apache.activemq"; 032 public static final String QUERY_EXP_PREFIX = "MBeans.QueryExp."; 033 034 private MBeanServerConnection jmxConnection; 035 036 /** 037 * Creates an mbeans object name query filter that will query on the given 038 * JMX connection 039 * 040 * @param jmxConnection - JMX connection to use 041 */ 042 public MBeansObjectNameQueryFilter(MBeanServerConnection jmxConnection) { 043 super(null); 044 this.jmxConnection = jmxConnection; 045 } 046 047 /** 048 * Queries the JMX service using a mapping of keys and values to construct 049 * the object name 050 * 051 * @param queries - mapping of keys and values 052 * @return collection of ObjectInstance that matches the query 053 * @throws MalformedObjectNameException - if the given string is an invalid 054 * object name 055 * @throws IOException - if there is a problem querying the JMX context 056 */ 057 public List query(List queries) throws MalformedObjectNameException, IOException { 058 // Query all mbeans 059 if (queries == null || queries.isEmpty()) { 060 return queryMBeans(new ObjectName(DEFAULT_JMX_DOMAIN + ":*"), null); 061 } 062 063 // Constructs object name query 064 String objNameQuery = ""; 065 String queryExp = ""; 066 for (Iterator i = queries.iterator(); i.hasNext();) { 067 String key = (String)i.next(); 068 String val = ""; 069 int pos = key.indexOf("="); 070 if (pos >= 0) { 071 val = key.substring(pos + 1); 072 key = key.substring(0, pos); 073 } 074 075 if (val.startsWith(QUERY_EXP_PREFIX)) { 076 // do nothing as of the moment 077 } else if (!key.equals("") && !val.equals("")) { 078 objNameQuery = objNameQuery + key + "=" + val + ","; 079 } 080 } 081 082 // Append * to object name 083 objNameQuery = objNameQuery + "*"; 084 085 return queryMBeans(new ObjectName(DEFAULT_JMX_DOMAIN + ":" + objNameQuery), queryExp); 086 } 087 088 /** 089 * Advance query that enables you to specify both the object name and the 090 * query expression to use. Note: Query expression is currently unsupported. 091 * 092 * @param objName - object name to use for query 093 * @param queryExpStr - query expression string 094 * @return set of mbeans that matches the query 095 * @throws IOException - if there is a problem querying the JMX context 096 */ 097 protected List queryMBeans(ObjectName objName, String queryExpStr) throws IOException { 098 QueryExp queryExp = createQueryExp(queryExpStr); 099 100 // Convert mbeans set to list to make it standard throughout the query 101 // filter 102 List mbeans = new ArrayList(jmxConnection.queryMBeans(objName, queryExp)); 103 104 return mbeans; 105 } 106 107 /** 108 * Creates a query expression based on the query expression string Note: 109 * currently unsupported 110 * 111 * @param queryExpStr - query expression string 112 * @return the created query expression 113 */ 114 protected QueryExp createQueryExp(String queryExpStr) { 115 // Currently unsupported 116 return null; 117 } 118 }