001: // ServletLoader.java
002: // $Id: ServletLoader.java,v 1.10 2000/08/16 21:37:45 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.servlet;
007:
008: import java.io.ByteArrayOutputStream;
009: import java.io.InputStream;
010: import java.io.PrintStream;
011:
012: import java.net.URL;
013: import java.net.URLConnection;
014:
015: import java.util.Hashtable;
016:
017: /**
018: * @author Alexandre Rafalovitch <alex@access.com.au>
019: * @author Anselm Baird-Smith <abaird@w3.org>
020: */
021:
022: public class ServletLoader extends ClassLoader {
023: private static final boolean debug = false;
024: private Hashtable classes = null;
025:
026: RemoteServletWrapper wrapper = null;
027:
028: private void trace(String msg) {
029: System.out.println("[" + wrapper.getURLPath() + "]: " + msg);
030: }
031:
032: /**
033: * Given the class name, return its URL.
034: * @param name The class to be loaded.
035: * @return The URL for the class.
036: */
037:
038: protected URL locateClass(String name) {
039: String base = wrapper.getServletBase();
040: try {
041: String cname = name.replace('.', '/') + ".class";
042: return new URL(new URL(base), cname);
043: } catch (Exception ex) {
044: }
045: return null;
046: }
047:
048: /**
049: * Try to load class.
050: * @param name The class name.
051: * @param boolean The resolve flag
052: * @return a Class instance
053: * @exception ClassNotFoundException if the class file can't be found.
054: */
055: protected Class loadClass(String name, boolean resolve)
056: throws ClassNotFoundException {
057: //remove the .class
058: if (name.endsWith(".class"))
059: name = name.substring(0, name.lastIndexOf('.'));
060: Class c = null;
061: if (debug)
062: trace("loading " + name);
063: // Look for a cached class first:
064: if ((c = (Class) classes.get(name)) != null)
065: return c;
066: SecurityManager s = System.getSecurityManager();
067: if (s != null) {
068: int i = name.lastIndexOf('.');
069: if (i >= 0)
070: s.checkPackageAccess(name.substring(0, i));
071: }
072: // Then look for a system class:
073: try {
074: if ((c = findSystemClass(name)) != null)
075: return c;
076: } catch (Exception ex) {
077: }
078: // Load the class bytes from the net:
079: byte data[] = null;
080: URL url = locateClass(name);
081: if (url == null)
082: throw new ClassNotFoundException("invalid servlet base");
083: if (debug)
084: trace(name + " located at " + url);
085: try {
086: URLConnection conn = url.openConnection();
087: InputStream in = conn.getInputStream();
088: ByteArrayOutputStream out = new ByteArrayOutputStream(512);
089: byte buf[] = new byte[512];
090: for (int got = 0; (got = in.read(buf)) > 0;)
091: out.write(buf, 0, got);
092: data = out.toByteArray();
093: in.close();
094: } catch (Exception ex) {
095: ex.printStackTrace();
096: throw new ClassNotFoundException(url.toExternalForm());
097: }
098: // Define and resolve the class:
099: c = defineClass(data, 0, data.length);
100: if (resolve)
101: resolveClass(c);
102: classes.put(name, c);
103: if (debug)
104: trace(name + ": loading done.");
105: return c;
106: }
107:
108: protected ServletLoader(RemoteServletWrapper wrapper) {
109: super ();
110: this .wrapper = wrapper;
111: this .classes = new Hashtable(13);
112: }
113:
114: }
|