KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > layers > NbinstURLStreamHandlerFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup.layers;
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLConnection JavaDoc;
27 import java.net.URLStreamHandler JavaDoc;
28 import java.net.URLStreamHandlerFactory JavaDoc;
29 import java.net.UnknownServiceException JavaDoc;
30 import org.openide.filesystems.FileObject;
31 import org.openide.util.Exceptions;
32
33 /**
34  * StreamHandlerFactory for nbinst protocol
35  */

36 public class NbinstURLStreamHandlerFactory implements URLStreamHandlerFactory JavaDoc {
37
38     /**
39      * Creates URLStreamHandler for nbinst protocol
40      * @param protocol
41      * @return NbinstURLStreamHandler if the protocol is nbinst otherwise null
42      */

43     public URLStreamHandler JavaDoc createURLStreamHandler(String JavaDoc protocol) {
44         if (NbinstURLMapper.PROTOCOL.equals(protocol)) {
45             return new NbinstURLStreamHandler ();
46         }
47         return null;
48     }
49
50     /**
51      * URLStreamHandler for nbinst protocol
52      */

53     private static class NbinstURLStreamHandler extends URLStreamHandler JavaDoc {
54
55         /**
56          * Creates URLConnection for URL with nbinst protocol.
57          * @param u URL for which the URLConnection should be created
58          * @return URLConnection
59          * @throws IOException
60          */

61         protected URLConnection JavaDoc openConnection(URL JavaDoc u) throws IOException JavaDoc {
62             return new NbinstURLConnection (u);
63         }
64     }
65
66     /** URLConnection for URL with nbinst protocol.
67      *
68      */

69     private static class NbinstURLConnection extends URLConnection JavaDoc {
70
71         private FileObject fo;
72         private InputStream JavaDoc iStream;
73
74         /**
75          * Creates new URLConnection
76          * @param url the parameter for which the connection should be
77          * created
78          */

79         public NbinstURLConnection (URL JavaDoc url) {
80             super (url);
81         }
82
83
84         public void connect() throws IOException JavaDoc {
85             if (fo == null) {
86                 FileObject[] decoded = NbinstURLMapper.decodeURL(this.url);
87                 if (decoded != null && decoded.length>0) {
88                     fo = decoded[0];
89                 }
90                 else {
91                     throw new FileNotFoundException JavaDoc("Cannot find: " + url); // NOI18N
92
}
93             }
94             if (fo.isFolder()) {
95                 throw new UnknownServiceException JavaDoc();
96             }
97         }
98
99         public int getContentLength() {
100             try {
101                 this.connect();
102                 return (int) this.fo.getSize(); //May cause overflow long->int
103
} catch (IOException JavaDoc e) {
104                 return -1;
105             }
106         }
107
108
109         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
110             this.connect();
111             if (iStream == null) {
112                 iStream = fo.getInputStream();
113             }
114             return iStream;
115         }
116
117
118         public String JavaDoc getHeaderField (String JavaDoc name) {
119             if ("content-type".equals(name)) { //NOI18N
120
try {
121                     this.connect();
122                     return fo.getMIMEType();
123                 } catch (IOException JavaDoc ioe) {
124                     Exceptions.printStackTrace(ioe);
125                 }
126             }
127             return super.getHeaderField(name);
128         }
129     }
130 }
131
Popular Tags