| 1 16 17 18 package org.apache.commons.modeler.util; 19 20 import java.io.File ; 21 import java.io.FilenameFilter ; 22 import java.io.IOException ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.net.InetAddress ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.net.UnknownHostException ; 29 import java.util.Hashtable ; 30 import java.util.StringTokenizer ; 31 import java.util.Vector ; 32 33 36 41 public final class IntrospectionUtils { 42 static final Class NO_PARAMS[]=new Class [0]; 43 static final Class STRING_OBJ_PARAM[]=new Class [] { 44 String .class, Object .class }; 45 static final Class STRING_PARAM[]=new Class [] { 46 String .class }; 47 48 50 public static void execute( Object proxy, String method ) 51 throws Exception  52 { 53 Method executeM=null; 54 Class c=proxy.getClass(); 55 executeM=findMethod( c, method, NO_PARAMS ); 56 if( executeM == null ) { 57 throw new RuntimeException ("No method " + method + " in " + 58 proxy.getClass() ); 59 } 60 executeM.invoke(proxy, null ); 61 } 62 63 66 public static void setAttribute( Object proxy, String n, Object v) 67 throws Exception  68 { 69 if( proxy instanceof AttributeHolder ) { 70 ((AttributeHolder)proxy).setAttribute( n, v ); 71 return; 72 } 73 74 Method executeM=null; 75 Class c=proxy.getClass(); 76 executeM=findMethod( c, "setAttribute", STRING_OBJ_PARAM ); 77 78 if( executeM == null ) { 79 System.out.println("No setAttribute in " + proxy.getClass() ); 80 return; 81 } 82 executeM.invoke(proxy, new Object [] { n, v }); 83 return; 84 } 85 86 87 90 public static Object getAttribute( Object proxy, String n) 91 throws Exception  92 { 93 Method executeM=null; 94 Class c=proxy.getClass(); 95 executeM=findMethod( c, "getAttribute", STRING_PARAM); 96 if( executeM == null ) { 97 System.out.println("No getAttribute in " + proxy.getClass() ); 98 return null; 99 } 100 return executeM.invoke(proxy, new Object [] { n }); 101 } 102 103 104 106 public static ClassLoader getURLClassLoader( URL urls[], 107 ClassLoader parent ) 108 { 109 try { 110 Class urlCL=Class.forName( "java.net.URLClassLoader"); 111 Class paramT[]=new Class [2]; 112 paramT[0]= urls.getClass(); 113 paramT[1]=ClassLoader .class; 114 Method m=findMethod( urlCL, "newInstance", paramT); 115 if( m==null ) return null; 116 117 ClassLoader cl=(ClassLoader )m.invoke( urlCL, 118 new Object [] { urls, 119 parent } ); 120 return cl; 121 } catch(ClassNotFoundException ex ) { 122 return null; 124 } catch(Exception ex ) { 125 ex.printStackTrace(); 126 return null; 127 } 128 } 129 130 131 public static String guessInstall(String installSysProp, 132 String homeSysProp, String jarName) { 133 return guessInstall( installSysProp, homeSysProp, jarName, null); 134 } 135 136 147 public static String guessInstall(String installSysProp, String homeSysProp, 148 String jarName, String classFile) { 149 String install=null; 150 String home=null; 151 152 if ( installSysProp != null ) 153 install=System.getProperty( installSysProp ); 154 155 if( homeSysProp != null ) 156 home=System.getProperty( homeSysProp ); 157 158 if ( install != null ) { 159 if ( home == null ) 160 System.getProperties().put( homeSysProp, install ); 161 return install; 162 } 163 164 166 String cpath=System.getProperty( "java.class.path"); 167 String pathSep=System.getProperty( "path.separator"); 168 StringTokenizer st=new StringTokenizer ( cpath, pathSep ); 169 while( st.hasMoreTokens() ) { 170 String path=st.nextToken(); 171 if( path.endsWith( jarName ) ) { 173 home=path.substring( 0, path.length() - jarName.length() ); 174 try { 175 if( "".equals(home) ) { 176 home=new File ("./").getCanonicalPath(); 177 } else if( home.endsWith(File.separator) ) { 178 home = home.substring(0,home.length()-1); 179 } 180 File f=new File ( home ); 181 String parentDir = f.getParent(); 182 if(parentDir == null) 183 parentDir = home; File f1=new File ( parentDir ); 185 install = f1.getCanonicalPath(); 186 if( installSysProp != null ) 187 System.getProperties().put( installSysProp, install ); 188 if( home == null && homeSysProp != null ) 189 System.getProperties().put( homeSysProp, install ); 190 return install; 191 } catch( Exception ex ) { 192 ex.printStackTrace(); 193 } 194 } else { 195 String fname=path + ( path.endsWith("/") ?"":"/" ) + classFile; 196 if( new File ( fname ).exists()) { 197 try { 198 File f=new File ( path ); 199 String parentDir = f.getParent(); 200 if( parentDir == null ) 201 parentDir = path; File f1=new File ( parentDir ); 203 install = f1.getCanonicalPath(); 204 if( installSysProp != null ) 205 System.getProperties().put( installSysProp, 206 install ); 207 if( home == null && homeSysProp != null ) 208 System.getProperties().put( homeSysProp, install ); 209 return install; 210 } catch( Exception ex ) { 211 ex.printStackTrace(); 212 } 213 } 214 } 215 } 216 217 if ( home != null ) { 219 System.getProperties().put( installSysProp, home ); 220 return home; 221 } 222 223 return null; 224 } 225 226 228 public static void displayClassPath( String msg, URL [] cp ) { 229 System.out.println(msg); 230 for( int i=0; i<cp.length; i++ ) { 231 System.out.println( cp[i].getFile() ); 232 } 233 } 234 235 public static String PATH_SEPARATOR = System.getProperty("path.separator"); 236 243 public static String classPathAdd(URL urls[], String cp ) 244 { 245 if( urls==null ) return cp; 246 247 for( int i=0; i<urls.length; i++ ) { 248 if( cp != null) 249 cp += PATH_SEPARATOR + urls[i].getFile(); 250 else 251 cp = urls[i].getFile(); 252 } 253 return cp; 254 } 255 256 260 public static void setProperty( Object o, String name, String value ) { 261 if( dbg > 1 ) d("setProperty(" + 262 o.getClass() + " " + name + "=" + 263 value +")" ); 264 265 String setter= "set" +capitalize(name); 266 267 try { 268 Method methods[]=findMethods( o.getClass() ); 269 Method setPropertyMethod=null; 270 271 for( int i=0; i< methods.length; i++ ) { 273 Class paramT[]=methods[i].getParameterTypes(); 274 if( setter.equals( methods[i].getName() ) && 275 paramT.length == 1 && 276 "java.lang.String".equals( paramT[0].getName())) { 277 278 methods[i].invoke( o, new Object [] { value } ); 279 return; 280 } 281 } 282 283 for( int i=0; i< methods.length; i++ ) { 285 boolean ok=true; 286 if( setter.equals( methods[i].getName() ) && 287 methods[i].getParameterTypes().length == 1) { 288 289 Class paramType=methods[i].getParameterTypes()[0]; 291 Object params[]=new Object [1]; 292 293 if ("java.lang.Integer".equals( paramType.getName()) || 295 "int".equals( paramType.getName())) { 296 try { 297 params[0]=new Integer (value); 298 } catch( NumberFormatException ex ) {ok=false;} 299 300 } else if ("java.lang.Boolean". 302 equals( paramType.getName()) || 303 "boolean".equals( paramType.getName())) { 304 params[0]=new Boolean (value); 305 306 } else if ("java.net.InetAddress". 308 equals( paramType.getName())){ 309 try{ 310 params[0]= InetAddress.getByName(value); 311 }catch(UnknownHostException exc) { 312 d("Unable to resolve host name:" + value); 313 ok=false; 314 } 315 316 } else if ("java.lang.Object". 318 equals( paramType.getName())) { 319 params[0] = value; 320 321 } else { 323 d("Unknown type " + paramType.getName() ); 324 } 325 326 if( ok ) { 327 methods[i].invoke( o, params ); 328 return; 329 } 330 } 331 332 if( "setProperty".equals( methods[i].getName())) { 334 setPropertyMethod=methods[i]; 335 } 336 } 337 338 if( setPropertyMethod != null ) { 340 Object params[]=new Object [2]; 341 params[0]=name; 342 params[1]=value; 343 setPropertyMethod.invoke( o, params ); 344 } 345 346 } catch( IllegalArgumentException ex2 ) { 347 System.err.println("IAE " + o + " " + name + " " + value); 348 ex2.printStackTrace(); 349 } catch( SecurityException ex1 ) { 350 if( dbg > 0 ) 351 d("SecurityException for " + o.getClass() + " " + 352 name + "=" + value +")" ); 353 if( dbg > 1 ) ex1.printStackTrace(); 354 } catch (IllegalAccessException iae) { 355 if( dbg > 0 ) 356 d("IllegalAccessException for " + 357 o.getClass() + " " + name + "=" + value +")" ); 358 if( dbg > 1 ) iae.printStackTrace(); 359 } catch (InvocationTargetException ie) { 360 if( dbg > 0 ) 361 d("InvocationTargetException for " + o.getClass() + 362 " " + name + "=" + value +")" ); 363 if( dbg > 1 ) ie.printStackTrace(); 364 } 365 } 366 367 public static Object getProperty( Object o, String name ) { 368 String getter= "get" +capitalize(name); 369 370 try { 371 Method methods[]=findMethods( o.getClass() ); 372 Method getPropertyMethod=null; 373 374 for( int i=0; i< methods.length; i++ ) { 376 Class paramT[]=methods[i].getParameterTypes(); 377 if( getter.equals( methods[i].getName() ) && 378 paramT.length == 0 ) { 379 return methods[i].invoke( o, null ); 380 } 381 382 if( "getProperty".equals( methods[i].getName())) { 383 getPropertyMethod=methods[i]; 384 } 385 if( "getAttribute".equals( methods[i].getName())) { 386 getPropertyMethod=methods[i]; 387 } 388 } 389 390 if( getPropertyMethod != null ) { 392 Object params[]=new Object [1]; 393 params[0]=name; 394 getPropertyMethod.invoke( o, params ); 395 } 396 397 } catch( IllegalArgumentException ex2 ) { 398 System.err.println("IAE " + o + " " + name ); 399 ex2.printStackTrace(); 400 } catch( SecurityException ex1 ) { 401 if( dbg > 0 ) 402 d("SecurityException for " + o.getClass() + " " + 403 name + ")" ); 404 if( dbg > 1 ) ex1.printStackTrace(); 405 } catch (IllegalAccessException iae) { 406 if( dbg > 0 ) 407 d("IllegalAccessException for " + 408 o.getClass() + " " + name +")" ); 409 if( dbg > 1 ) iae.printStackTrace(); 410 } catch (InvocationTargetException ie) { 411 if( dbg > 0 ) 412 d("InvocationTargetException for " + o.getClass() + 413 " " + name +")" ); 414 if( dbg > 1 ) ie.printStackTrace(); 415 } 416 return null; 417 } 418 419 421 public static void setProperty( Object o, String name ) { 422 String setter= "set" +capitalize(name); 423 try { 424 Method methods[]=findMethods( o.getClass() ); 425 Method setPropertyMethod=null; 426 for( int i=0; i< methods.length; i++ ) { 428 Class paramT[]=methods[i].getParameterTypes(); 429 if( setter.equals( methods[i].getName() ) && 430 paramT.length == 0 ) { 431 methods[i].invoke( o, new Object [] {} ); 432 return; 433 } 434 } 435 } catch( Exception ex1 ) { 436 if( dbg > 0 ) 437 d("Exception for " + o.getClass() + " " + name); 438 if( dbg > 1 ) ex1.printStackTrace(); 439 } 440 } 441 442 445 public static String replaceProperties(String value, 446 Object getter ) 447 { 448 if( getter instanceof Hashtable ) 449 return replaceProperties( value, (Hashtable )getter, null ); 450 451 if( getter instanceof PropertySource ) { 452 PropertySource src[]=new PropertySource[] {(PropertySource)getter}; 453 return replaceProperties( value, null, src); 454 } 455 return value; 456 } 457 458 460 public static String replaceProperties(String value, 461 Hashtable staticProp, PropertySource dynamicProp[] ) 462 { 463 StringBuffer sb=new StringBuffer (); 464 int prev=0; 465 int pos; 467 while( (pos=value.indexOf( "$", prev )) >= 0 ) { 468 if(pos>0) { 469 sb.append( value.substring( prev, pos ) ); 470 } 471 if( pos == (value.length() - 1)) { 472 sb.append('$'); 473 prev = pos + 1; 474 } 475 else if (value.charAt( pos + 1 ) != '{' ) { 476 sb.append( value.charAt( pos + 1 ) ); 477 prev=pos+2; } else { 479 int endName=value.indexOf( '}', pos ); 480 if( endName < 0 ) { 481 sb.append( value.substring( pos )); 482 prev=value.length(); 483 continue; 484 } 485 String n=value.substring( pos+2, endName ); 486 String v= null; 487 if( staticProp != null ) { 488 v=(String )((Hashtable )staticProp).get(n); 489 } 490 if( v==null && dynamicProp != null) { 491 for( int i=0; i<dynamicProp.length; i++ ) { 492 v=dynamicProp[i].getProperty( n ); 493 if( v!=null ) { 494 break; 495 } 496 } 497 } 498 if( v== null ) 499 v = "${"+n+"}"; 500 501 sb.append( v ); 502 prev=endName+1; 503 } 504 } 505 if( prev < value.length() ) sb.append( value.substring( prev ) ); 506 return sb.toString(); 507 } 508 509 511 public static String capitalize(String name) { 512 if (name == null || name.length() == 0) { 513 return name; 514 } 515 char chars[] = name.toCharArray(); 516 chars[0] = Character.toUpperCase(chars[0]); 517 return new String (chars); 518 } 519 520 public static String unCapitalize(String name) { 521 if (name == null || name.length() == 0) { 522 return name; 523 } 524 char chars[] = name.toCharArray(); 525 chars[0] = Character.toLowerCase(chars[0]); 526 return new String (chars); 527 } 528 529 531 534 public static void addToClassPath( Vector cpV, String dir ) { 535 try{ 536 String cpComp[]=getFilesByExt(dir, ".jar"); 537 if (cpComp != null){ 538 int jarCount=cpComp.length; 539 for( int i=0; i< jarCount ; i++ ) { 540 URL url=getURL( dir , cpComp[i] ); 541 if( url!=null ) 542 cpV.addElement( url ); 543 } 544 } 545 }catch(Exception ex){ 546 ex.printStackTrace(); 547 } 548 } 549 550 551 public static void addToolsJar( Vector v ) 552 { 553 try { 554 File f=new File ( System.getProperty( "java.home" ) + 556 "/../lib/tools.jar"); 557 558 if( ! f.exists() ) { 559 f=new File ( System.getProperty( "java.home" ) + 562 "/lib/tools.jar"); 563 if( f.exists() ) { 564 System.out.println("Detected strange java.home value " + 565 System.getProperty( "java.home" ) + 566 ", it should point to jre"); 567 } 568 } 569 URL url=new URL ( "file", "" , f.getAbsolutePath() ); 570 571 v.addElement( url ); 572 } catch ( MalformedURLException ex ) { 573 ex.printStackTrace(); 574 } 575 } 576 577 578 580 public static String [] getFilesByExt( String ld, String ext ) { 581 File dir = new File (ld); 582 String [] names=null; 583 final String lext=ext; 584 if (dir.isDirectory()){ 585 names = dir.list( new FilenameFilter (){ 586 public boolean accept(File d, String name) { 587 if (name.endsWith(lext)){ 588 return true; 589 } 590 return false; 591 } 592 }); 593 } 594 return names; 595 } 596 597 598 600 public static URL getURL( String base, String file ) { 601 try { 602 File baseF = new File (base); 603 File f = new File (baseF,file); 604 String path = f.getCanonicalPath(); 605 if( f.isDirectory() ){ 606 path +="/"; 607 } 608 if( ! f.exists() ) return null; 609 return new URL ( "file", "", path ); 610 } catch (Exception ex) { 611 ex.printStackTrace(); 612 return null; 613 } 614 } 615 616 624 public static void addJarsFromClassPath(Vector jars, String cp) 625 throws IOException ,MalformedURLException  626 { 627 String sep = System.getProperty("path.separator"); 628 String token; 629 StringTokenizer st; 630 if(cp!=null){ 631 st = new StringTokenizer (cp,sep); 632 while(st.hasMoreTokens()){ 633 File f = new File (st.nextToken()); 634 String path = f.getCanonicalPath(); 635 if(f.isDirectory()){ 636 path += "/"; 637 } 638 URL url = new URL ("file","",path); 639 if(!jars.contains(url)){ 640 jars.addElement(url); 641 } 642 } 643 } 644 } 645 646 648 public static URL [] getClassPath(Vector v){ 649 URL [] urls=new URL [ v.size() ]; 650 for( int i=0; i<v.size(); i++ ) { 651 urls[i]=(URL )v.elementAt( i ); 652 } 653 return urls; 654 } 655 656 659 public static URL [] getClassPath( String dir, String cpath, 660 String cpathProp, boolean addTools ) 661 throws IOException , MalformedURLException  662 { 663 Vector jarsV = new Vector (); 664 if( dir!=null ) { 665 URL url=getURL( dir, "classes"); 667 if( url!=null ) 668 jarsV.addElement(url); 669 addToClassPath( jarsV, dir ); 670 } 671 672 if( cpath != null ) 673 addJarsFromClassPath(jarsV,cpath); 674 675 if( cpathProp!=null ) { 676 String cpath1=System.getProperty( cpathProp ); 677 addJarsFromClassPath(jarsV,cpath1); 678 } 679 680 if(addTools) 681 addToolsJar( jarsV ); 682 683 return getClassPath(jarsV); 684 } 685 686
|