KickJava   Java API By Example, From Geeks To Geeks.

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


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.JMXConnectorFactory JavaDoc;
22 import javax.management.remote.JMXConnector JavaDoc;
23 import javax.management.ObjectInstance JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25 import javax.management.MBeanServerConnection JavaDoc;
26 import javax.management.ReflectionException JavaDoc;
27 import javax.management.InstanceNotFoundException JavaDoc;
28 import javax.management.AttributeList JavaDoc;
29 import javax.management.Attribute JavaDoc;
30 import javax.management.MBeanAttributeInfo JavaDoc;
31 import javax.management.IntrospectionException JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.List JavaDoc;
37 import java.io.IOException JavaDoc;
38
39 public class MBeansAttributeQueryFilter extends AbstractQueryFilter {
40     public static final String JavaDoc KEY_OBJECT_NAME_ATTRIBUTE = "Attribute:ObjectName:";
41
42     private JMXServiceURL JavaDoc jmxServiceUrl;
43     private Set JavaDoc attribView;
44
45     /**
46      * Create an mbean attributes query filter that is able to select specific mbean attributes based on the object name to get.
47      * @param jmxServiceUrl - JMX service url to connect to.
48      * @param attribView - the attributes to extract
49      * @param next - the next query filter
50      */

51     public MBeansAttributeQueryFilter(JMXServiceURL JavaDoc jmxServiceUrl, Set JavaDoc attribView, MBeansObjectNameQueryFilter next) {
52         super(next);
53         this.jmxServiceUrl = jmxServiceUrl;
54         this.attribView = attribView;
55     }
56
57     /**
58      * Filter the query by retrieving the attributes specified, this will modify the collection to a list of AttributeList
59      * @param queries - query list
60      * @return List of AttributeList, which includes the ObjectName, which has a key of MBeansAttributeQueryFilter.KEY_OBJECT_NAME_ATTRIBUTE
61      * @throws Exception
62      */

63     public List JavaDoc query(List JavaDoc queries) throws Exception JavaDoc {
64         return getMBeanAttributesCollection(next.query(queries));
65     }
66
67     /**
68      * Retrieve the specified attributes of the mbean
69      * @param result - collection of ObjectInstances and/or ObjectNames
70      * @return List of AttributeList
71      * @throws IOException
72      * @throws ReflectionException
73      * @throws InstanceNotFoundException
74      * @throws NoSuchMethodException
75      */

76     protected List JavaDoc getMBeanAttributesCollection(Collection JavaDoc result) throws IOException JavaDoc, ReflectionException JavaDoc, InstanceNotFoundException JavaDoc, NoSuchMethodException JavaDoc, IntrospectionException JavaDoc {
77         List JavaDoc mbeansCollection = new ArrayList JavaDoc();
78
79         for (Iterator JavaDoc i=result.iterator(); i.hasNext();) {
80             Object JavaDoc mbean = i.next();
81             if (mbean instanceof ObjectInstance JavaDoc) {
82                 mbeansCollection.add(getMBeanAttributes(((ObjectInstance JavaDoc)mbean).getObjectName(), attribView));
83             } else if (mbean instanceof ObjectName JavaDoc) {
84                 mbeansCollection.add(getMBeanAttributes((ObjectName JavaDoc)mbean, attribView));
85             } else {
86                 throw new NoSuchMethodException JavaDoc("Cannot get the mbean attributes for class: " + mbean.getClass().getName());
87             }
88         }
89
90         return mbeansCollection;
91     }
92
93     /**
94      * Retrieve the specified attributes of the mbean
95      * @param obj - mbean ObjectInstance
96      * @param attrView - list of attributes to retrieve
97      * @return AttributeList for the mbean
98      * @throws ReflectionException
99      * @throws InstanceNotFoundException
100      * @throws IOException
101      */

102     protected AttributeList JavaDoc getMBeanAttributes(ObjectInstance JavaDoc obj, Set JavaDoc attrView) throws ReflectionException JavaDoc, InstanceNotFoundException JavaDoc, IOException JavaDoc, IntrospectionException JavaDoc {
103         return getMBeanAttributes(obj.getObjectName(), attrView);
104     }
105
106     /**
107      * Retrieve the specified attributes of the mbean
108      * @param objName - mbean ObjectName
109      * @param attrView - list of attributes to retrieve
110      * @return AttributeList for the mbean
111      * @throws IOException
112      * @throws ReflectionException
113      * @throws InstanceNotFoundException
114      */

115     protected AttributeList JavaDoc getMBeanAttributes(ObjectName JavaDoc objName, Set JavaDoc attrView) throws IOException JavaDoc, ReflectionException JavaDoc, InstanceNotFoundException JavaDoc, IntrospectionException JavaDoc {
116         JMXConnector JavaDoc jmxConnector = JMXConnectorFactory.connect(jmxServiceUrl);
117         MBeanServerConnection JavaDoc server = jmxConnector.getMBeanServerConnection();
118
119         // If no attribute view specified, get all attributes
120
String JavaDoc[] attribs;
121         if (attrView == null || attrView.isEmpty()) {
122             MBeanAttributeInfo JavaDoc[] infos = server.getMBeanInfo(objName).getAttributes();
123             attribs = new String JavaDoc[infos.length];
124
125             for (int i=0; i<infos.length; i++) {
126                 if (infos[i].isReadable()) {
127                     attribs[i] = infos[i].getName();
128                 }
129             }
130
131         // Get selected attributes
132
} else {
133
134             attribs = new String JavaDoc[attrView.size()];
135             int count = 0;
136             for (Iterator JavaDoc i=attrView.iterator(); i.hasNext();) {
137                 attribs[count++] = (String JavaDoc)i.next();
138             }
139         }
140
141         AttributeList JavaDoc attribList = server.getAttributes(objName, attribs);
142
143         jmxConnector.close();
144
145         attribList.add(0, new Attribute JavaDoc(KEY_OBJECT_NAME_ATTRIBUTE, objName));
146
147         return attribList;
148     }
149 }
150
Popular Tags