KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectInstance JavaDoc;
21 import javax.management.ObjectName JavaDoc;
22 import javax.management.AttributeList JavaDoc;
23 import javax.management.Attribute JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 public class MBeansRegExQueryFilter extends RegExQueryFilter {
30     /**
31      * Creates a regular expression query that is able to match the values of specific mbeans
32      * @param next - next query filter
33      */

34     public MBeansRegExQueryFilter(QueryFilter next) {
35         super(next);
36     }
37
38     /**
39      * Try to match the object data using the regular expression map. The regex map contains a key-value mapping of an attribute
40      * key to a regular expression the value of the key should match. The basic rule of matching is that the data must contain
41      * a property key that is included in the regex map, and that the value of the property key should match the regex specified.
42      * @param data - object data to match
43      * @param regex - regex map
44      * @return true if the data matches the regex map specified
45      * @throws Exception
46      */

47     protected boolean matches(Object JavaDoc data, Map JavaDoc regex) throws Exception JavaDoc {
48         // TODO why not just use instanceof?
49

50         // Use reflection to determine where the object should go
51
try {
52             Method JavaDoc method = this.getClass().getDeclaredMethod("matches", new Class JavaDoc[] {data.getClass(), Map JavaDoc.class});
53             return ((Boolean JavaDoc)method.invoke(this, new Object JavaDoc[] {data, regex})).booleanValue();
54         } catch (NoSuchMethodException JavaDoc e) {
55             return false;
56         }
57     }
58
59     /**
60      * Try to match the object instance using the regular expression map
61      * @param data - object instance to match
62      * @param regex - regex map
63      * @return true if the object instance matches the regex map
64      */

65     protected boolean matches(ObjectInstance JavaDoc data, Map JavaDoc regex) {
66         return matches(data.getObjectName(), regex);
67     }
68
69     /**
70      * Try to match the object name instance using the regular expression map
71      * @param data - object name to match
72      * @param regex - regex map
73      * @return true if the object name matches the regex map
74      */

75     protected boolean matches(ObjectName JavaDoc data, Map JavaDoc regex) {
76         for (Iterator JavaDoc i=regex.keySet().iterator(); i.hasNext();) {
77             String JavaDoc key = (String JavaDoc)i.next();
78             String JavaDoc target = data.getKeyProperty(key);
79
80             // Try to match the value of the property of the object name
81
if (target != null && !((Pattern JavaDoc)regex.get(key)).matcher(target).matches()) {
82                 return false;
83             }
84         }
85         return true;
86     }
87
88     /**
89      * Try to match the attribute list using the regular expression map
90      * @param data - attribute list to match
91      * @param regex - regex map
92      * @return true if the attribute list matches the regex map
93      */

94     protected boolean matches(AttributeList JavaDoc data, Map JavaDoc regex) {
95         for (Iterator JavaDoc i=regex.keySet().iterator(); i.hasNext();) {
96             String JavaDoc key = (String JavaDoc)i.next();
97
98             // Try to match each regex to the attributes of the mbean including its ObjectName
99
for (Iterator JavaDoc j=data.iterator(); j.hasNext();) {
100                 Attribute JavaDoc attrib = (Attribute JavaDoc)j.next();
101
102                 // Try to match to the properties of the ObjectName
103
if (attrib.getName().equals(MBeansAttributeQueryFilter.KEY_OBJECT_NAME_ATTRIBUTE)) {
104                     String JavaDoc target = ((ObjectName JavaDoc)attrib.getValue()).getKeyProperty(key);
105
106                     if (target == null || !((Pattern JavaDoc)regex.get(key)).matcher(target).matches()) {
107                         return false;
108                     } else {
109                         // If match skip to the next regex
110
break;
111                     }
112
113                 // Try to match to the mbean attributes
114
} else if (attrib.getName().equals(key)) {
115                     if (!((Pattern JavaDoc)regex.get(key)).matcher(attrib.getValue().toString()).matches()) {
116                         return false;
117                     } else {
118                         // If match skip to the next regex
119
break;
120                     }
121
122                 // If mbean does not contain the specified attribute
123
} else {
124                     return false;
125                 }
126             }
127         }
128         return true;
129     }
130 }
131
Popular Tags