home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / rockridge / getStarted / protocol / example / Handler.java < prev    next >
Encoding:
Java Source  |  1995-11-13  |  1.6 KB  |  70 lines

  1.  
  2. package net.www.protocol.run;
  3.  
  4. import java.io.*;
  5. import net.www.html.*;
  6.  
  7. /**
  8.  *
  9.  * Given a URL of the form 
  10.  *
  11.  *           run:classname 
  12.  *
  13.  * run a compiled java class
  14.  * Currently the class must be in the CLASSPATH.
  15.  *
  16.  */
  17.  
  18. class Handler extends URLStreamHandler {
  19.  
  20.     public synchronized InputStream openStream(URL u) {
  21.  
  22.         /* This function must return an input stream suitable
  23.          * for reading in the contents of the URL.
  24.          * So, set up a PipedOutputStream which is connected
  25.          * to a PipedInputStream. To make it easier to
  26.          * write to the PipedOutputStream, make it a PrintStream.
  27.          */
  28.     PipedOutputStream ps = new PipedOutputStream();
  29.     PipedInputStream is = new PipedInputStream(ps);
  30.         PrintStream os = new PrintStream(ps);
  31.  
  32.         /* Get the class name from the URL, and
  33.          * strip off any leading slashes.
  34.          */
  35.     String className = u.file;
  36.     String args;
  37.     className = className.substring(className.lastIndexOf("/") + 1);
  38.  
  39.         /* Get the args -- if any.
  40.          */
  41.     if (className.indexOf("?") != 0) {
  42.         className.replace(':', ' ');
  43.     }
  44.  
  45.         /* Force URL type to be HTML.
  46.          */
  47.     u.setType(URL.content_html);
  48.  
  49.         /* Print appropriate HTML to the PipedOutputStream.
  50.          */
  51.      os.println("<HTML>");
  52.     os.print("<TITLE> Running Class: ");
  53.     os.print(className);
  54.     os.println(" </TITLE>");
  55.     os.print("<BODY><APP CLASS=\"");
  56.     os.print(className);
  57.     os.println("\"></BODY>\n</HTML>\n");
  58.  
  59.         /* Close the PipedOutputStream, so that the
  60.          * PipedInputStream can be read by the caller.
  61.          */
  62.     os.close();
  63.  
  64.         /* Return the PipedInputStream.
  65.          */
  66.     return is;
  67.     }
  68.  
  69. }
  70.