KickJava   Java API By Example, From Geeks To Geeks.

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


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

11
12 package org.eclipse.osgi.framework.adaptor.core;
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.*;
17
18 /**
19  * URLConnection for BundleClassLoader resources.
20  */

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

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

114     public URL getLocalURL() {
115         return bundleEntry.getLocalURL();
116     }
117
118     /**
119      * Converts the URL to a URL that uses the file: protocol. The content of this
120      * URL may be downloaded or extracted onto the local filesystem to create a file URL.
121      * @return the local URL that uses the file: protocol
122      */

123     public URL getFileURL() {
124         return bundleEntry.getFileURL();
125     }
126 }
Popular Tags