KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > jmx > ObjectNameQueryImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin-core/mbeanapi/src/java/com/sun/appserv/management/util/jmx/ObjectNameQueryImpl.java,v 1.4 2006/03/09 20:30:32 llc Exp $
26  * $Revision: 1.4 $
27  * $Date: 2006/03/09 20:30:32 $
28  */

29 package com.sun.appserv.management.util.jmx;
30
31 import java.util.Set JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.regex.*;
34 import java.util.Hashtable JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38
39 import javax.management.MBeanServerConnection JavaDoc;
40 import javax.management.ObjectName JavaDoc;
41 import javax.management.MalformedObjectNameException JavaDoc;
42
43 import com.sun.appserv.management.util.misc.EnumerationIterator;
44
45 public class ObjectNameQueryImpl implements ObjectNameQuery
46 {
47         public
48     ObjectNameQueryImpl()
49     {
50     }
51     
52     
53     /**
54         Return true if one (or more) of the properties match the regular expressions
55         for both name and value. Return false if no property/value combinations match.
56         
57         A null pattern matches anything.
58      */

59         boolean
60     match( Hashtable JavaDoc properties, Pattern propertyPattern, Pattern valuePattern )
61     {
62         final Iterator JavaDoc keys = new EnumerationIterator( properties.keys() );
63         boolean matches = false;
64         
65         while ( keys.hasNext() )
66         {
67             final String JavaDoc key = (String JavaDoc)keys.next();
68             
69             if ( propertyPattern == null || propertyPattern.matcher( key ).matches() )
70             {
71                 if ( valuePattern == null )
72                 {
73                     matches = true;
74                     break;
75                 }
76
77                 // see if value matches
78
final String JavaDoc value = (String JavaDoc)properties.get( key );
79                 
80                 if ( valuePattern.matcher( value ).matches() )
81                 {
82                     matches = true;
83                     break;
84                 }
85             }
86         }
87         
88         return( matches );
89     }
90     
91     /**
92         Match all property/value expressions against the ObjectName.
93         
94         Return true if for each property/value regular expression pair there is at least one
95         property within the ObjectName whose property name and value match their respective
96         patterns.
97         
98         A null regex indicates "match anything".
99      */

100         boolean
101     matchAll( ObjectName JavaDoc name,
102                 Pattern [] propertyPatterns,
103                 Pattern [] valuePatterns )
104     {
105         boolean matches = true;
106         
107         final Hashtable JavaDoc properties = name.getKeyPropertyList();
108         
109         for( int i = 0; i < propertyPatterns.length; ++i )
110         {
111             if ( ! match( properties, propertyPatterns[ i ], valuePatterns[ i ] ) )
112             {
113                 matches = false;
114                 break;
115             }
116         }
117         
118         return( matches );
119     }
120     
121     
122     /**
123         Match all property/value expressions against the ObjectName.
124         
125         Return true if there is at least one property/value regular expression pair that
126         matches a property/value pair within the ObjectName.
127         
128         A null regex indicates "match anything".
129      */

130         boolean
131     matchAny( ObjectName JavaDoc name,
132                 Pattern [] propertyPatterns,
133                 Pattern [] valuePatterns )
134     {
135         boolean matches = false;
136         
137         final Hashtable JavaDoc properties = name.getKeyPropertyList();
138         
139         for( int i = 0; i < propertyPatterns.length; ++i )
140         {
141             if ( match( properties, propertyPatterns[ i ], valuePatterns[ i ] ) )
142             {
143                 matches = true;
144                 break;
145             }
146         }
147         
148         return( matches );
149     }
150     
151     
152     
153         Pattern []
154     createPatterns( final String JavaDoc [] patternStrings, int numItems )
155     {
156         final Pattern [] patterns = new Pattern [ numItems ];
157         
158         if ( patternStrings == null )
159         {
160             for( int i = 0; i < numItems; ++i )
161             {
162                 patterns[ i ] = null;
163             }
164             
165             return( patterns );
166         }
167             
168         
169         for( int i = 0; i < numItems; ++i )
170         {
171             // consider null to match anything
172

173             if ( patternStrings[ i ] == null )
174             {
175                 patterns[ i ] = null;
176             }
177             else
178             {
179                 patterns[ i ] = Pattern.compile( patternStrings[ i ] );
180             }
181         }
182         
183         return( patterns );
184     }
185     
186     private interface Matcher
187     {
188         boolean match( ObjectName JavaDoc name, Pattern [] names, Pattern [] values );
189     }
190     
191     private class MatchAnyMatcher implements Matcher
192     {
193         public MatchAnyMatcher() {}
194         
195             public boolean
196         match( ObjectName JavaDoc name, Pattern [] names, Pattern [] values )
197         {
198             return( matchAny( name, names, values ) );
199         }
200     }
201     
202     private class MatchAllMatcher implements Matcher
203     {
204         public MatchAllMatcher() {}
205         
206             public boolean
207         match( ObjectName JavaDoc name, Pattern [] names, Pattern [] values )
208         {
209             return( matchAll( name, names, values ) );
210         }
211     }
212     
213
214         Set JavaDoc<ObjectName JavaDoc>
215     matchEither(
216         Matcher matcher,
217         Set JavaDoc<ObjectName JavaDoc> startingSet,
218         String JavaDoc [] regexNames,
219         String JavaDoc [] regexValues )
220     {
221         if ( regexNames == null && regexValues == null )
222         {
223             // both null => matches entire original set
224
return( startingSet );
225         }
226         
227         final Set JavaDoc<ObjectName JavaDoc> results = new HashSet JavaDoc<ObjectName JavaDoc>();
228         
229         int numMatches = 0;
230         if ( regexNames != null )
231         {
232             numMatches = regexNames.length;
233         }
234         else
235         {
236             numMatches = regexValues.length;
237         }
238
239         final Pattern [] namePatterns = createPatterns( regexNames, numMatches );
240         final Pattern [] valuePatterns = createPatterns( regexValues, numMatches );
241         
242         for( final ObjectName JavaDoc name : startingSet )
243         {
244             if ( matcher.match( name, namePatterns, valuePatterns ) )
245             {
246                 results.add( name );
247             }
248         }
249
250         return( results );
251     }
252
253         public Set JavaDoc<ObjectName JavaDoc>
254     matchAll( Set JavaDoc<ObjectName JavaDoc> startingSet, String JavaDoc [] regexNames, String JavaDoc [] regexValues )
255     {
256         return( matchEither( new MatchAllMatcher(), startingSet, regexNames, regexValues ) );
257     }
258     
259
260                 
261         public Set JavaDoc<ObjectName JavaDoc>
262     matchAny( Set JavaDoc<ObjectName JavaDoc> startingSet, String JavaDoc [] regexNames, String JavaDoc [] regexValues )
263     {
264         return( matchEither( new MatchAnyMatcher(), startingSet, regexNames, regexValues ) );
265     }
266 }
267
268
269
270
271
272
273
Popular Tags