KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > BundleURLConnection


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.core;
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.net.URLConnection JavaDoc;
18 import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry;
19 import org.eclipse.osgi.internal.baseadaptor.AdaptorMsg;
20 import org.eclipse.osgi.util.NLS;
21
22 /**
23  * URLConnection for BundleClassLoader resources.
24  */

25
26 public class BundleURLConnection extends URLConnection JavaDoc {
27     /** BundleEntry that the URL is associated. */
28     protected final BundleEntry bundleEntry;
29
30     /** InputStream for this URLConnection. */
31     protected InputStream JavaDoc in;
32
33     /** content type for this URLConnection */
34     protected String JavaDoc contentType;
35
36     /**
37      * Constructor for a BundleClassLoader resource URLConnection.
38      *
39      * @param url URL for this URLConnection.
40      * @param bundleEntry BundleEntry that the URLConnection is associated.
41      */

42     public BundleURLConnection(URL JavaDoc url, BundleEntry bundleEntry) {
43         super(url);
44
45         this.bundleEntry = bundleEntry;
46         this.in = null;
47         this.contentType = null;
48     }
49
50     public synchronized void connect() throws IOException JavaDoc {
51         if (!connected) {
52             if (bundleEntry != null) {
53                 in = bundleEntry.getInputStream();
54                 connected = true;
55             } else {
56                 throw new IOException JavaDoc(NLS.bind(AdaptorMsg.RESOURCE_NOT_FOUND_EXCEPTION, url));
57             }
58         }
59     }
60
61     public int getContentLength() {
62         return ((int) bundleEntry.getSize());
63     }
64
65     public String JavaDoc getContentType() {
66         if (contentType == null) {
67             contentType = guessContentTypeFromName(bundleEntry.getName());
68
69             if (contentType == null) {
70                 if (!connected) {
71                     try {
72                         connect();
73                     } catch (IOException JavaDoc e) {
74                         return (null);
75                     }
76                 }
77                 try {
78                     if (in.markSupported())
79                         contentType = guessContentTypeFromStream(in);
80                 } catch (IOException JavaDoc e) {
81                     // do nothing
82
}
83             }
84         }
85
86         return (contentType);
87     }
88
89     public boolean getDoInput() {
90         return (true);
91     }
92
93     public boolean getDoOutput() {
94         return (false);
95     }
96
97     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
98         if (!connected) {
99             connect();
100         }
101
102         return (in);
103     }
104
105     public long getLastModified() {
106         long lastModified = bundleEntry.getTime();
107
108         if (lastModified == -1) {
109             return (0);
110         }
111
112         return (lastModified);
113     }
114
115     /**
116      * Converts the URL to a common local URL protocol (i.e file: or jar: protocol)
117      * @return the local URL using a common local protocol
118      */

119     public URL JavaDoc getLocalURL() {
120         return bundleEntry.getLocalURL();
121     }
122
123     /**
124      * Converts the URL to a URL that uses the file: protocol. The content of this
125      * URL may be downloaded or extracted onto the local filesystem to create a file URL.
126      * @return the local URL that uses the file: protocol
127      */

128     public URL JavaDoc getFileURL() {
129         return bundleEntry.getFileURL();
130     }
131 }
132
Popular Tags