home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 1999 March / maximum-cd-1999-03.iso / Feature / Lotus / ORGANIZE / COMPNENT / LTOUIN21.ZIP / sun / beanbox / simpleresource / SimpleResourceConnection.class (.txt) < prev   
Encoding:
Java Class File  |  1998-03-12  |  2.5 KB  |  71 lines

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