PageRenderTime 224ms CodeModel.GetById 216ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 1ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/classpath/BshClassLoader.java

#
Java | 196 lines | 66 code | 28 blank | 102 comment | 15 complexity | 72fa2bf43f6cc089c0e82358998c2180 MD5 | raw file
  1/*****************************************************************************
  2 *                                                                           *
  3 *  This file is part of the BeanShell Java Scripting distribution.          *
  4 *  Documentation and updates may be found at http://www.beanshell.org/      *
  5 *                                                                           *
  6 *  Sun Public License Notice:                                               *
  7 *                                                                           *
  8 *  The contents of this file are subject to the Sun Public License Version  *
  9 *  1.0 (the "License"); you may not use this file except in compliance with *
 10 *  the License. A copy of the License is available at http://www.sun.com    *
 11 *                                                                           *
 12 *  The Original Code is BeanShell. The Initial Developer of the Original    *
 13 *  Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright     *
 14 *  (C) 2000.  All Rights Reserved.                                          *
 15 *                                                                           *
 16 *  GNU Public License Notice:                                               *
 17 *                                                                           *
 18 *  Alternatively, the contents of this file may be used under the terms of  *
 19 *  the GNU Lesser General Public License (the "LGPL"), in which case the    *
 20 *  provisions of LGPL are applicable instead of those above. If you wish to *
 21 *  allow use of your version of this file only under the  terms of the LGPL *
 22 *  and not to allow others to use your version of this file under the SPL,  *
 23 *  indicate your decision by deleting the provisions above and replace      *
 24 *  them with the notice and other provisions required by the LGPL.  If you  *
 25 *  do not delete the provisions above, a recipient may use your version of  *
 26 *  this file under either the SPL or the LGPL.                              *
 27 *                                                                           *
 28 *  Patrick Niemeyer (pat@pat.net)                                           *
 29 *  Author of Learning Java, O'Reilly & Associates                           *
 30 *  http://www.pat.net/~pat/                                                 *
 31 *                                                                           *
 32 *****************************************************************************/
 33
 34package org.gjt.sp.jedit.bsh.classpath;
 35
 36import java.net.*;
 37
 38import org.gjt.sp.jedit.bsh.BshClassManager;
 39
 40/**
 41    One of the things BshClassLoader does is to address a deficiency in
 42    URLClassLoader that prevents us from specifying individual classes
 43    via URLs.
 44*/
 45public class BshClassLoader extends URLClassLoader
 46{
 47    BshClassManager classManager;
 48
 49    /**
 50        @param bases URLs JARClassLoader seems to require absolute paths
 51    */
 52    public BshClassLoader( BshClassManager classManager, URL [] bases ) {
 53        super( bases );
 54        this.classManager = classManager;
 55    }
 56
 57    /**
 58        @param bcp URLs JARClassLoader seems to require absolute paths
 59    */
 60    public BshClassLoader( BshClassManager classManager, BshClassPath bcp ) {
 61        this( classManager, bcp.getPathComponents() );
 62    }
 63
 64    /**
 65        For use by children
 66        @param classManager URLs JARClassLoader seems to require absolute paths
 67    */
 68    protected BshClassLoader( BshClassManager classManager ) {
 69        this( classManager, new URL [] { } );
 70    }
 71
 72    // public version of addURL
 73    public void addURL( URL url ) {
 74        super.addURL( url );
 75    }
 76
 77    /**
 78        This modification allows us to reload classes which are in the
 79        Java VM user classpath.  We search first rather than delegate to
 80        the parent classloader (or bootstrap path) first.
 81
 82        An exception is for BeanShell core classes which are always loaded from
 83        the same classloader as the interpreter.
 84    */
 85    public Class loadClass(String name, boolean resolve)
 86        throws ClassNotFoundException
 87    {
 88        Class c = null;
 89
 90        /*
 91            Check first for classes loaded through this loader.
 92            The VM will not allow a class to be loaded twice.
 93        */
 94        c = findLoadedClass(name);
 95        if ( c != null )
 96            return c;
 97
 98// This is copied from ClassManagerImpl
 99// We should refactor this somehow if it sticks around
100        if ( name.startsWith( ClassManagerImpl.BSH_PACKAGE ) )
101            try {
102                return org.gjt.sp.jedit.bsh.Interpreter.class.getClassLoader().loadClass( name );
103            } catch ( ClassNotFoundException e ) {}
104
105        /*
106            Try to find the class using our classloading mechanism.
107            Note: I wish we didn't have to catch the exception here... slow
108        */
109        try {
110            c = findClass( name );
111        } catch ( ClassNotFoundException e ) { }
112
113        if ( c == null )
114            throw new ClassNotFoundException("here in loaClass");
115
116        if ( resolve )
117            resolveClass( c );
118
119        return c;
120    }
121
122    /**
123        Find the correct source for the class...
124
125        Try designated loader if any
126        Try our URLClassLoader paths if any
127        Try base loader if any
128        Try system ???
129    */
130    // add some caching for not found classes?
131    protected Class findClass( String name )
132        throws ClassNotFoundException
133    {
134        // Deal with this cast somehow... maybe have this class use
135        // ClassManagerImpl type directly.
136        // Don't add the method to BshClassManager... it's really an impl thing
137        ClassManagerImpl bcm = (ClassManagerImpl)getClassManager();
138
139        // Should we try to load the class ourselves or delegate?
140        // look for overlay loader
141
142        // Deal with this cast somehow... maybe have this class use
143        // ClassManagerImpl type directly.
144        // Don't add the method to BshClassManager... it's really an impl thing
145        ClassLoader cl = bcm.getLoaderForClass( name );
146
147        Class c;
148
149        // If there is a designated loader and it's not us delegate to it
150        if ( cl != null && cl != this )
151            try {
152                return cl.loadClass( name );
153            } catch ( ClassNotFoundException e ) {
154                throw new ClassNotFoundException(
155                    "Designated loader could not find class: "+e );
156            }
157
158        // Let URLClassLoader try any paths it may have
159        if ( getURLs().length > 0 )
160            try {
161                return super.findClass(name);
162            } catch ( ClassNotFoundException e ) {
163                //System.out.println(
164                //	"base loader here caught class not found: "+name );
165            }
166
167
168        // If there is a baseLoader and it's not us delegate to it
169        cl = bcm.getBaseLoader();
170
171        if ( cl != null && cl != this )
172            try {
173                return cl.loadClass( name );
174            } catch ( ClassNotFoundException e ) { }
175
176        // Try system loader
177        return bcm.plainClassForName( name );
178    }
179
180    /*
181        The superclass does something like this
182
183        c = findLoadedClass(name);
184        if null
185            try
186                if parent not null
187                    c = parent.loadClass(name, false);
188                else
189                    c = findBootstrapClass(name);
190            catch ClassNotFoundException
191                c = findClass(name);
192    */
193
194    BshClassManager getClassManager() { return classManager; }
195
196}