KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > util > 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-cli/cli-api/src/java/com/sun/cli/jmx/util/ObjectNameQueryImpl.java,v 1.3 2005/12/25 03:45:56 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:56 $
28  */

29 package com.sun.cli.jmx.util;
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.cli.util.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         void
214     p( Object JavaDoc o )
215     {
216         System.out.println( o.toString() );
217     }
218
219         Set JavaDoc
220     matchEither( Matcher matcher, Set JavaDoc startingSet, String JavaDoc [] regexNames, String JavaDoc [] regexValues )
221     {
222         if ( regexNames == null && regexValues == null )
223         {
224             // both null => matches entire original set
225
return( startingSet );
226         }
227         
228         final Iterator JavaDoc iter = startingSet.iterator();
229         final Set JavaDoc results = new HashSet JavaDoc();
230         
231         int numMatches = 0;
232         if ( regexNames != null )
233         {
234             numMatches = regexNames.length;
235         }
236         else
237         {
238             numMatches = regexValues.length;
239         }
240
241         final Pattern [] namePatterns = createPatterns( regexNames, numMatches );
242         final Pattern [] valuePatterns = createPatterns( regexValues, numMatches );
243         
244         while ( iter.hasNext() )
245         {
246             final ObjectName JavaDoc name = (ObjectName JavaDoc)iter.next();
247             
248             if ( matcher.match( name, namePatterns, valuePatterns ) )
249             {
250                 results.add( name );
251             }
252         }
253
254         return( results );
255     }
256
257         public Set JavaDoc
258     matchAll( Set JavaDoc startingSet, String JavaDoc [] regexNames, String JavaDoc [] regexValues )
259     {
260         return( matchEither( new MatchAllMatcher(), startingSet, regexNames, regexValues ) );
261     }
262     
263
264                 
265         public Set JavaDoc
266     matchAny( Set JavaDoc startingSet, String JavaDoc [] regexNames, String JavaDoc [] regexValues )
267     {
268         return( matchEither( new MatchAnyMatcher(), startingSet, regexNames, regexValues ) );
269     }
270 }
271
272
273
274
275
276
277
Popular Tags