KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > util > JmxMBeansUtil


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.console.util;
19
20 import org.apache.activemq.console.filter.*;
21
22 import javax.management.remote.JMXServiceURL JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 public class JmxMBeansUtil {
29
30     public static List JavaDoc getAllBrokers(JMXServiceURL JavaDoc jmxUrl) throws Exception JavaDoc {
31         return (new MBeansObjectNameQueryFilter(jmxUrl)).query("Type=Broker");
32     }
33
34     public static List JavaDoc getBrokersByName(JMXServiceURL JavaDoc jmxUrl, String JavaDoc brokerName) throws Exception JavaDoc {
35         return (new MBeansObjectNameQueryFilter(jmxUrl)).query("Type=Broker,BrokerName=" + brokerName);
36     }
37
38     public static List JavaDoc getAllBrokers(JMXServiceURL JavaDoc jmxUrl, Set JavaDoc attributes) throws Exception JavaDoc {
39         return (new MBeansAttributeQueryFilter(jmxUrl, attributes, new MBeansObjectNameQueryFilter(jmxUrl))).query("Type=Broker");
40     }
41
42     public static List JavaDoc getBrokersByName(JMXServiceURL JavaDoc jmxUrl, String JavaDoc brokerName, Set JavaDoc attributes) throws Exception JavaDoc {
43         return (new MBeansAttributeQueryFilter(jmxUrl, attributes, new MBeansObjectNameQueryFilter(jmxUrl))).query("Type=Broker,BrokerName=" + brokerName);
44     }
45
46     public static List JavaDoc queryMBeans(JMXServiceURL JavaDoc jmxUrl, List JavaDoc queryList) throws Exception JavaDoc {
47         // If there is no query defined get all mbeans
48
if (queryList==null || queryList.size()==0) {
49             return createMBeansObjectNameQuery(jmxUrl).query("");
50
51         // Parse through all the query strings
52
} else {
53             return createMBeansObjectNameQuery(jmxUrl).query(queryList);
54         }
55     }
56
57     public static List JavaDoc queryMBeans(JMXServiceURL JavaDoc jmxUrl, List JavaDoc queryList, Set JavaDoc attributes) throws Exception JavaDoc {
58         // If there is no query defined get all mbeans
59
if (queryList==null || queryList.size()==0) {
60             return createMBeansAttributeQuery(jmxUrl, attributes).query("");
61
62         // Parse through all the query strings
63
} else {
64             return createMBeansAttributeQuery(jmxUrl, attributes).query(queryList);
65         }
66     }
67
68     public static List JavaDoc queryMBeans(JMXServiceURL JavaDoc jmxUrl, String JavaDoc queryString) throws Exception JavaDoc {
69         return createMBeansObjectNameQuery(jmxUrl).query(queryString);
70     }
71
72     public static List JavaDoc queryMBeans(JMXServiceURL JavaDoc jmxUrl, String JavaDoc queryString, Set JavaDoc attributes) throws Exception JavaDoc {
73         return createMBeansAttributeQuery(jmxUrl, attributes).query(queryString);
74     }
75
76     public static List JavaDoc filterMBeansView(List JavaDoc mbeans, Set JavaDoc viewFilter) throws Exception JavaDoc {
77         return (new PropertiesViewFilter(viewFilter, new MapTransformFilter(new StubQueryFilter(mbeans))).query(""));
78     }
79
80     public static String JavaDoc createQueryString(String JavaDoc query, String JavaDoc param) {
81         return query.replaceAll("%1", param);
82     }
83
84     public static String JavaDoc createQueryString(String JavaDoc query, List JavaDoc params) {
85
86         int count = 1;
87         for (Iterator JavaDoc i=params.iterator();i.hasNext();) {
88             query.replaceAll("%" + count++, i.next().toString());
89         }
90
91         return query;
92     }
93
94     public static QueryFilter createMBeansObjectNameQuery(JMXServiceURL JavaDoc jmxUrl) {
95         return new WildcardToRegExTransformFilter( // Let us be able to accept wildcard queries
96
new MBeansRegExQueryFilter( // Use regular expressions to filter the query results
97
new MBeansObjectNameQueryFilter(jmxUrl) // Let us retrieve the mbeans object name specified by the query
98
)
99         );
100     }
101
102     public static QueryFilter createMBeansAttributeQuery(JMXServiceURL JavaDoc jmxUrl, Set JavaDoc attributes) {
103         return new WildcardToRegExTransformFilter( // Let use be able to accept wildcard queries
104
new MBeansRegExQueryFilter( // Use regular expressions to filter the query result
105
new MBeansAttributeQueryFilter(jmxUrl, attributes, // Retrieve the attributes needed
106
new MBeansObjectNameQueryFilter(jmxUrl) // Retrieve the mbeans object name specified by the query
107
)
108             )
109         );
110     }
111
112     public static QueryFilter createMessageQueryFilter(JMXServiceURL JavaDoc jmxUrl, ObjectName JavaDoc destName) {
113         return new WildcardToMsgSelectorTransformFilter(
114             new MessagesQueryFilter(jmxUrl, destName)
115         );
116     }
117
118     public static List JavaDoc filterMessagesView(List JavaDoc messages, Set JavaDoc groupViews, Set JavaDoc attributeViews) throws Exception JavaDoc {
119         return (new PropertiesViewFilter(attributeViews,
120             new GroupPropertiesViewFilter(groupViews,
121                 new MapTransformFilter(
122                     new StubQueryFilter(messages)
123                 )
124             )
125         )).query("");
126     }
127 }
128
Popular Tags