1 23 24 29 package com.sun.cli.jmx.util; 30 31 import java.util.Set ; 32 import java.util.HashSet ; 33 import java.util.regex.*; 34 import java.util.Hashtable ; 35 import java.util.Enumeration ; 36 import java.util.Iterator ; 37 38 39 import javax.management.MBeanServerConnection ; 40 import javax.management.ObjectName ; 41 import javax.management.MalformedObjectNameException ; 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 59 boolean 60 match( Hashtable properties, Pattern propertyPattern, Pattern valuePattern ) 61 { 62 final Iterator keys = new EnumerationIterator( properties.keys() ); 63 boolean matches = false; 64 65 while ( keys.hasNext() ) 66 { 67 final String key = (String )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 final String value = (String )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 100 boolean 101 matchAll( ObjectName name, 102 Pattern [] propertyPatterns, 103 Pattern [] valuePatterns ) 104 { 105 boolean matches = true; 106 107 final Hashtable 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 130 boolean 131 matchAny( ObjectName name, 132 Pattern [] propertyPatterns, 133 Pattern [] valuePatterns ) 134 { 135 boolean matches = false; 136 137 final Hashtable 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 [] 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 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 name, Pattern [] names, Pattern [] values ); 189 } 190 191 private class MatchAnyMatcher implements Matcher 192 { 193 public MatchAnyMatcher() {} 194 195 public boolean 196 match( ObjectName 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 name, Pattern [] names, Pattern [] values ) 208 { 209 return( matchAll( name, names, values ) ); 210 } 211 } 212 213 void 214 p( Object o ) 215 { 216 System.out.println( o.toString() ); 217 } 218 219 Set 220 matchEither( Matcher matcher, Set startingSet, String [] regexNames, String [] regexValues ) 221 { 222 if ( regexNames == null && regexValues == null ) 223 { 224 return( startingSet ); 226 } 227 228 final Iterator iter = startingSet.iterator(); 229 final Set results = new HashSet (); 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 name = (ObjectName )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 258 matchAll( Set startingSet, String [] regexNames, String [] regexValues ) 259 { 260 return( matchEither( new MatchAllMatcher(), startingSet, regexNames, regexValues ) ); 261 } 262 263 264 265 public Set 266 matchAny( Set startingSet, String [] regexNames, String [] regexValues ) 267 { 268 return( matchEither( new MatchAnyMatcher(), startingSet, regexNames, regexValues ) ); 269 } 270 } 271 272 273 274 275 276 277 | Popular Tags |