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.util;
018    
019    import java.util.Iterator;
020    import java.util.List;
021    import java.util.Set;
022    
023    import javax.management.ObjectName;
024    import javax.management.MBeanServerConnection;
025    import javax.management.remote.JMXServiceURL;
026    
027    import org.apache.activemq.console.filter.GroupPropertiesViewFilter;
028    import org.apache.activemq.console.filter.MBeansAttributeQueryFilter;
029    import org.apache.activemq.console.filter.MBeansObjectNameQueryFilter;
030    import org.apache.activemq.console.filter.MBeansRegExQueryFilter;
031    import org.apache.activemq.console.filter.MapTransformFilter;
032    import org.apache.activemq.console.filter.MessagesQueryFilter;
033    import org.apache.activemq.console.filter.PropertiesViewFilter;
034    import org.apache.activemq.console.filter.QueryFilter;
035    import org.apache.activemq.console.filter.StubQueryFilter;
036    import org.apache.activemq.console.filter.WildcardToMsgSelectorTransformFilter;
037    import org.apache.activemq.console.filter.WildcardToRegExTransformFilter;
038    
039    public final class JmxMBeansUtil {
040    
041        private JmxMBeansUtil() {
042        }
043    
044        public static List getAllBrokers(MBeanServerConnection jmxConnection) throws Exception {
045            return (new MBeansObjectNameQueryFilter(jmxConnection)).query("type=Broker,brokerName=*");
046        }
047    
048        public static List getBrokersByName(MBeanServerConnection jmxConnection, String brokerName) throws Exception {
049            return (new MBeansObjectNameQueryFilter(jmxConnection)).query("type=Broker,brokerName=" + brokerName);
050        }
051    
052        public static List getAllBrokers(MBeanServerConnection jmxConnection, Set attributes) throws Exception {
053            return (new MBeansAttributeQueryFilter(jmxConnection, attributes, new MBeansObjectNameQueryFilter(jmxConnection))).query("type=Broker");
054        }
055    
056        public static List getBrokersByName(MBeanServerConnection jmxConnection, String brokerName, Set attributes) throws Exception {
057            return (new MBeansAttributeQueryFilter(jmxConnection, attributes, new MBeansObjectNameQueryFilter(jmxConnection))).query("type=Broker,brokerName=" + brokerName);
058        }
059    
060        public static List queryMBeans(MBeanServerConnection jmxConnection, List queryList) throws Exception {
061            // If there is no query defined get all mbeans
062            if (queryList == null || queryList.size() == 0) {
063                return createMBeansObjectNameQuery(jmxConnection).query("");
064    
065                // Parse through all the query strings
066            } else {
067                return createMBeansObjectNameQuery(jmxConnection).query(queryList);
068            }
069        }
070    
071        public static List queryMBeans(MBeanServerConnection jmxConnection, List queryList, Set attributes) throws Exception {
072            // If there is no query defined get all mbeans
073            if (queryList == null || queryList.size() == 0) {
074                return createMBeansAttributeQuery(jmxConnection, attributes).query("");
075    
076                // Parse through all the query strings
077            } else {
078                return createMBeansAttributeQuery(jmxConnection, attributes).query(queryList);
079            }
080        }
081    
082        public static List queryMBeans(MBeanServerConnection jmxConnection, String queryString) throws Exception {
083            return createMBeansObjectNameQuery(jmxConnection).query(queryString);
084        }
085    
086        public static List queryMBeans(MBeanServerConnection jmxConnection, String queryString, Set attributes) throws Exception {
087            return createMBeansAttributeQuery(jmxConnection, attributes).query(queryString);
088        }
089    
090        public static List filterMBeansView(List mbeans, Set viewFilter) throws Exception {
091            return new PropertiesViewFilter(viewFilter, new MapTransformFilter(new StubQueryFilter(mbeans))).query("");
092        }
093    
094        public static String createQueryString(String query, String param) {
095            return query.replaceAll("%1", param);
096        }
097    
098        public static String createQueryString(String query, List params) {
099            String output = query;
100            int count = 1;
101            for (Iterator i = params.iterator(); i.hasNext();) {
102                output = output.replaceAll("%" + count++, i.next().toString());
103            }
104    
105            return output;
106        }
107    
108        public static QueryFilter createMBeansObjectNameQuery(MBeanServerConnection jmxConnection) {
109            // Let us be able to accept wildcard queries
110            // Use regular expressions to filter the query results
111            // Let us retrieve the mbeans object name specified by the query
112            return new WildcardToRegExTransformFilter(new MBeansRegExQueryFilter(new MBeansObjectNameQueryFilter(jmxConnection)));
113        }
114    
115        public static QueryFilter createMBeansAttributeQuery(MBeanServerConnection jmxConnection, Set attributes) {
116            // Let use be able to accept wildcard queries
117            // Use regular expressions to filter the query result
118            // Retrieve the attributes needed
119            // Retrieve the mbeans object name specified by the query
120            return new MBeansAttributeQueryFilter(jmxConnection, attributes, new MBeansObjectNameQueryFilter(jmxConnection));
121        }
122    
123        public static QueryFilter createMessageQueryFilter(MBeanServerConnection jmxConnection, ObjectName destName) {
124            return new WildcardToMsgSelectorTransformFilter(new MessagesQueryFilter(jmxConnection, destName));
125        }
126    
127        public static List filterMessagesView(List messages, Set groupViews, Set attributeViews) throws Exception {
128            return (new PropertiesViewFilter(attributeViews, new GroupPropertiesViewFilter(groupViews, new MapTransformFilter(new StubQueryFilter(messages))))).query("");
129        }
130    }