KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > URLClassPath


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

15
16 package javassist;
17
18 import java.io.*;
19 import java.net.*;
20
21 /**
22  * A class search-path specified with URL (http).
23  *
24  * @see javassist.ClassPath
25  * @see ClassPool#insertClassPath(ClassPath)
26  * @see ClassPool#appendClassPath(ClassPath)
27  */

28 public class URLClassPath implements ClassPath {
29     protected String JavaDoc hostname;
30     protected int port;
31     protected String JavaDoc directory;
32     protected String JavaDoc packageName;
33
34     /**
35      * Creates a search path specified with URL (http).
36      *
37      * <p>This search path is used only if a requested
38      * class name starts with the name specified by <code>packageName</code>.
39      * If <code>packageName</code> is "mypack" and a requested class is
40      * "mypack.sub.Test", then the given URL is used for loading that class.
41      * If <code>packageName</code> is <code>null</code>, the URL is used
42      * for loading any class.
43      *
44      * @param host host name
45      * @param port port number
46      * @param directory directory name ending with "/".
47      * It can be "/" (root directory).
48      * It must start with "/".
49      * @param packageName package name.
50      */

51     public URLClassPath(String JavaDoc host, int port,
52                         String JavaDoc directory, String JavaDoc packageName) {
53         hostname = host;
54         this.port = port;
55         this.directory = directory;
56         this.packageName = packageName;
57     }
58
59     public String JavaDoc toString() {
60         return hostname + ":" + port + directory;
61     }
62
63     /**
64      * Opens a class file with http.
65      *
66      * @return null if the class file could not be found.
67      */

68     public InputStream openClassfile(String JavaDoc classname) {
69         try {
70             URLConnection con = openClassfile0(classname);
71             if (con != null)
72                 return con.getInputStream();
73         }
74         catch (IOException e) {}
75         return null; // not found
76
}
77
78     private URLConnection openClassfile0(String JavaDoc classname) throws IOException {
79         if (packageName == null || classname.startsWith(packageName)) {
80             String JavaDoc jarname
81                     = directory + classname.replace('.', '/') + ".class";
82             return fetchClass0(hostname, port, jarname);
83         }
84         else
85             return null; // not found
86
}
87
88     /**
89      * Returns the URL.
90      *
91      * @return null if the class file could not be obtained.
92      */

93     public URL find(String JavaDoc classname) {
94         try {
95             URLConnection con = openClassfile0(classname);
96             InputStream is = con.getInputStream();
97             if (is != null) {
98                 is.close();
99                 return con.getURL();
100             }
101         }
102         catch (IOException e) {}
103         return null;
104     }
105
106     /**
107      * Closes this class path.
108      */

109     public void close() {}
110
111     /**
112      * Reads a class file on an http server.
113      *
114      * @param host host name
115      * @param port port number
116      * @param directory directory name ending with "/".
117      * It can be "/" (root directory).
118      * It must start with "/".
119      * @param classname fully-qualified class name
120      */

121     public static byte[] fetchClass(String JavaDoc host, int port,
122                                     String JavaDoc directory, String JavaDoc classname)
123         throws IOException
124     {
125         byte[] b;
126         URLConnection con = fetchClass0(host, port,
127                 directory + classname.replace('.', '/') + ".class");
128         int size = con.getContentLength();
129         InputStream s = con.getInputStream();
130         try {
131             if (size <= 0)
132                 b = ClassPoolTail.readStream(s);
133             else {
134                 b = new byte[size];
135                 int len = 0;
136                 do {
137                     int n = s.read(b, len, size - len);
138                     if (n < 0)
139                         throw new IOException("the stream was closed: "
140                                               + classname);
141
142                     len += n;
143                 } while (len < size);
144             }
145         }
146         finally {
147             s.close();
148         }
149
150         return b;
151     }
152
153     private static URLConnection fetchClass0(String JavaDoc host, int port,
154                                              String JavaDoc filename)
155         throws IOException
156     {
157         URL url;
158         try {
159             url = new URL("http", host, port, filename);
160         }
161         catch (MalformedURLException e) {
162             // should never reache here.
163
throw new IOException("invalid URL?");
164         }
165
166         URLConnection con = url.openConnection();
167         con.connect();
168         return con;
169     }
170 }
171
Popular Tags