1 22 package org.jboss.mx.util; 23 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.StringTokenizer ; 27 28 import javax.management.MalformedObjectNameException ; 29 import javax.management.ObjectName ; 30 31 51 public class ObjectNameConverter 52 { 53 72 public static ObjectName convert( String pObjectName ) 73 throws MalformedObjectNameException 74 { 75 if( pObjectName == null ) { 76 throw new MalformedObjectNameException ( "null name" ); 77 } 78 79 if( pObjectName.length() == 0 ) { 81 pObjectName = "*:*"; 82 } 83 84 int lIndex = pObjectName.indexOf( ":" ); 85 if( lIndex < 0 ) { 86 throw new MalformedObjectNameException ( "missing domain" ); 87 } 88 String lDomain = pObjectName.substring( 0, lIndex ); 89 if( ( lIndex + 1 ) < pObjectName.length() ) { 90 return createObjectName(lDomain, pObjectName.substring( lIndex + 1 )); 91 } else { 92 throw new MalformedObjectNameException ( "properties missing" ); 93 } 94 } 95 96 107 public static ObjectName convert( String pDomainName, Hashtable pProperties ) 108 throws MalformedObjectNameException 109 { 110 if( pDomainName == null ) { 111 throw new MalformedObjectNameException ( "missing domain" ); 112 } 113 if( pProperties == null || pProperties.size() == 0 ) { 114 throw new MalformedObjectNameException (" null or empty properties" ); 115 } 116 return createObjectName(pDomainName, pProperties, false); 117 } 118 119 129 public static Hashtable getProperties( ObjectName pObjectName ) 130 { 131 Hashtable lReturn = reverseProperties( pObjectName.getKeyPropertyList() ); 132 if( pObjectName.isPropertyPattern() ) { 133 lReturn.put( "*", "*" ); 134 } 135 return lReturn; 136 } 137 138 148 public static String getString( ObjectName pObjectName ) 149 { 150 String lReturn = pObjectName.getDomain() + ":" + reverseString( pObjectName.getKeyPropertyList() ); 151 if( pObjectName.isPropertyPattern() ) { 152 lReturn = lReturn + ",*"; 153 } 154 return lReturn; 155 } 156 157 165 public static String convertCharacters( String pValue, boolean pEncrypt ) { 166 String lReturn = pValue; 167 if( pEncrypt ) { 168 int lIndex = lReturn.indexOf( "%" ); 169 while( lIndex >= 0 ) { 170 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 171 "%25" + 172 ( ( lIndex + 1 ) < lReturn.length() ? lReturn.substring( lIndex + 1 ) : "" ); 173 lIndex = lReturn.indexOf( "%", lIndex + 2 ); 174 } 175 lIndex = lReturn.indexOf( "*" ); 176 while( lIndex >= 0 ) { 177 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 178 "%2a" + 179 ( ( lIndex + 1 ) < lReturn.length() ? lReturn.substring( lIndex + 1 ) : "" ); 180 lIndex = lReturn.indexOf( "*" ); 181 } 182 lIndex = lReturn.indexOf( ":" ); 183 while( lIndex >= 0 ) { 184 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 185 "%3a" + 186 ( ( lIndex + 1 ) < lReturn.length() ? lReturn.substring( lIndex + 1 ) : "" ); 187 lIndex = lReturn.indexOf( ":" ); 188 } 189 lIndex = lReturn.indexOf( "?" ); 190 while( lIndex >= 0 ) { 191 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 192 "%3f" + 193 ( ( lIndex + 1 ) < lReturn.length() ? lReturn.substring( lIndex + 1 ) : "" ); 194 lIndex = lReturn.indexOf( "?" ); 195 } 196 lIndex = lReturn.indexOf( "=" ); 197 while( lIndex >= 0 ) { 198 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 199 "%3d" + 200 ( ( lIndex + 1 ) < lReturn.length() ? lReturn.substring( lIndex + 1 ) : "" ); 201 lIndex = lReturn.indexOf( "=" ); 202 } 203 lIndex = lReturn.indexOf( "," ); 204 while( lIndex >= 0 ) { 205 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 206 "%2c" + 207 ( ( lIndex + 1 ) < lReturn.length() ? lReturn.substring( lIndex + 1 ) : "" ); 208 lIndex = lReturn.indexOf( "," ); 209 } 210 } else { 211 int lIndex = lReturn.indexOf( "%2a" ); 212 while( lIndex >= 0 ) { 213 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 214 "*" + 215 ( ( lIndex + 3 ) < lReturn.length() ? lReturn.substring( lIndex + 3 ) : "" ); 216 lIndex = lReturn.indexOf( "%2a" ); 217 } 218 lIndex = lReturn.indexOf( "%3a" ); 219 while( lIndex >= 0 ) { 220 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 221 ":" + 222 ( ( lIndex + 3 ) < lReturn.length() ? lReturn.substring( lIndex + 3 ) : "" ); 223 lIndex = lReturn.indexOf( "%3a" ); 224 } 225 lIndex = lReturn.indexOf( "%3f" ); 226 while( lIndex >= 0 ) { 227 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 228 "?" + 229 ( ( lIndex + 3 ) < lReturn.length() ? lReturn.substring( lIndex + 3 ) : "" ); 230 lIndex = lReturn.indexOf( "%3f" ); 231 } 232 lIndex = lReturn.indexOf( "%3d" ); 233 while( lIndex >= 0 ) { 234 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 235 "=" + 236 ( ( lIndex + 3 ) < lReturn.length() ? lReturn.substring( lIndex + 3 ) : "" ); 237 lIndex = lReturn.indexOf( "%3d" ); 238 } 239 lIndex = lReturn.indexOf( "%2c" ); 240 while( lIndex >= 0 ) { 241 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 242 "," + 243 ( ( lIndex + 3 ) < lReturn.length() ? lReturn.substring( lIndex + 3 ) : "" ); 244 lIndex = lReturn.indexOf( "%2c" ); 245 } 246 lIndex = lReturn.indexOf( "%25" ); 247 while( lIndex >= 0 ) { 248 lReturn = ( lIndex > 0 ? lReturn.substring( 0, lIndex ) : "" ) + 249 "%" + 250 ( ( lIndex + 3 ) < lReturn.length() ? lReturn.substring( lIndex + 3 ) : "" ); 251 lIndex = lReturn.indexOf( "%25" ); 252 } 253 } 254 return lReturn; 255 } 256 257 269 private static ObjectName createObjectName(String domain, String properties) throws MalformedObjectNameException 270 { 271 if (null == properties || properties.length() < 1) 272 { 273 throw new MalformedObjectNameException ("null or empty properties"); 274 } 275 276 281 if (properties.startsWith(",") || properties.endsWith(",") || properties.indexOf(",,") != -1) 282 { 283 throw new MalformedObjectNameException ("empty key/value pair in properties string"); 284 } 285 286 Hashtable ptable = new Hashtable (); 287 288 StringTokenizer tokenizer = new StringTokenizer (properties, ","); 289 boolean lPattern = false; 290 while (tokenizer.hasMoreTokens()) 291 { 292 String chunk = tokenizer.nextToken(); 293 294 if (chunk.equals("*")) 295 { 296 lPattern = true; 297 continue; 298 } 299 300 int keylen = chunk.length(); 301 int eqpos = chunk.indexOf('='); 302 303 if (eqpos < 1 || (keylen == eqpos + 1)) 305 { 306 throw new MalformedObjectNameException ("malformed key/value pair: " + chunk); 307 } 308 309 String key = chunk.substring(0, eqpos); 310 if (ptable.containsKey(key)) 311 { 312 throw new MalformedObjectNameException ("duplicate key: " + key); 313 } 314 315 ptable.put(key, chunk.substring(eqpos + 1, keylen)); 316 } 317 318 return createObjectName(domain, ptable, lPattern); 319 } 320 321 328 private static ObjectName createObjectName(String domain, Hashtable properties, boolean pPattern) throws MalformedObjectNameException 329 { 330 if (null == properties || (!pPattern && properties.size() < 1)) 331 { 332 throw new MalformedObjectNameException ("null or empty properties"); 333 } 334 335 Iterator it = properties.keySet().iterator(); 336 Hashtable lReturn = new Hashtable ( properties.size() ); 337 while (it.hasNext()) 338 { 339 String key = null; 340 try 341 { 342 key = (String ) it.next(); 343 } 344 catch (ClassCastException e) 345 { 346 throw new MalformedObjectNameException ("key is not a string"); 347 } 348 349 String val = null; 350 try 351 { 352 val = (String ) properties.get(key); 353 } 354 catch (ClassCastException e) 355 { 356 throw new MalformedObjectNameException ("value is not a string"); 357 } 358 359 String lKey = convertCharacters( key, true ); 361 String lValue = convertCharacters( val, true ); 362 363 lReturn.put( lKey, lValue ); 364 } 365 ObjectName result = new ObjectName (domain, lReturn); 366 if (pPattern) 367 return new ObjectName (result.getCanonicalName() + ",*"); 368 return result; 369 } 370 371 private static Hashtable reverseProperties( Hashtable pProperties ) { 372 Hashtable lReturn = new Hashtable ( pProperties.size() ); 373 Iterator i = pProperties.keySet().iterator(); 374 while( i.hasNext() ) { 375 String lKey = (String ) i.next(); 376 String lValue = (String ) pProperties.get( lKey ); 377 lKey = convertCharacters( lKey, false ); 378 lValue = convertCharacters( lValue, false ); 379 lReturn.put( lKey, lValue ); 380 } 381 return lReturn; 382 } 383 384 private static String reverseString( Hashtable pProperties ) { 385 StringBuffer lReturn = new StringBuffer (); 386 Iterator i = pProperties.keySet().iterator(); 387 while( i.hasNext() ) { 388 String lKey = (String ) i.next(); 389 String lValue = (String ) pProperties.get( lKey ); 390 lKey = convertCharacters( lKey, false ); 391 lValue = convertCharacters( lValue, false ); 392 if( lReturn.length() > 0 ) { 393 lReturn.append( "," ); 394 } 395 lReturn.append( lKey ); 396 lReturn.append( "=" ); 397 lReturn.append( lValue ); 398 } 399 return lReturn.toString(); 400 } 401 } 402 | Popular Tags |