1 18 package org.apache.tools.ant.util; 19 20 import org.apache.tools.ant.AntClassLoader; 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.ProjectComponent; 24 import org.apache.tools.ant.MagicNames; 25 import org.apache.tools.ant.types.Path; 26 import org.apache.tools.ant.types.Reference; 27 28 30 70 public class ClasspathUtils { 71 72 75 public static final String REUSE_LOADER_REF = MagicNames.REFID_CLASSPATH_REUSE_LOADER; 76 77 87 public static ClassLoader getClassLoaderForPath( 88 Project p, Reference ref) { 89 90 return getClassLoaderForPath(p, ref, false); 91 } 92 93 107 public static ClassLoader getClassLoaderForPath( 108 Project p, Reference ref, boolean reverseLoader) { 109 110 String pathId = ref.getRefId(); 111 Object path = p.getReference(pathId); 112 if (!(path instanceof Path)) { 113 throw new BuildException( 114 "The specified classpathref " 115 + pathId 116 + " does not reference a Path."); 117 } 118 String loaderId = MagicNames.REFID_CLASSPATH_LOADER_PREFIX + pathId; 119 return getClassLoaderForPath(p, (Path) path, loaderId, reverseLoader); 120 } 121 122 133 public static ClassLoader getClassLoaderForPath( 134 Project p, Path path, String loaderId) { 135 136 return getClassLoaderForPath(p, path, loaderId, false); 137 } 138 139 154 public static ClassLoader getClassLoaderForPath( 155 Project p, Path path, String loaderId, boolean reverseLoader) { 156 return getClassLoaderForPath(p, path, loaderId, reverseLoader, 157 isMagicPropertySet(p)); 158 } 159 160 177 public static ClassLoader getClassLoaderForPath( 178 Project p, Path path, String loaderId, boolean reverseLoader, 179 boolean reuseLoader) { 180 181 ClassLoader cl = null; 182 183 if (loaderId != null && reuseLoader) { 185 Object reusedLoader = p.getReference(loaderId); 186 if (reusedLoader != null 187 && !(reusedLoader instanceof ClassLoader )) { 188 throw new BuildException("The specified loader id " + loaderId 189 + " does not reference a class loader"); 190 } 191 cl = (ClassLoader ) reusedLoader; 192 } 193 if (cl == null) { 194 cl = getUniqueClassLoaderForPath(p, path, reverseLoader); 195 if (loaderId != null && reuseLoader) { 196 p.addReference(loaderId, cl); 197 } 198 } 199 return cl; 200 } 201 202 215 public static ClassLoader getUniqueClassLoaderForPath( 216 Project p, 217 Path path, 218 boolean reverseLoader) { 219 AntClassLoader acl = p.createClassLoader(path); 220 if (reverseLoader) { 221 acl.setParentFirst(false); 222 acl.addJavaLibraries(); 223 } 224 return acl; 225 } 226 227 239 public static Object newInstance( 240 String className, 241 ClassLoader userDefinedLoader) { 242 return newInstance(className, userDefinedLoader, Object .class); 243 } 244 245 261 public static Object newInstance( 262 String className, 263 ClassLoader userDefinedLoader, 264 Class expectedType) { 265 try { 266 Class clazz = Class.forName(className, true, userDefinedLoader); 267 Object o = clazz.newInstance(); 268 if (!expectedType.isInstance(o)) { 269 throw new BuildException( 270 "Class of unexpected Type: " 271 + className 272 + " expected :" 273 + expectedType); 274 } 275 return o; 276 } catch (ClassNotFoundException e) { 277 throw new BuildException( 278 "Class not found: " 279 + className, 280 e); 281 } catch (InstantiationException e) { 282 throw new BuildException( 283 "Could not instantiate " 284 + className 285 + ". Specified class should have a no " 286 + "argument constructor.", 287 e); 288 } catch (IllegalAccessException e) { 289 throw new BuildException( 290 "Could not instantiate " 291 + className 292 + ". Specified class should have a " 293 + "public constructor.", 294 e); 295 } catch (LinkageError e) { 296 throw new BuildException( 297 "Class " 298 + className 299 + " could not be loaded because of an invalid dependency.", 300 e); 301 } 302 } 303 304 311 public static Delegate getDelegate(ProjectComponent component) { 312 return new Delegate(component); 313 } 314 315 319 private static boolean isMagicPropertySet(Project p) { 320 return p.getProperty(REUSE_LOADER_REF) != null; 321 } 322 323 341 public static class Delegate { 342 private final ProjectComponent component; 343 private Path classpath; 344 private String classpathId; 345 private String className; 346 private String loaderId; 347 private boolean reverseLoader = false; 348 349 353 Delegate(ProjectComponent component) { 354 this.component = component; 355 } 356 357 364 public void setClasspath(Path classpath) { 365 if (this.classpath == null) { 366 this.classpath = classpath; 367 } else { 368 this.classpath.append(classpath); 369 } 370 } 371 372 380 public Path createClasspath() { 381 if (this.classpath == null) { 382 this.classpath = new Path(component.getProject()); 383 } 384 return this.classpath.createPath(); 385 } 386 387 395 public void setClassname(String fcqn) { 396 this.className = fcqn; 397 } 398 399 407 public void setClasspathref(Reference r) { 408 this.classpathId = r.getRefId(); 409 createClasspath().setRefid(r); 410 } 411 412 427 public void setReverseLoader(boolean reverseLoader) { 428 this.reverseLoader = reverseLoader; 429 } 430 431 435 public void setLoaderRef(Reference r) { 436 this.loaderId = r.getRefId(); 437 } 438 439 440 444 public ClassLoader getClassLoader() { 445 return getClassLoaderForPath( 446 getContextProject(), 447 this.classpath, 448 getClassLoadId(), 449 this.reverseLoader, 450 loaderId != null || isMagicPropertySet(getContextProject())); 451 } 452 453 456 private Project getContextProject() { 457 return this.component.getProject(); 458 } 459 460 464 public String getClassLoadId() { 465 return this.loaderId == null && this.classpathId != null 466 ? MagicNames.REFID_CLASSPATH_LOADER_PREFIX + this.classpathId 467 : this.loaderId; 468 } 469 470 476 public Object newInstance() { 477 return ClasspathUtils.newInstance(this.className, getClassLoader()); 478 } 479 480 484 public Path getClasspath() { 485 return classpath; 486 } 487 488 492 public boolean isReverseLoader() { 493 return reverseLoader; 494 } 495 496 } 499 } 500 | Popular Tags |