Annotation of java/classes/org/w3c/jigsaw/servlet/ServletLoader.java, revision 1.7

1.2       abaird      1: // ServletLoader.java
1.7     ! bmahe       2: // $Id: ServletLoader.java,v 1.6 1998/01/22 14:11:43 bmahe Exp $
1.2       abaird      3: // (c) COPYRIGHT MIT and INRIA, 1996.
                      4: // Please first read the full copyright statement in file COPYRIGHT.html
                      5: 
1.6       bmahe       6: package org.w3c.jigsaw.servlet;
1.1       abaird      7: 
                      8: import java.io.*;
                      9: import java.net.*;
1.4       abaird     10: import java.util.*;
1.2       abaird     11: 
                     12: /**
                     13:  * @author Alexandre Rafalovitch <alex@access.com.au>
                     14:  * @author Anselm Baird-Smith <abaird@w3.org>
                     15:  */
1.1       abaird     16: 
                     17: public class ServletLoader extends ClassLoader {
1.7     ! bmahe      18:     private static final boolean debug = false;
1.3       abaird     19:     private Hashtable classes = null;
1.1       abaird     20: 
                     21:     RemoteServletWrapper wrapper = null;
                     22: 
                     23:     private void trace(String msg) {
1.5       abaird     24:        System.out.println("["+wrapper.getURLPath()+"]: "+msg);
1.1       abaird     25:     }
                     26: 
                     27:     /**
                     28:      * Given the class name, return its URL.
                     29:      * @param name The class to be loaded.
                     30:      * @return The URL for the class.
                     31:      */
                     32: 
                     33:     protected URL locateClass(String name) {
                     34:        String base = wrapper.getServletBase();
                     35:        try {
1.3       abaird     36:            String cname = name.replace('.', '/')+".class";
                     37:            return new URL(new URL(base), cname);
1.1       abaird     38:        } catch (Exception ex) {
                     39:        }
                     40:        return null;
                     41:     }
                     42: 
                     43:     protected Class loadClass(String name, boolean resolve)
                     44:        throws ClassNotFoundException
                     45:     {
1.3       abaird     46:        Class c = null;
1.1       abaird     47:        if ( debug )
                     48:            trace("loading "+name);
1.3       abaird     49:        // Look for a cached class first:
                     50:        if ((c = (Class) classes.get(name)) != null) 
                     51:            return c;
                     52:        SecurityManager s = System.getSecurityManager();
                     53:        if ( s != null ) {
                     54:            int i = name.lastIndexOf('.');
                     55:            if ( i >= 0 )
                     56:                s.checkPackageAccess(name.substring(0, i));
                     57:        }
                     58:        // Then look for a system class:
1.1       abaird     59:        try {
1.3       abaird     60:            if ((c = findSystemClass(name)) != null)
                     61:                return c;
1.1       abaird     62:        } catch (Exception ex) {
                     63:        }
                     64:        // Load the class bytes from the net:
                     65:        byte data[] = null;
                     66:        URL  url    = locateClass(name);
                     67:        if ( url == null )
                     68:            throw new ClassNotFoundException("invalid servlet base");
                     69:        if ( debug )
                     70:            trace(name+" located at "+url);
                     71:        try {
                     72:            URLConnection         conn  = url.openConnection();
                     73:            InputStream           in    = conn.getInputStream();
                     74:            ByteArrayOutputStream out   = new ByteArrayOutputStream(512);
                     75:            byte                  buf[] = new byte[512];
                     76:            for (int got = 0 ; (got = in.read(buf)) > 0 ; )
                     77:                out.write(buf, 0, got);
                     78:            data = out.toByteArray();
                     79:            in.close();
                     80:        } catch (Exception ex) {
                     81:            ex.printStackTrace();
                     82:            throw new ClassNotFoundException(url.toExternalForm());
                     83:        }
                     84:        // Define and resolve the class:
                     85:        c = defineClass(data, 0, data.length);
                     86:        if ( resolve )
                     87:            resolveClass(c);
1.3       abaird     88:        classes.put(name, c);
1.1       abaird     89:        if ( debug )
                     90:            trace(name+": loading done.");
                     91:        return c;
                     92:     }
                     93: 
                     94:     protected ServletLoader(RemoteServletWrapper wrapper) {
                     95:        super();
                     96:        this.wrapper = wrapper;
1.3       abaird     97:        this.classes = new Hashtable(13);
1.1       abaird     98:     }
                     99: 
                    100: 
                    101: }

Webmaster