You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AntClassLoader.java 43 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant;
  55. import java.lang.reflect.Constructor;
  56. import java.lang.reflect.Method;
  57. import java.lang.reflect.InvocationTargetException;
  58. import java.util.Enumeration;
  59. import java.util.Vector;
  60. import java.util.Hashtable;
  61. import java.util.zip.ZipFile;
  62. import java.util.zip.ZipEntry;
  63. import java.io.File;
  64. import java.io.InputStream;
  65. import java.io.FileInputStream;
  66. import java.io.IOException;
  67. import java.io.ByteArrayOutputStream;
  68. import java.net.URL;
  69. import java.net.MalformedURLException;
  70. import org.apache.tools.ant.types.Path;
  71. import org.apache.tools.ant.util.LoaderUtils;
  72. /**
  73. * Used to load classes within ant with a different claspath from
  74. * that used to start ant. Note that it is possible to force a class
  75. * into this loader even when that class is on the system classpath by
  76. * using the forceLoadClass method. Any subsequent classes loaded by that
  77. * class will then use this loader rather than the system class loader.
  78. *
  79. * @author Conor MacNeill
  80. * @author <a href="mailto:Jesse.Glick@netbeans.com">Jesse Glick</a>
  81. * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
  82. */
  83. public class AntClassLoader extends ClassLoader implements BuildListener {
  84. /**
  85. * An enumeration of all resources of a given name found within the
  86. * classpath of this class loader. This enumeration is used by the
  87. * ClassLoader.findResources method, which is in
  88. * turn used by the ClassLoader.getResources method.
  89. *
  90. * @see AntClassLoader#findResources(String)
  91. * @see java.lang.ClassLoader#getResources(String)
  92. * @author <a href="mailto:hermand@alumni.grinnell.edu">David A. Herman</a>
  93. */
  94. private class ResourceEnumeration implements Enumeration {
  95. /**
  96. * The name of the resource being searched for.
  97. */
  98. private String resourceName;
  99. /**
  100. * The index of the next classpath element to search.
  101. */
  102. private int pathElementsIndex;
  103. /**
  104. * The URL of the next resource to return in the enumeration. If this
  105. * field is <code>null</code> then the enumeration has been completed,
  106. * i.e., there are no more elements to return.
  107. */
  108. private URL nextResource;
  109. /**
  110. * Constructs a new enumeration of resources of the given name found
  111. * within this class loader's classpath.
  112. *
  113. * @param name the name of the resource to search for.
  114. */
  115. ResourceEnumeration(String name) {
  116. this.resourceName = name;
  117. this.pathElementsIndex = 0;
  118. findNextResource();
  119. }
  120. /**
  121. * Indicates whether there are more elements in the enumeration to
  122. * return.
  123. *
  124. * @return <code>true</code> if there are more elements in the
  125. * enumeration; <code>false</code> otherwise.
  126. */
  127. public boolean hasMoreElements() {
  128. return (this.nextResource != null);
  129. }
  130. /**
  131. * Returns the next resource in the enumeration.
  132. *
  133. * @return the next resource in the enumeration
  134. */
  135. public Object nextElement() {
  136. URL ret = this.nextResource;
  137. findNextResource();
  138. return ret;
  139. }
  140. /**
  141. * Locates the next resource of the correct name in the classpath and
  142. * sets <code>nextResource</code> to the URL of that resource. If no
  143. * more resources can be found, <code>nextResource</code> is set to
  144. * <code>null</code>.
  145. */
  146. private void findNextResource() {
  147. URL url = null;
  148. while ((pathElementsIndex < pathComponents.size()) &&
  149. (url == null)) {
  150. try {
  151. File pathComponent
  152. = (File)pathComponents.elementAt(pathElementsIndex);
  153. url = getResourceURL(pathComponent, this.resourceName);
  154. pathElementsIndex++;
  155. } catch (BuildException e) {
  156. // ignore path elements which are not valid relative to the
  157. // project
  158. }
  159. }
  160. this.nextResource = url;
  161. }
  162. }
  163. /**
  164. * The size of buffers to be used in this classloader.
  165. */
  166. private static final int BUFFER_SIZE = 8192;
  167. /**
  168. * The components of the classpath that the classloader searches
  169. * for classes.
  170. */
  171. private Vector pathComponents = new Vector();
  172. /**
  173. * The project to which this class loader belongs.
  174. */
  175. private Project project;
  176. /**
  177. * Indicates whether the parent class loader should be
  178. * consulted before trying to load with this class loader.
  179. */
  180. private boolean parentFirst = true;
  181. /**
  182. * These are the package roots that are to be loaded by the parent class
  183. * loader regardless of whether the parent class loader is being searched
  184. * first or not.
  185. */
  186. private Vector systemPackages = new Vector();
  187. /**
  188. * These are the package roots that are to be loaded by this class loader
  189. * regardless of whether the parent class loader is being searched first
  190. * or not.
  191. */
  192. private Vector loaderPackages = new Vector();
  193. /**
  194. * Whether or not this classloader will ignore the base
  195. * classloader if it can't find a class.
  196. *
  197. * @see #setIsolated(boolean)
  198. */
  199. private boolean ignoreBase = false;
  200. /**
  201. * The parent class loader, if one is given or can be determined.
  202. */
  203. private ClassLoader parent = null;
  204. /**
  205. * A hashtable of zip files opened by the classloader (File to ZipFile).
  206. */
  207. private Hashtable zipFiles = new Hashtable();
  208. /**
  209. * The context loader saved when setting the thread's current
  210. * context loader.
  211. */
  212. private ClassLoader savedContextLoader = null;
  213. /**
  214. * Whether or not the context loader is currently saved.
  215. */
  216. private boolean isContextLoaderSaved = false;
  217. /**
  218. * Reflection method reference for getProtectionDomain;
  219. * used to avoid 1.1-compatibility problems.
  220. */
  221. private static Method getProtectionDomain = null;
  222. /**
  223. * Reflection method reference for defineClassProtectionDomain;
  224. * used to avoid 1.1-compatibility problems.
  225. */
  226. private static Method defineClassProtectionDomain = null;
  227. // Set up the reflection-based Java2 methods if possible
  228. static {
  229. try {
  230. getProtectionDomain
  231. = Class.class.getMethod("getProtectionDomain", new Class[0]);
  232. Class protectionDomain
  233. = Class.forName("java.security.ProtectionDomain");
  234. Class[] args = new Class[] {String.class, byte[].class,
  235. Integer.TYPE, Integer.TYPE, protectionDomain};
  236. defineClassProtectionDomain
  237. = ClassLoader.class.getDeclaredMethod("defineClass", args);
  238. } catch (Exception e) {
  239. // ignore failure to get access to 1.2+ methods
  240. }
  241. }
  242. /**
  243. * Creates a classloader for the given project using the classpath given.
  244. *
  245. * @param project The project to which this classloader is to belong.
  246. * Must not be <code>null</code>.
  247. * @param classpath The classpath to use to load the classes. This
  248. * is combined with the system classpath in a manner
  249. * determined by the value of ${build.sysclasspath}.
  250. * May be <code>null</code>, in which case no path
  251. * elements are set up to start with.
  252. */
  253. public AntClassLoader(Project project, Path classpath) {
  254. parent = AntClassLoader.class.getClassLoader();
  255. this.project = project;
  256. project.addBuildListener(this);
  257. if (classpath != null) {
  258. Path actualClasspath = classpath.concatSystemClasspath("ignore");
  259. String[] pathElements = actualClasspath.list();
  260. for (int i = 0; i < pathElements.length; ++i) {
  261. try {
  262. addPathElement(pathElements[i]);
  263. } catch (BuildException e) {
  264. // ignore path elements which are invalid
  265. // relative to the project
  266. }
  267. }
  268. }
  269. }
  270. /**
  271. * Creates a classloader for the given project using the classpath given.
  272. *
  273. * @param parent The parent classloader to which unsatisfied loading
  274. * attempts are delegated. May be <code>null</code>,
  275. * in which case the classloader which loaded this
  276. * class is used as the parent.
  277. * @param project The project to which this classloader is to belong.
  278. * Must not be <code>null</code>.
  279. * @param classpath the classpath to use to load the classes.
  280. * May be <code>null</code>, in which case no path
  281. * elements are set up to start with.
  282. * @param parentFirst If <code>true</code>, indicates that the parent
  283. * classloader should be consulted before trying to
  284. * load the a class through this loader.
  285. */
  286. public AntClassLoader(ClassLoader parent, Project project, Path classpath,
  287. boolean parentFirst) {
  288. this(project, classpath);
  289. if (parent != null) {
  290. this.parent = parent;
  291. }
  292. this.parentFirst = parentFirst;
  293. addSystemPackageRoot("java");
  294. addSystemPackageRoot("javax");
  295. }
  296. /**
  297. * Creates a classloader for the given project using the classpath given.
  298. *
  299. * @param project The project to which this classloader is to belong.
  300. * Must not be <code>null</code>.
  301. * @param classpath The classpath to use to load the classes. May be
  302. * <code>null</code>, in which case no path
  303. * elements are set up to start with.
  304. * @param parentFirst If <code>true</code>, indicates that the parent
  305. * classloader should be consulted before trying to
  306. * load the a class through this loader.
  307. */
  308. public AntClassLoader(Project project, Path classpath,
  309. boolean parentFirst) {
  310. this(null, project, classpath, parentFirst);
  311. }
  312. /**
  313. * Creates an empty class loader. The classloader should be configured
  314. * with path elements to specify where the loader is to look for
  315. * classes.
  316. *
  317. * @param parent The parent classloader to which unsatisfied loading
  318. * attempts are delegated. May be <code>null</code>,
  319. * in which case the classloader which loaded this
  320. * class is used as the parent.
  321. * @param parentFirst If <code>true</code>, indicates that the parent
  322. * classloader should be consulted before trying to
  323. * load the a class through this loader.
  324. */
  325. public AntClassLoader(ClassLoader parent, boolean parentFirst) {
  326. if (parent != null) {
  327. this.parent = parent;
  328. } else {
  329. parent = AntClassLoader.class.getClassLoader();
  330. }
  331. project = null;
  332. this.parentFirst = parentFirst;
  333. }
  334. /**
  335. * Logs a message through the project object if one has been provided.
  336. *
  337. * @param message The message to log.
  338. * Should not be <code>null</code>.
  339. *
  340. * @param priority The logging priority of the message.
  341. */
  342. protected void log(String message, int priority) {
  343. if (project != null) {
  344. project.log(message, priority);
  345. }
  346. // else {
  347. // System.out.println(message);
  348. // }
  349. }
  350. /**
  351. * Sets the current thread's context loader to this classloader, storing
  352. * the current loader value for later resetting.
  353. */
  354. public void setThreadContextLoader() {
  355. if (isContextLoaderSaved) {
  356. throw new BuildException("Context loader has not been reset");
  357. }
  358. if (LoaderUtils.isContextLoaderAvailable()) {
  359. savedContextLoader = LoaderUtils.getContextClassLoader();
  360. ClassLoader loader = this;
  361. if ("only".equals(project.getProperty("build.sysclasspath"))) {
  362. loader = this.getClass().getClassLoader();
  363. }
  364. LoaderUtils.setContextClassLoader(loader);
  365. isContextLoaderSaved = true;
  366. }
  367. }
  368. /**
  369. * Resets the current thread's context loader to its original value.
  370. */
  371. public void resetThreadContextLoader() {
  372. if (LoaderUtils.isContextLoaderAvailable()
  373. && isContextLoaderSaved) {
  374. LoaderUtils.setContextClassLoader(savedContextLoader);
  375. savedContextLoader = null;
  376. isContextLoaderSaved = false;
  377. }
  378. }
  379. /**
  380. * Adds an element to the classpath to be searched.
  381. *
  382. * @param pathElement The path element to add. Must not be
  383. * <code>null</code>.
  384. *
  385. * @exception BuildException if the given path element cannot be resolved
  386. * against the project.
  387. */
  388. public void addPathElement(String pathElement) throws BuildException {
  389. File pathComponent
  390. = project != null ? project.resolveFile(pathElement)
  391. : new File(pathElement);
  392. pathComponents.addElement(pathComponent);
  393. }
  394. /**
  395. * Returns the classpath this classloader will consult.
  396. *
  397. * @return the classpath used for this classloader, with elements
  398. * separated by the path separator for the system.
  399. */
  400. public String getClasspath(){
  401. StringBuffer sb = new StringBuffer();
  402. boolean firstPass = true;
  403. Enumeration enum = pathComponents.elements();
  404. while (enum.hasMoreElements()) {
  405. if (!firstPass) {
  406. sb.append(System.getProperty("path.separator"));
  407. } else {
  408. firstPass = false;
  409. }
  410. sb.append(((File) enum.nextElement()).getAbsolutePath());
  411. }
  412. return sb.toString();
  413. }
  414. /**
  415. * Sets whether this classloader should run in isolated mode. In
  416. * isolated mode, classes not found on the given classpath will
  417. * not be referred to the parent class loader but will cause a
  418. * ClassNotFoundException.
  419. *
  420. * @param isolated Whether or not this classloader should run in
  421. * isolated mode.
  422. */
  423. public void setIsolated(boolean isolated) {
  424. ignoreBase = isolated;
  425. }
  426. /**
  427. * Forces initialization of a class in a JDK 1.1 compatible, albeit hacky
  428. * way.
  429. *
  430. * @param theClass The class to initialize.
  431. * Must not be <code>null</code>.
  432. */
  433. public static void initializeClass(Class theClass) {
  434. // ***HACK*** We ask the VM to create an instance
  435. // by voluntarily providing illegal arguments to force
  436. // the VM to run the class' static initializer, while
  437. // at the same time not running a valid constructor.
  438. final Constructor[] cons = theClass.getDeclaredConstructors();
  439. //At least one constructor is guaranteed to be there, but check anyway.
  440. if (cons != null) {
  441. if (cons.length > 0 && cons[0] != null) {
  442. final String[] strs = new String[256];
  443. try {
  444. cons[0].newInstance(strs);
  445. // Expecting an exception to be thrown by this call:
  446. // IllegalArgumentException: wrong number of Arguments
  447. } catch (Throwable t) {
  448. // Ignore - we are interested only in the side
  449. // effect - that of getting the static initializers
  450. // invoked. As we do not want to call a valid
  451. // constructor to get this side effect, an
  452. // attempt is made to call a hopefully
  453. // invalid constructor - come on, nobody
  454. // would have a constructor that takes in
  455. // 256 String arguments ;-)
  456. // (In fact, they can't - according to JVM spec
  457. // section 4.10, the number of method parameters is limited
  458. // to 255 by the definition of a method descriptor.
  459. // Constructors count as methods here.)
  460. }
  461. }
  462. }
  463. }
  464. /**
  465. * Adds a package root to the list of packages which must be loaded on the
  466. * parent loader.
  467. *
  468. * All subpackages are also included.
  469. *
  470. * @param packageRoot The root of all packages to be included.
  471. * Should not be <code>null</code>.
  472. */
  473. public void addSystemPackageRoot(String packageRoot) {
  474. systemPackages.addElement(packageRoot + ".");
  475. }
  476. /**
  477. * Adds a package root to the list of packages which must be loaded using
  478. * this loader.
  479. *
  480. * All subpackages are also included.
  481. *
  482. * @param packageRoot The root of all packages to be included.
  483. * Should not be <code>null</code>.
  484. */
  485. public void addLoaderPackageRoot(String packageRoot) {
  486. loaderPackages.addElement(packageRoot + ".");
  487. }
  488. /**
  489. * Loads a class through this class loader even if that class is available
  490. * on the parent classpath.
  491. *
  492. * This ensures that any classes which are loaded by the returned class
  493. * will use this classloader.
  494. *
  495. * @param classname The name of the class to be loaded.
  496. * Must not be <code>null</code>.
  497. *
  498. * @return the required Class object
  499. *
  500. * @exception ClassNotFoundException if the requested class does not exist
  501. * on this loader's classpath.
  502. */
  503. public Class forceLoadClass(String classname)
  504. throws ClassNotFoundException {
  505. log("force loading " + classname, Project.MSG_DEBUG);
  506. Class theClass = findLoadedClass(classname);
  507. if (theClass == null) {
  508. theClass = findClass(classname);
  509. }
  510. return theClass;
  511. }
  512. /**
  513. * Loads a class through this class loader but defer to the parent class
  514. * loader.
  515. *
  516. * This ensures that instances of the returned class will be compatible
  517. * with instances which which have already been loaded on the parent
  518. * loader.
  519. *
  520. * @param classname The name of the class to be loaded.
  521. * Must not be <code>null</code>.
  522. *
  523. * @return the required Class object
  524. *
  525. * @exception ClassNotFoundException if the requested class does not exist
  526. * on this loader's classpath.
  527. */
  528. public Class forceLoadSystemClass(String classname)
  529. throws ClassNotFoundException {
  530. log("force system loading " + classname, Project.MSG_DEBUG);
  531. Class theClass = findLoadedClass(classname);
  532. if (theClass == null) {
  533. theClass = findBaseClass(classname);
  534. }
  535. return theClass;
  536. }
  537. /**
  538. * Returns a stream to read the requested resource name.
  539. *
  540. * @param name The name of the resource for which a stream is required.
  541. * Must not be <code>null</code>.
  542. *
  543. * @return a stream to the required resource or <code>null</code> if the
  544. * resource cannot be found on the loader's classpath.
  545. */
  546. public InputStream getResourceAsStream(String name) {
  547. InputStream resourceStream = null;
  548. if (isParentFirst(name)) {
  549. resourceStream = loadBaseResource(name);
  550. if (resourceStream != null) {
  551. log("ResourceStream for " + name
  552. + " loaded from parent loader", Project.MSG_DEBUG);
  553. } else {
  554. resourceStream = loadResource(name);
  555. if (resourceStream != null) {
  556. log("ResourceStream for " + name
  557. + " loaded from ant loader", Project.MSG_DEBUG);
  558. }
  559. }
  560. } else {
  561. resourceStream = loadResource(name);
  562. if (resourceStream != null) {
  563. log("ResourceStream for " + name
  564. + " loaded from ant loader", Project.MSG_DEBUG);
  565. } else {
  566. resourceStream = loadBaseResource(name);
  567. if (resourceStream != null) {
  568. log("ResourceStream for " + name
  569. + " loaded from parent loader", Project.MSG_DEBUG);
  570. }
  571. }
  572. }
  573. if (resourceStream == null) {
  574. log("Couldn't load ResourceStream for " + name,
  575. Project.MSG_DEBUG);
  576. }
  577. return resourceStream;
  578. }
  579. /**
  580. * Returns a stream to read the requested resource name from this loader.
  581. *
  582. * @param name The name of the resource for which a stream is required.
  583. * Must not be <code>null</code>.
  584. *
  585. * @return a stream to the required resource or <code>null</code> if
  586. * the resource cannot be found on the loader's classpath.
  587. */
  588. private InputStream loadResource(String name) {
  589. // we need to search the components of the path to see if we can
  590. // find the class we want.
  591. InputStream stream = null;
  592. Enumeration e = pathComponents.elements();
  593. while (e.hasMoreElements() && stream == null) {
  594. File pathComponent = (File)e.nextElement();
  595. stream = getResourceStream(pathComponent, name);
  596. }
  597. return stream;
  598. }
  599. /**
  600. * Finds a system resource (which should be loaded from the parent
  601. * classloader).
  602. *
  603. * @param name The name of the system resource to load.
  604. * Must not be <code>null</code>.
  605. *
  606. * @return a stream to the named resource, or <code>null</code> if
  607. * the resource cannot be found.
  608. */
  609. private InputStream loadBaseResource(String name) {
  610. if (parent == null) {
  611. return getSystemResourceAsStream(name);
  612. } else {
  613. return parent.getResourceAsStream(name);
  614. }
  615. }
  616. /**
  617. * Returns an inputstream to a given resource in the given file which may
  618. * either be a directory or a zip file.
  619. *
  620. * @param file the file (directory or jar) in which to search for the
  621. * resource. Must not be <code>null</code>.
  622. * @param resourceName The name of the resource for which a stream is
  623. * required. Must not be <code>null</code>.
  624. *
  625. * @return a stream to the required resource or <code>null</code> if
  626. * the resource cannot be found in the given file.
  627. */
  628. private InputStream getResourceStream(File file, String resourceName) {
  629. try {
  630. if (!file.exists()) {
  631. return null;
  632. }
  633. if (file.isDirectory()) {
  634. File resource = new File(file, resourceName);
  635. if (resource.exists()) {
  636. return new FileInputStream(resource);
  637. }
  638. } else {
  639. // is the zip file in the cache
  640. ZipFile zipFile = (ZipFile)zipFiles.get(file);
  641. if (zipFile == null) {
  642. zipFile = new ZipFile(file);
  643. zipFiles.put(file, zipFile);
  644. }
  645. ZipEntry entry = zipFile.getEntry(resourceName);
  646. if (entry != null) {
  647. return zipFile.getInputStream(entry);
  648. }
  649. }
  650. } catch (Exception e) {
  651. log("Ignoring Exception " + e.getClass().getName()
  652. + ": " + e.getMessage() + " reading resource " + resourceName
  653. + " from " + file, Project.MSG_VERBOSE);
  654. }
  655. return null;
  656. }
  657. /**
  658. * Tests whether or not the parent classloader should be checked for
  659. * a resource before this one. If the resource matches both the
  660. * "use parent classloader first" and the "use this classloader first"
  661. * lists, the latter takes priority.
  662. *
  663. * @param resourceName The name of the resource to check.
  664. * Must not be <code>null</code>.
  665. *
  666. * @return whether or not the parent classloader should be checked for a
  667. * resource before this one is.
  668. */
  669. private boolean isParentFirst(String resourceName) {
  670. // default to the global setting and then see
  671. // if this class belongs to a package which has been
  672. // designated to use a specific loader first
  673. // (this one or the parent one)
  674. // XXX - shouldn't this always return false in isolated mode?
  675. boolean useParentFirst = parentFirst;
  676. for (Enumeration e = systemPackages.elements(); e.hasMoreElements();) {
  677. String packageName = (String)e.nextElement();
  678. if (resourceName.startsWith(packageName)) {
  679. useParentFirst = true;
  680. break;
  681. }
  682. }
  683. for (Enumeration e = loaderPackages.elements(); e.hasMoreElements();) {
  684. String packageName = (String)e.nextElement();
  685. if (resourceName.startsWith(packageName)) {
  686. useParentFirst = false;
  687. break;
  688. }
  689. }
  690. return useParentFirst;
  691. }
  692. /**
  693. * Finds the resource with the given name. A resource is
  694. * some data (images, audio, text, etc) that can be accessed by class
  695. * code in a way that is independent of the location of the code.
  696. *
  697. * @param name The name of the resource for which a stream is required.
  698. * Must not be <code>null</code>.
  699. *
  700. * @return a URL for reading the resource, or <code>null</code> if the
  701. * resource could not be found or the caller doesn't have
  702. * adequate privileges to get the resource.
  703. */
  704. public URL getResource(String name) {
  705. // we need to search the components of the path to see if
  706. // we can find the class we want.
  707. URL url = null;
  708. if (isParentFirst(name)) {
  709. url = (parent == null) ? super.getResource(name)
  710. : parent.getResource(name);
  711. }
  712. if (url != null) {
  713. log("Resource " + name + " loaded from parent loader",
  714. Project.MSG_DEBUG);
  715. } else {
  716. // try and load from this loader if the parent either didn't find
  717. // it or wasn't consulted.
  718. Enumeration e = pathComponents.elements();
  719. while (e.hasMoreElements() && url == null) {
  720. File pathComponent = (File)e.nextElement();
  721. url = getResourceURL(pathComponent, name);
  722. if (url != null) {
  723. log("Resource " + name
  724. + " loaded from ant loader",
  725. Project.MSG_DEBUG);
  726. }
  727. }
  728. }
  729. if (url == null && !isParentFirst(name)) {
  730. // this loader was first but it didn't find it - try the parent
  731. url = (parent == null) ? super.getResource(name)
  732. : parent.getResource(name);
  733. if (url != null) {
  734. log("Resource " + name + " loaded from parent loader",
  735. Project.MSG_DEBUG);
  736. }
  737. }
  738. if (url == null) {
  739. log("Couldn't load Resource " + name, Project.MSG_DEBUG);
  740. }
  741. return url;
  742. }
  743. /**
  744. * Returns an enumeration of URLs representing all the resources with the
  745. * given name by searching the class loader's classpath.
  746. *
  747. * @param name The resource name to search for.
  748. * Must not be <code>null</code>.
  749. * @return an enumeration of URLs for the resources
  750. * @exception IOException if I/O errors occurs (can't happen)
  751. */
  752. protected Enumeration findResources(String name) throws IOException {
  753. return new ResourceEnumeration(name);
  754. }
  755. /**
  756. * Returns an inputstream to a given resource in the given file which may
  757. * either be a directory or a zip file.
  758. *
  759. * @param file The file (directory or jar) in which to search for
  760. * the resource. Must not be <code>null</code>.
  761. * @param resourceName The name of the resource for which a stream
  762. * is required. Must not be <code>null</code>.
  763. *
  764. * @return a stream to the required resource or <code>null</code> if the
  765. * resource cannot be found in the given file object.
  766. */
  767. private URL getResourceURL(File file, String resourceName) {
  768. try {
  769. if (!file.exists()) {
  770. return null;
  771. }
  772. if (file.isDirectory()) {
  773. File resource = new File(file, resourceName);
  774. if (resource.exists()) {
  775. try {
  776. return new URL("file:" + resource.toString());
  777. } catch (MalformedURLException ex) {
  778. return null;
  779. }
  780. }
  781. } else {
  782. ZipFile zipFile = (ZipFile)zipFiles.get(file);
  783. if (zipFile == null) {
  784. zipFile = new ZipFile(file);
  785. zipFiles.put(file, zipFile);
  786. }
  787. ZipEntry entry = zipFile.getEntry(resourceName);
  788. if (entry != null) {
  789. try {
  790. return new URL("jar:file:" + file.toString()
  791. + "!/" + entry);
  792. } catch (MalformedURLException ex) {
  793. return null;
  794. }
  795. }
  796. }
  797. } catch (Exception e) {
  798. e.printStackTrace();
  799. }
  800. return null;
  801. }
  802. /**
  803. * Loads a class with this class loader.
  804. *
  805. * This class attempts to load the class in an order determined by whether
  806. * or not the class matches the system/loader package lists, with the
  807. * loader package list taking priority. If the classloader is in isolated
  808. * mode, failure to load the class in this loader will result in a
  809. * ClassNotFoundException.
  810. *
  811. * @param classname The name of the class to be loaded.
  812. * Must not be <code>null</code>.
  813. * @param resolve <code>true</code> if all classes upon which this class
  814. * depends are to be loaded.
  815. *
  816. * @return the required Class object
  817. *
  818. * @exception ClassNotFoundException if the requested class does not exist
  819. * on the system classpath (when not in isolated mode) or this loader's
  820. * classpath.
  821. */
  822. protected Class loadClass(String classname, boolean resolve)
  823. throws ClassNotFoundException {
  824. Class theClass = findLoadedClass(classname);
  825. if (theClass != null) {
  826. return theClass;
  827. }
  828. if (isParentFirst(classname)) {
  829. try {
  830. theClass = findBaseClass(classname);
  831. log("Class " + classname + " loaded from parent loader",
  832. Project.MSG_DEBUG);
  833. } catch (ClassNotFoundException cnfe) {
  834. theClass = findClass(classname);
  835. log("Class " + classname + " loaded from ant loader",
  836. Project.MSG_DEBUG);
  837. }
  838. } else {
  839. try {
  840. theClass = findClass(classname);
  841. log("Class " + classname + " loaded from ant loader",
  842. Project.MSG_DEBUG);
  843. } catch (ClassNotFoundException cnfe) {
  844. if (ignoreBase) {
  845. throw cnfe;
  846. }
  847. theClass = findBaseClass(classname);
  848. log("Class " + classname + " loaded from parent loader",
  849. Project.MSG_DEBUG);
  850. }
  851. }
  852. if (resolve) {
  853. resolveClass(theClass);
  854. }
  855. return theClass;
  856. }
  857. /**
  858. * Converts the class dot notation to a filesystem equivalent for
  859. * searching purposes.
  860. *
  861. * @param classname The class name in dot format (eg java.lang.Integer).
  862. * Must not be <code>null</code>.
  863. *
  864. * @return the classname in filesystem format (eg java/lang/Integer.class)
  865. */
  866. private String getClassFilename(String classname) {
  867. return classname.replace('.', '/') + ".class";
  868. }
  869. /**
  870. * Reads a class definition from a stream.
  871. *
  872. * @param stream The stream from which the class is to be read.
  873. * Must not be <code>null</code>.
  874. * @param classname The name of the class in the stream.
  875. * Must not be <code>null</code>.
  876. *
  877. * @return the Class object read from the stream.
  878. *
  879. * @exception IOException if there is a problem reading the class from the
  880. * stream.
  881. * @exception SecurityException if there is a security problem while
  882. * reading the class from the stream.
  883. */
  884. private Class getClassFromStream(InputStream stream, String classname)
  885. throws IOException, SecurityException {
  886. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  887. int bytesRead = -1;
  888. byte[] buffer = new byte[BUFFER_SIZE];
  889. while ((bytesRead = stream.read(buffer, 0, BUFFER_SIZE)) != -1) {
  890. baos.write(buffer, 0, bytesRead);
  891. }
  892. byte[] classData = baos.toByteArray();
  893. // Simply put:
  894. // defineClass(classname, classData, 0, classData.length,
  895. // Project.class.getProtectionDomain());
  896. // Made more elaborate to be 1.1-safe.
  897. if (defineClassProtectionDomain != null) {
  898. try {
  899. Object domain
  900. = getProtectionDomain.invoke(Project.class, new Object[0]);
  901. Object[] args
  902. = new Object[] {classname, classData, new Integer(0),
  903. new Integer(classData.length), domain};
  904. return (Class)defineClassProtectionDomain.invoke(this, args);
  905. } catch (InvocationTargetException ite) {
  906. Throwable t = ite.getTargetException();
  907. if (t instanceof ClassFormatError) {
  908. throw (ClassFormatError)t;
  909. } else if (t instanceof NoClassDefFoundError) {
  910. throw (NoClassDefFoundError)t;
  911. }
  912. else if (t instanceof SecurityException) {
  913. throw (SecurityException)t;
  914. }
  915. else {
  916. throw new IOException(t.toString());
  917. }
  918. } catch (Exception e) {
  919. throw new IOException(e.toString());
  920. }
  921. } else {
  922. return defineClass(classname, classData, 0, classData.length);
  923. }
  924. }
  925. /**
  926. * Searches for and load a class on the classpath of this class loader.
  927. *
  928. * @param name The name of the class to be loaded. Must not be
  929. * <code>null</code>.
  930. *
  931. * @return the required Class object
  932. *
  933. * @exception ClassNotFoundException if the requested class does not exist
  934. * on this loader's classpath.
  935. */
  936. public Class findClass(String name) throws ClassNotFoundException {
  937. log("Finding class " + name, Project.MSG_DEBUG);
  938. return findClassInComponents(name);
  939. }
  940. /**
  941. * Finds a class on the given classpath.
  942. *
  943. * @param name The name of the class to be loaded. Must not be
  944. * <code>null</code>.
  945. *
  946. * @return the required Class object
  947. *
  948. * @exception ClassNotFoundException if the requested class does not exist
  949. * on this loader's classpath.
  950. */
  951. private Class findClassInComponents(String name)
  952. throws ClassNotFoundException {
  953. // we need to search the components of the path to see if
  954. // we can find the class we want.
  955. InputStream stream = null;
  956. String classFilename = getClassFilename(name);
  957. try {
  958. Enumeration e = pathComponents.elements();
  959. while (e.hasMoreElements()) {
  960. File pathComponent = (File)e.nextElement();
  961. try {
  962. stream = getResourceStream(pathComponent, classFilename);
  963. if (stream != null) {
  964. return getClassFromStream(stream, name);
  965. }
  966. }
  967. catch (SecurityException se) {
  968. throw se;
  969. }
  970. catch (IOException ioe) {
  971. // ioe.printStackTrace();
  972. log("Exception reading component " + pathComponent ,
  973. Project.MSG_VERBOSE);
  974. }
  975. }
  976. throw new ClassNotFoundException(name);
  977. } finally {
  978. try {
  979. if (stream != null) {
  980. stream.close();
  981. }
  982. } catch (IOException e) {}
  983. }
  984. }
  985. /**
  986. * Finds a system class (which should be loaded from the same classloader
  987. * as the Ant core).
  988. *
  989. * For JDK 1.1 compatability, this uses the findSystemClass method if
  990. * no parent classloader has been specified.
  991. *
  992. * @param name The name of the class to be loaded.
  993. * Must not be <code>null</code>.
  994. *
  995. * @return the required Class object
  996. *
  997. * @exception ClassNotFoundException if the requested class does not exist
  998. * on this loader's classpath.
  999. */
  1000. private Class findBaseClass(String name) throws ClassNotFoundException {
  1001. if (parent == null) {
  1002. return findSystemClass(name);
  1003. } else {
  1004. return parent.loadClass(name);
  1005. }
  1006. }
  1007. /**
  1008. * Cleans up any resources held by this classloader. Any open archive
  1009. * files are closed.
  1010. */
  1011. public void cleanup() {
  1012. pathComponents = null;
  1013. project = null;
  1014. for (Enumeration e = zipFiles.elements(); e.hasMoreElements(); ) {
  1015. ZipFile zipFile = (ZipFile)e.nextElement();
  1016. try {
  1017. zipFile.close();
  1018. } catch (IOException ioe) {
  1019. // ignore
  1020. }
  1021. }
  1022. zipFiles = new Hashtable();
  1023. }
  1024. /**
  1025. * Empty implementation to satisfy the BuildListener interface.
  1026. *
  1027. * @param event the buildStarted event
  1028. */
  1029. public void buildStarted(BuildEvent event) {
  1030. }
  1031. /**
  1032. * Cleans up any resources held by this classloader at the end
  1033. * of a build.
  1034. *
  1035. * @param event the buildFinished event
  1036. */
  1037. public void buildFinished(BuildEvent event) {
  1038. cleanup();
  1039. }
  1040. /**
  1041. * Empty implementation to satisfy the BuildListener interface.
  1042. *
  1043. * @param event the targetStarted event
  1044. */
  1045. public void targetStarted(BuildEvent event) {
  1046. }
  1047. /**
  1048. * Empty implementation to satisfy the BuildListener interface.
  1049. *
  1050. * @param event the targetFinished event
  1051. */
  1052. public void targetFinished(BuildEvent event) {
  1053. }
  1054. /**
  1055. * Empty implementation to satisfy the BuildListener interface.
  1056. *
  1057. * @param event the taskStarted event
  1058. */
  1059. public void taskStarted(BuildEvent event) {
  1060. }
  1061. /**
  1062. * Empty implementation to satisfy the BuildListener interface.
  1063. *
  1064. * @param event the taskFinished event
  1065. */
  1066. public void taskFinished(BuildEvent event) {
  1067. }
  1068. /**
  1069. * Empty implementation to satisfy the BuildListener interface.
  1070. *
  1071. * @param event the messageLogged event
  1072. */
  1073. public void messageLogged(BuildEvent event) {
  1074. }
  1075. }