1 26 27 package net.sourceforge.groboutils.util.classes.v1; 28 29 import java.util.Hashtable ; 30 31 32 33 34 59 public class ClassUtil 60 { 61 64 67 72 protected static ClassUtil s_instance = new ClassUtil(); 73 74 private IUrlClassLoader classLoader = null; 75 private Hashtable classHash = new Hashtable (); 76 77 80 84 protected ClassUtil() 85 { 86 89 if (isJdk2Compatible()) 90 { 91 this.classLoader = (IUrlClassLoader)createObject( 92 getClassPackage( ClassUtil.class ) + ".jdk2.UrlClassLoader" ); 93 } 94 if (this.classLoader == null) 95 { 96 this.classLoader = (IUrlClassLoader)createObject( 97 getClassPackage( ClassUtil.class ) + ".jdk0.UrlClassLoader" ); 98 if (this.classLoader == null) 99 { 100 throw new IllegalStateException ( 101 "There was an error loading a class loader." ); 102 } 103 } 104 } 105 106 107 110 111 116 public static ClassUtil getInstance() 117 { 118 121 return s_instance; 122 } 123 124 125 126 129 130 136 public void flush() 137 { 138 this.classHash = new Hashtable (); 139 this.classLoader.flush(); 140 } 141 142 143 151 public Class getClass( String name ) 152 { 153 return getClass( name, null ); 154 } 155 156 157 165 public Class getClass( String name, String jarName ) 166 { 167 String hashName = getClassHashName( name, jarName ); 168 Class c = (Class )classHash.get( hashName ); 169 if (c == null) 170 { 171 c = loadClass( name, jarName ); 172 if (c != null) 173 { 174 classHash.put( hashName, c ); 175 } 176 } 177 return c; 178 } 179 180 181 193 public Object createObject( String className ) 194 { 195 return createObject( className, null ); 196 } 197 198 199 212 public Object createObject( String className, String jarName ) 213 { 214 return createObject( getClass( className, jarName ) ); 215 } 216 217 218 228 public Object createObject( Class c ) 229 { 230 if (c == null) return null; 231 232 Object obj = null; 233 try 234 { 235 obj = c.newInstance(); 236 } 237 catch (InstantiationException ie) 238 { 239 System.out.println("Could not instantiate "+c.getName()+ 240 ": "+ie.getMessage()); 241 obj = null; 242 } 243 catch (IllegalAccessException iae) 244 { 245 System.out.println("Could not instantiate "+c.getName()+ 246 ": "+iae.getMessage()); 247 obj = null; 248 } 249 catch (NoSuchMethodError nsme) 250 { 251 System.out.println("Could not instantiate "+c.getName()+ 252 ": "+nsme.getMessage()); 253 obj = null; 254 } 255 return obj; 256 } 257 258 259 266 public boolean isJdk2Compatible() 267 { 268 return (getClass( "java.net.URLClassLoader" ) != null); 269 } 270 271 272 280 public String getClassPackage( Class c ) 281 { 282 if (c == null) 283 { 284 return null; 285 } 286 String fullname = c.getName(); 287 int pos = fullname.lastIndexOf( '.' ); 288 if (pos < 0) 289 { 290 return ""; 292 } 293 294 return fullname.substring( 0, pos ); 296 } 297 298 299 302 303 312 protected String getClassHashName( String name, String jarName ) 313 { 314 if (jarName == null) 315 { 316 jarName = "<null>"; 317 } 318 StringBuffer sb = new StringBuffer ( jarName ); 319 sb.append( ';' ); 320 sb.append( name ); 321 return new String ( sb ); 322 } 323 324 325 336 protected Class loadClass( String className, String baseJar ) 337 { 338 Class c; 339 340 try 341 { 342 if (baseJar == null || baseJar.length() <= 0) 343 { 344 try 345 { 346 c = Class.forName( className ); 347 } 348 catch (ClassNotFoundException cnfe) 349 { 350 c = null; 352 } 353 } 354 else 355 { 356 c = this.classLoader.loadClass( className, baseJar ); 357 } 358 } 359 catch (Throwable t) 360 { 361 c = null; 363 } 364 return c; 365 } 366 367 368 369 } 372 373 | Popular Tags |