1 23 24 29 package com.sun.appserv.management.util.jmx; 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.appserv.management.util.misc.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 214 Set <ObjectName > 215 matchEither( 216 Matcher matcher, 217 Set <ObjectName > startingSet, 218 String [] regexNames, 219 String [] regexValues ) 220 { 221 if ( regexNames == null && regexValues == null ) 222 { 223 return( startingSet ); 225 } 226 227 final Set <ObjectName > results = new HashSet <ObjectName >(); 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 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 <ObjectName > 254 matchAll( Set <ObjectName > startingSet, String [] regexNames, String [] regexValues ) 255 { 256 return( matchEither( new MatchAllMatcher(), startingSet, regexNames, regexValues ) ); 257 } 258 259 260 261 public Set <ObjectName > 262 matchAny( Set <ObjectName > startingSet, String [] regexNames, String [] regexValues ) 263 { 264 return( matchEither( new MatchAnyMatcher(), startingSet, regexNames, regexValues ) ); 265 } 266 } 267 268 269 270 271 272 273 | Popular Tags |