01: package com.javujavu.javux.app.compat.protocol.javuplug1;
02:
03: import java.io.File;
04: import java.io.IOException;
05: import java.io.InputStream;
06: import java.net.URL;
07: import java.net.URLConnection;
08: import java.util.Hashtable;
09:
10: import com.javujavu.javux.app.compat.JavuPlugClassLoader1;
11: import com.javujavu.javux.app.compat.JavuPlugJarClassLoader1;
12:
13: public class JavuPlugURLConnection1 extends URLConnection {
14: private static Hashtable loaders = new Hashtable();
15: private static boolean initDone;
16:
17: public static void addHandler(String urlPrefix,
18: JavuPlugClassLoader1 javuPlugClassLoader) {
19: if (!initDone) {
20: initDone = true;
21: String PROP = "java.protocol.handler.pkgs";
22: System.getProperties().put(
23: PROP,
24: "com.javujavu.javux.app.compat.protocol|"
25: + System.getProperty(PROP, ""));
26: }
27: loaders.put(urlPrefix, javuPlugClassLoader);
28: }
29:
30: public static void removeHandler(String urlPrefix) {
31: loaders.remove(urlPrefix);
32: }
33:
34: private InputStream in;
35:
36: protected JavuPlugURLConnection1(URL url) {
37: super (url);
38: }
39:
40: public InputStream getInputStream() throws IOException {
41: connect();
42: return in;
43: }
44:
45: public void connect() throws IOException {
46: if (in != null)
47: return;
48:
49: String file = getURL().getFile();
50: int i = file.indexOf(':');
51: int j = file.indexOf('!');
52: if (i != -1 && j > i) {
53: String loaderName = file.substring(1, i);
54: String jarName = file.substring(i + 1, j).replace('/',
55: File.separatorChar);
56: String resourceName = file.substring(j + 1);
57:
58: JavuPlugClassLoader1 loader = (JavuPlugClassLoader1) loaders
59: .get(loaderName);
60: if (loader != null) {
61: JavuPlugJarClassLoader1 jar = loader
62: .getJarLoader(jarName);
63: if (jar != null) {
64: in = jar.getResourceAsStream(resourceName);
65: }
66: }
67: }
68: if (in == null) {
69: throw new IOException("JavuPlug1 resource not found: "
70: + file);
71: }
72: }
73: }
|