1 23 package com.sun.enterprise.management.support; 24 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.Iterator ; 29 import java.util.HashSet ; 30 import java.util.Arrays ; 31 import java.util.Collections ; 32 33 import com.sun.appserv.management.util.misc.Output; 34 import com.sun.appserv.management.util.misc.GSetUtil; 35 import com.sun.appserv.management.util.misc.StringUtil; 36 import com.sun.appserv.management.util.misc.MapUtil; 37 import com.sun.appserv.management.util.misc.CollectionUtil; 38 39 43 public class AMXAttributeNameMapperImpl implements AMXAttributeNameMapper 44 { 45 final Map <String ,String > mDerivedToOriginal; 46 final Map <String ,String > mOriginalToDerived; 47 final Set <String > mDerivedNames; 48 49 final Map <String ,String > mUnmappedOriginals; 50 51 private Output mDebug; 52 53 56 public 57 AMXAttributeNameMapperImpl() 58 { 59 mDebug = null; 60 mDerivedToOriginal = new HashMap <String ,String >(); 61 mOriginalToDerived = new HashMap <String ,String >(); 62 mDerivedNames = new HashSet <String >(); 63 64 mUnmappedOriginals = new HashMap <String ,String >(); 65 } 66 67 public void 68 setDebugOutput( final Output debugOutput ) 69 { 70 mDebug = debugOutput; 71 } 72 73 protected final void 74 debug(final Object o) 75 { 76 if ( mDebug != null ) 77 { 78 mDebug.println( o ); 79 } 80 } 81 82 private static final char DASH_CHAR = '-'; 83 private static final String DASH = "" + DASH_CHAR; 84 85 private String 86 stripDashes( final String name ) 87 { 88 String result = name; 89 90 if ( name.indexOf( DASH ) >= 0 ) 91 { 92 final StringBuilder builder = new StringBuilder (); 93 final int length = name.length(); 94 95 boolean upperNext = false; 96 for( int i = 0; i < length; ++i ) 97 { 98 final char c = name.charAt( i ); 99 100 if ( c == DASH_CHAR ) 101 { 102 upperNext = true; 103 } 104 else 105 { 106 if ( upperNext ) 107 { 108 builder.append( ("" + c ).toUpperCase() ); 109 upperNext = false; 110 } 111 else 112 { 113 builder.append( c ); 114 } 115 } 116 } 117 118 result = builder.toString(); 119 } 120 121 return result; 122 } 123 124 private String 125 normalize( final String s ) 126 { 127 return stripDashes( s ).toLowerCase(); 128 } 129 130 131 private Map <String ,String > 132 normalize( final Set <String > s ) 133 { 134 final Map <String ,String > result = new HashMap <String ,String >(); 135 136 for( final String name : s ) 137 { 138 result.put( normalize( name ), name ); 139 } 140 141 return result; 142 } 143 144 145 154 public String 155 matchName( 156 final String derivedName, 157 final String [] candidates ) 158 { 159 String result = null; 160 161 final String existingOriginal = derivedToOriginal( derivedName ); 162 if ( existingOriginal != null ) 163 { 164 throw new IllegalArgumentException ( "Already mapped: " + 165 derivedName + " => " + existingOriginal ); 166 } 167 if ( mUnmappedOriginals.keySet().size() == 0 ) 168 { 169 throw new IllegalArgumentException ( 170 "There are no unmapped originals, existing mappings: " + 171 MapUtil.toString( mOriginalToDerived, ", " ) ); 172 } 173 174 for( final String candidate : candidates ) 175 { 176 final String candidateNormalized = normalize( candidate ); 177 178 if ( mUnmappedOriginals.keySet().contains( candidateNormalized ) ) 179 { 180 result = mUnmappedOriginals.get( candidateNormalized ); 181 mUnmappedOriginals.remove( candidateNormalized ); 182 addMapping( result, derivedName ); 183 break; 184 } 185 } 186 187 if ( result == null ) 188 { 189 final String msg = derivedName + " can't be matched to " + 190 StringUtil.toString( ", " , (Object [])candidates ) + 191 ", unmapped originals: " + 192 CollectionUtil.toString( mUnmappedOriginals.values(), ", " ); 193 throw new IllegalArgumentException ( msg ); 194 } 195 196 return result; 197 } 198 199 public String 200 matchName( 201 final String derivedName, 202 final String candidate1, 203 final String candidate2 ) 204 { 205 return matchName( derivedName, new String [] { candidate1, candidate2 } ); 206 } 207 208 public String 209 matchName( 210 final String derivedName, 211 final String candidate1 ) 212 { 213 return matchName( derivedName, new String [] { candidate1 } ); 214 } 215 216 217 225 public void 226 matchNames( 227 final String [] derivedNames, 228 final String [] originalNames ) 229 { 230 final Set <String > originals = GSetUtil.newSet( originalNames ); 231 final Set <String > deriveds = GSetUtil.newSet( derivedNames ); 232 233 for( final String original : originalNames ) 235 { 236 if ( deriveds.contains( original ) ) 237 { 238 addMapping( original, original ); 240 originals.remove( original ); 241 deriveds.remove( original ); 242 } 243 } 244 245 if ( originals.size() != 0 ) 247 { 248 final Map <String ,String > originalsMap = normalize( originals ); 249 final Map <String ,String > derivedsMap = normalize( deriveds ); 250 251 for( final String originalLowerCase : originalsMap.keySet() ) 252 { 253 final String original = originalsMap.get( originalLowerCase ); 254 final String derived = derivedsMap.get( originalLowerCase ); 255 if ( derived != null ) 256 { 257 addMapping( original, derived ); 259 } 260 else 261 { 262 mUnmappedOriginals.put( normalize( original ), original ); 264 } 265 } 266 } 267 } 268 269 public Set <String > 270 getUnmappedOriginals() 271 { 272 Set <String > unmapped = Collections.emptySet(); 273 274 if ( mUnmappedOriginals.keySet().size() != 0 ) 275 { 276 unmapped = new HashSet <String >( mUnmappedOriginals.values()); 277 } 278 279 return unmapped; 280 } 281 282 public void 283 addMapping( 284 final String originalName, 285 final String derivedName ) 286 { 287 mDerivedToOriginal.put( derivedName, originalName ); 288 mOriginalToDerived.put( originalName, derivedName ); 289 290 if ( mUnmappedOriginals.keySet().contains( originalName ) ) 291 { 292 mUnmappedOriginals.remove( originalName ); 293 } 294 295 mDerivedNames.add( derivedName ); 296 } 297 298 public void 299 dontMap( final String originalName ) 300 { 301 addMapping( originalName, originalName ); 302 } 303 304 public String 305 originalToDerived( final String originalName ) 306 { 307 final String derivedName = mOriginalToDerived.get( originalName ); 308 return( derivedName ); 309 } 310 311 public String 312 derivedToOriginal( final String derivedName ) 313 { 314 final String original = mDerivedToOriginal.get( derivedName ); 315 return( original ); 316 } 317 318 public Set <String > 319 getDerivedNames( ) 320 { 321 return( mDerivedNames ); 322 } 323 324 public String 325 toString() 326 { 327 final StringBuilder builder = new StringBuilder (); 328 329 final String LINE_SEPARATOR = System.getProperty( "line.separator" ); 330 for( final String name : getDerivedNames() ) 331 { 332 builder.append( name ); 333 builder.append( " => " ); 334 builder.append( derivedToOriginal( name ) ); 335 builder.append( LINE_SEPARATOR ); 336 } 337 338 return( builder.toString() ); 339 } 340 } 341 342 343 344 345 346 347 348 349 350 351 352 | Popular Tags |