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 */ 017package org.apache.activemq.console.filter; 018 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.Iterator; 022import java.util.List; 023 024import javax.management.MBeanServerConnection; 025import javax.management.MalformedObjectNameException; 026import javax.management.ObjectName; 027import javax.management.QueryExp; 028 029public 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 String delimiter = ""; 067 for (Iterator i = queries.iterator(); i.hasNext();) { 068 String key = (String)i.next(); 069 String val = ""; 070 int pos = key.indexOf("="); 071 if (pos >= 0) { 072 val = key.substring(pos + 1); 073 key = key.substring(0, pos); 074 } else { 075 objNameQuery += delimiter + key; 076 } 077 078 if (val.startsWith(QUERY_EXP_PREFIX)) { 079 // do nothing as of the moment 080 } else if (!key.equals("") && !val.equals("")) { 081 objNameQuery += delimiter + key + "=" + val; 082 delimiter = ","; 083 } 084 } 085 086 return queryMBeans(new ObjectName(DEFAULT_JMX_DOMAIN + ":" + objNameQuery), queryExp); 087 } 088 089 /** 090 * Advance query that enables you to specify both the object name and the 091 * query expression to use. Note: Query expression is currently unsupported. 092 * 093 * @param objName - object name to use for query 094 * @param queryExpStr - query expression string 095 * @return set of mbeans that matches the query 096 * @throws IOException - if there is a problem querying the JMX context 097 */ 098 protected List queryMBeans(ObjectName objName, String queryExpStr) throws IOException { 099 QueryExp queryExp = createQueryExp(queryExpStr); 100 101 // Convert mbeans set to list to make it standard throughout the query 102 // filter 103 List mbeans = new ArrayList(jmxConnection.queryMBeans(objName, queryExp)); 104 105 return mbeans; 106 } 107 108 /** 109 * Creates a query expression based on the query expression string Note: 110 * currently unsupported 111 * 112 * @param queryExpStr - query expression string 113 * @return the created query expression 114 */ 115 protected QueryExp createQueryExp(String queryExpStr) { 116 // Currently unsupported 117 return null; 118 } 119}