KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > url > file > Handler


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.system.url.file;
19
20 import java.io.File JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25 import java.net.URLStreamHandler JavaDoc;
26
27 import sun.net.www.ParseUtil;
28
29 /**
30  * A protocol handler for the 'file' protocol.
31  *
32  * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
33  */

34 public class Handler extends URLStreamHandler JavaDoc {
35     protected void parseURL(final URL JavaDoc url, final String JavaDoc s, final int i, final int j) {
36         super.parseURL(url, s.replace(File.separatorChar, '/'), i, j);
37     }
38
39     /**
40      * Open a connection to the file.
41      *
42      * <p>NOTE: Sun's impl attempts to translate into a 'ftp' URL which is dumb
43      * so fix that by removing it ;-)
44      */

45     public URLConnection JavaDoc openConnection(final URL JavaDoc url) throws IOException JavaDoc {
46         String JavaDoc path = ParseUtil.decode(url.getPath());
47         path = path.replace('/', File.separatorChar).replace('|', ':');
48         File JavaDoc file = new File JavaDoc(path);
49         if (!file.exists()) {
50             throw new FileNotFoundException JavaDoc(file.toString());
51         }
52
53         // Handle the hostname of the URL if given, puke if not valid
54
String JavaDoc hostname = url.getHost();
55         if (hostname == null ||
56                 hostname.equals("") ||
57                 hostname.equals("~") ||
58                 hostname.equals("localhost") ||
59                 file.exists()) {
60             return new FileURLConnection(url, file);
61         }
62
63         throw new FileNotFoundException JavaDoc("Invalid host specification: " + url);
64     }
65 }
66
Popular Tags