KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > protocol > reference > ReferenceURLConnection


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.internal.protocol.reference;
13
14 import java.io.*;
15 import java.net.URL JavaDoc;
16 import java.net.URLConnection JavaDoc;
17 import org.eclipse.osgi.framework.adaptor.FilePath;
18 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
19 import org.eclipse.osgi.framework.internal.core.ReferenceInputStream;
20
21 /**
22  * URLConnection for the reference protocol.
23  */

24
25 public class ReferenceURLConnection extends URLConnection JavaDoc {
26     protected URL JavaDoc reference;
27
28     protected ReferenceURLConnection(URL JavaDoc url) {
29         super(url);
30     }
31
32     public synchronized void connect() throws IOException {
33         if (!connected) {
34             // TODO assumes that reference URLs are always based on file: URLs.
35
// There are not solid usecases to the contrary. Yet.
36
// Construct the ref URL carefully so as to preserve UNC paths etc.
37
File file = new File(url.getPath().substring(5));
38             URL JavaDoc ref;
39             if (!file.isAbsolute()) {
40                 String JavaDoc installPath = getInstallPath();
41                 if (installPath != null)
42                     file = makeAbsolute(installPath, file);
43             }
44             ref = file.toURL();
45             if (!file.exists())
46                 throw new FileNotFoundException(file.toString());
47             reference = ref;
48         }
49     }
50
51     public boolean getDoInput() {
52         return true;
53     }
54
55     public boolean getDoOutput() {
56         return false;
57     }
58
59     public InputStream getInputStream() throws IOException {
60         if (!connected) {
61             connect();
62         }
63
64         return new ReferenceInputStream(reference);
65     }
66
67     private String JavaDoc getInstallPath() {
68         String JavaDoc installURL = FrameworkProperties.getProperty("osgi.install.area"); //$NON-NLS-1$
69
if (installURL == null)
70             return null;
71         if (!installURL.startsWith("file:")) //$NON-NLS-1$
72
return null;
73         // this is the safest way to create a File object off a file: URL
74
return installURL.substring(5);
75     }
76
77     private static File makeAbsolute(String JavaDoc base, File relative) {
78         if (relative.isAbsolute())
79             return relative;
80         return new File(new FilePath(base + relative.getPath()).toString());
81     }
82 }
83
Popular Tags