001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.classloader;
014    
015    import java.io.File;
016    import java.net.MalformedURLException;
017    import java.net.URL;
018    import java.net.URLClassLoader;
019    import java.util.StringTokenizer;
020    import org.jikesrvm.VM;
021    import org.jikesrvm.runtime.Magic;
022    
023    /**
024     * The class loader used by Jikes RVM to load the application program.  Since
025     * version 1.2 of the Sun API docs, the Application ClassLoader  and the
026     * System Class Loader are officially the same thing.  (What Jikes RVM used to
027     * call the "System Class Loader" is officially the "Bootstrap Class
028     * Loader".)
029     *
030     * We use a two-link chain.  An ordinary user's class is loaded by this class
031     * loader.  This class loader first delegates to its parent (the Bootstrap
032     * Class Loader) before trying the class itself.
033     *
034     *
035     *  Renamed the former "system class loader" to the "bootstrap class loader".
036     */
037    public class ApplicationClassLoader extends URLClassLoader {
038    
039      static final boolean DBG = false;
040    
041      static int numInstantiations = 0;
042    
043      /** For status printing, to make sure that, if an application class loader is
044       *  created at boot image writing time, it won't leak out into the next
045       *  mode.   This is actually not used any more, but it should never hurt,
046       *  and can give one a sense of confidence when debugging Jikes RVM's
047       *  classloaders.
048       *  */
049      private final boolean createdAtBootImageWritingTime;
050      private final boolean createdWithRunningVM;
051    
052      public ApplicationClassLoader(String specifiedClasspath) {
053        super(new URL[0]);
054        if (DBG) {
055          VM.sysWriteln("The Application Class Loader has been instantiated ", numInstantiations, " times");
056        }
057        ++numInstantiations;
058    
059        createdAtBootImageWritingTime = VM.writingBootImage;
060        createdWithRunningVM = VM.runningVM;
061    
062        try {
063          if (specifiedClasspath == null) {
064            addURL(new URL("file", null, -1, System.getProperty("user.dir") + File.separator));
065          } else {
066            StringTokenizer tok = new StringTokenizer(specifiedClasspath, File.pathSeparator);
067            while (tok.hasMoreElements()) {
068              String elt = tok.nextToken();
069    
070              if (!(elt.endsWith(".jar") || elt.endsWith(".zip"))) {
071                if (!elt.endsWith(File.separator)) {
072                  elt += File.separator;
073                }
074              }
075    
076              if (elt.indexOf(File.pathSeparatorChar) != -1) {
077                addURL(new URL(elt));
078              } else if (elt.startsWith(File.separator)) {
079                addURL(new URL("file", null, -1, elt));
080              } else {
081                addURL(new URL("file", null, -1, System.getProperty("user.dir") + File.separator + elt));
082              }
083            }
084          }
085        } catch (MalformedURLException e) {
086          VM.sysFail(
087              "JikesRVM: ApplicationClassLoader: Initialization Failed with a MalformedURLException; there was an error setting the application's classpath: " +
088              e);
089        }
090      }
091    
092      /** Name of the Application Class Loader.  Actually used by Jikes RVM's
093       * serialization code.
094       * <P>
095       * I intended this name to reflect both "SystemClassLoader" and
096       * "ApplicationClassLoader".
097       */
098      public static final String myName = "SystemAppCL";
099    
100      public String toString() {
101        return myName +
102               (createdAtBootImageWritingTime ? "-createdAtBootImageWritingTime" : "") +
103               (createdWithRunningVM ? "" : "-NOTcreatedWithRunningVM") +
104               (DBG ? "@" + VM.addressAsHexString(Magic.objectAsAddress(this)) : "");
105      }
106    
107      protected String findLibrary(String libName) {
108        return null;
109      }
110    }
111    
112