KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > filter > MBeansObjectNameQueryFilter


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.filter;
19
20 import javax.management.remote.JMXServiceURL JavaDoc;
21 import javax.management.remote.JMXConnector JavaDoc;
22 import javax.management.remote.JMXConnectorFactory JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import javax.management.MalformedObjectNameException JavaDoc;
25 import javax.management.MBeanServerConnection JavaDoc;
26 import javax.management.QueryExp JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 public class MBeansObjectNameQueryFilter extends AbstractQueryFilter {
34
35     public static final String JavaDoc DEFAULT_JMX_DOMAIN = "org.apache.activemq";
36     public static final String JavaDoc QUERY_EXP_PREFIX = "MBeans.QueryExp.";
37
38     private JMXServiceURL JavaDoc jmxServiceUrl;
39
40     /**
41      * Creates an mbeans object name query filter that will query on the given JMX Service URL
42      * @param jmxUrl - JMX service URL to connect to
43      * @throws MalformedURLException
44      */

45     public MBeansObjectNameQueryFilter(String JavaDoc jmxUrl) throws MalformedURLException JavaDoc {
46         this(new JMXServiceURL JavaDoc(jmxUrl));
47     }
48
49     /**
50      * Creates an mbeans objecet name query filter that will query on the given JMX Service URL
51      * @param jmxUrl - JMX service URL to connect to
52      */

53     public MBeansObjectNameQueryFilter(JMXServiceURL JavaDoc jmxUrl) {
54         super(null);
55         this.jmxServiceUrl = jmxUrl;
56     }
57
58     /**
59      * Queries the JMX service using a mapping of keys and values to construct the object name
60      * @param queries - mapping of keys and values
61      * @return collection of ObjectInstance that matches the query
62      * @throws MalformedObjectNameException - if the given string is an invalid object name
63      * @throws IOException - if there is a problem querying the JMX context
64      */

65     public List JavaDoc query(List JavaDoc queries) throws MalformedObjectNameException JavaDoc, IOException JavaDoc {
66
67         // Query all mbeans
68
if (queries == null || queries.isEmpty()) {
69             return queryMBeans(new ObjectName JavaDoc(DEFAULT_JMX_DOMAIN + ":*"), null);
70         }
71
72         // Constructs object name query
73
String JavaDoc objNameQuery = "";
74         String JavaDoc queryExp = "";
75         for (Iterator JavaDoc i=queries.iterator(); i.hasNext();) {
76             String JavaDoc key = (String JavaDoc)i.next();
77             String JavaDoc val = "";
78             int pos = key.indexOf("=");
79             if (pos >= 0) {
80                 val = key.substring(pos + 1);
81                 key = key.substring(0, pos);
82             }
83
84             if (val.startsWith(QUERY_EXP_PREFIX)) {
85                 // do nothing as of the moment
86
} else if (key != "" && val != "") {
87                 objNameQuery = objNameQuery + key + "=" + val + ",";
88             }
89         }
90
91         // Append * to object name
92
objNameQuery = objNameQuery + "*";
93
94         return queryMBeans(new ObjectName JavaDoc(DEFAULT_JMX_DOMAIN + ":" + objNameQuery), queryExp);
95     }
96
97     /**
98      * Advance query that enables you to specify both the object name and the query expression to use.
99      * Note: Query expression is currently unsupported.
100      * @param objName - object name to use for query
101      * @param queryExpStr - query expression string
102      * @return set of mbeans that matches the query
103      * @throws IOException - if there is a problem querying the JMX context
104      */

105     protected List JavaDoc queryMBeans(ObjectName JavaDoc objName, String JavaDoc queryExpStr) throws IOException JavaDoc {
106         JMXConnector JavaDoc jmxConn = createJmxConnector();
107         MBeanServerConnection JavaDoc server = jmxConn.getMBeanServerConnection();
108
109         QueryExp JavaDoc queryExp = createQueryExp(queryExpStr);
110
111         // Convert mbeans set to list to make it standard throughout the query filter
112
List JavaDoc mbeans = new ArrayList JavaDoc(server.queryMBeans(objName, queryExp));
113
114         jmxConn.close();
115
116         return mbeans;
117     }
118
119     /**
120      * Get the JMX service URL the query is connecting to.
121      * @return JMX service URL
122      */

123     public JMXServiceURL JavaDoc getJmxServiceUrl() {
124         return jmxServiceUrl;
125     }
126
127     /**
128      * Sets the JMX service URL the query is going to connect to.
129      * @param jmxServiceUrl - new JMX service URL
130      */

131     public void setJmxServiceUrl(JMXServiceURL JavaDoc jmxServiceUrl) {
132         this.jmxServiceUrl = jmxServiceUrl;
133     }
134
135     /**
136      * Sets the JMX service URL the query is going to connect to.
137      * @param jmxServiceUrl - new JMX service URL
138      */

139     public void setJmxServiceUrl(String JavaDoc jmxServiceUrl) throws MalformedURLException JavaDoc {
140         setJmxServiceUrl(new JMXServiceURL JavaDoc(jmxServiceUrl));
141     }
142
143     /**
144      * Creates a JMX connector
145      * @return JMX connector
146      * @throws IOException
147      */

148     protected JMXConnector JavaDoc createJmxConnector() throws IOException JavaDoc {
149         return JMXConnectorFactory.connect(getJmxServiceUrl());
150     }
151
152     /**
153      * Creates a query expression based on the query expression string
154      * Note: currently unsupported
155      * @param queryExpStr - query expression string
156      * @return the created query expression
157      */

158     protected QueryExp JavaDoc createQueryExp(String JavaDoc queryExpStr) {
159         // Currently unsupported
160
return null;
161     }
162 }
163
Popular Tags