home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / SimpleResourceConnection.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  1.8 KB  |  75 lines

  1. package sun.beanbox.simpleresource;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5. import sun.beanbox.*;
  6.  
  7. public class SimpleResourceConnection extends URLConnection {
  8.     private static boolean debug = false;
  9.  
  10.     private Object resource;    // the resource we are fetching
  11.     private String cookie;    // identification of the loader instance to use
  12.     private String name;    // name of the resource
  13.     private final String prefix = SimpleClassLoader.urlPrefix;
  14.     private final int prefixLength = prefix.length();
  15.  
  16.     protected SimpleResourceConnection (URL url)
  17.         throws MalformedURLException, IOException
  18.     {
  19.     super(url);
  20.     debug("SimpleResourceConnection("+url+")");
  21.     String file = url.getFile();
  22.     if (file.startsWith("/")) {
  23.         file = file.substring(1);
  24.     }
  25.     if (! file.startsWith(prefix)) {
  26.         throw new MalformedURLException("SimpleResource file should start with /SIMPLE");
  27.     }
  28.     cookie = file.substring(prefixLength, file.indexOf("/+/"));
  29.     name = file.substring(file.indexOf("/+/")+3);
  30.  
  31.     debug(" cookie: "+cookie);
  32.     debug(" name: "+name);
  33.     }
  34.  
  35.     public void connect() throws IOException {
  36.     debug("Looking for "+cookie+", "+name+" in SimpleResourceLoader");
  37.     Object o = SimpleClassLoader.getLocalResource(cookie, name);
  38.     if (o == null) {
  39.         debug("Invalid resource name");
  40.         resource = null;
  41.         return;
  42.     } else {
  43.         debug("Found resource "+o);
  44.         resource = o;
  45.     }
  46.     }
  47.  
  48.     public Object getContent() throws IOException {
  49.     if (!connected) {
  50.         connect();
  51.     }
  52.     return resource;
  53.     }
  54.  
  55.     public InputStream getInputStream() throws IOException {
  56.     if (!connected) {
  57.         connect();
  58.     }
  59.  
  60.     if (resource instanceof InputStream) {
  61.         return (InputStream) resource;
  62.     }
  63.     return SimpleClassLoader.getLocalResourceAsStream(cookie, name);
  64.     }
  65.  
  66.  
  67.     private void debug(String msg) {
  68.     if (debug) {
  69.         System.err.println(msg);
  70.     }
  71.     }
  72.  
  73.  
  74. }
  75.