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     * <p>
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     * <p>
034     *  Renamed the former "system class loader" to the "bootstrap class loader".
035     */
036    public class ApplicationClassLoader extends URLClassLoader {
037    
038      static final boolean DBG = false;
039    
040      static int numInstantiations = 0;
041    
042      /** For status printing, to make sure that, if an application class loader is
043       *  created at boot image writing time, it won't leak out into the next
044       *  mode.   This is actually not used any more, but it should never hurt,
045       *  and can give one a sense of confidence when debugging Jikes RVM's
046       *  classloaders.
047       *  */
048      private final boolean createdAtBootImageWritingTime;
049      private final boolean createdWithRunningVM;
050    
051      public ApplicationClassLoader(String specifiedClasspath) {
052        super(new URL[0]);
053        if (DBG) {
054          VM.sysWriteln("The Application Class Loader has been instantiated ", numInstantiations, " times");
055        }
056        ++numInstantiations;
057    
058        createdAtBootImageWritingTime = VM.writingBootImage;
059        createdWithRunningVM = VM.runningVM;
060    
061        try {
062          if (specifiedClasspath == null) {
063            addURL(new URL("file", null, -1, System.getProperty("user.dir") + File.separator));
064          } else {
065            StringTokenizer tok = new StringTokenizer(specifiedClasspath, File.pathSeparator);
066            while (tok.hasMoreElements()) {
067              String elt = tok.nextToken();
068    
069              if (!(elt.endsWith(".jar") || elt.endsWith(".zip"))) {
070                if (!elt.endsWith(File.separator)) {
071                  elt += File.separator;
072                }
073              }
074    
075              if (elt.indexOf(File.pathSeparatorChar) != -1) {
076                addURL(new URL(elt));
077              } else if (elt.startsWith(File.separator)) {
078                addURL(new URL("file", null, -1, elt));
079              } else {
080                addURL(new URL("file", null, -1, System.getProperty("user.dir") + File.separator + elt));
081              }
082            }
083          }
084        } catch (MalformedURLException e) {
085          VM.sysFail(
086              "JikesRVM: ApplicationClassLoader: Initialization Failed with a MalformedURLException; there was an error setting the application's classpath: " +
087              e);
088        }
089      }
090    
091      /** Name of the Application Class Loader.  Actually used by Jikes RVM's
092       * serialization code.
093       * <P>
094       * I intended this name to reflect both "SystemClassLoader" and
095       * "ApplicationClassLoader".
096       */
097      public static final String myName = "SystemAppCL";
098    
099      @Override
100      public String toString() {
101        return myName +
102               (createdAtBootImageWritingTime ? "-createdAtBootImageWritingTime" : "") +
103               (createdWithRunningVM ? "" : "-NOTcreatedWithRunningVM") +
104               (DBG ? "@" + VM.addressAsHexString(Magic.objectAsAddress(this)) : "");
105      }
106    
107      @Override
108      protected String findLibrary(String libName) {
109        return null;
110      }
111    }
112    
113