KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > core > ContentReference


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 package org.eclipse.update.core;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.net.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.update.internal.core.FatalIOException;
22 import org.eclipse.update.internal.core.Messages;
23 import org.eclipse.update.internal.core.URLEncoder;
24 import org.eclipse.update.internal.core.UpdateManagerUtils;
25 import org.eclipse.update.internal.core.connection.ConnectionFactory;
26 import org.eclipse.update.internal.core.connection.HttpResponse;
27 import org.eclipse.update.internal.core.connection.IResponse;
28
29 /**
30  * Content reference implements a general access wrapper
31  * to feature and site content. The reference specifies
32  * a "symbolic" path identifier for the content, and the actual
33  * reference as a file, or a URL.
34  * <p>
35  * This class may be instantiated or subclassed by clients.
36  * </p>
37  * <p>
38  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
39  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
40  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
41  * (repeatedly) as the API evolves.
42  * </p>
43  * @see org.eclipse.update.core.JarContentReference
44  * @see org.eclipse.update.core.JarEntryContentReference
45  * @since 2.0
46  */

47 public class ContentReference {
48
49     /**
50      * Unknown size indication
51      * @since 2.0
52      */

53     public static final long UNKNOWN_SIZE = -1;
54
55     /**
56      * Default executable permission when installing a content reference
57      * Will add executable bit if necessary
58      *
59      * @since 2.0.1
60      */

61     public static final int DEFAULT_EXECUTABLE_PERMISSION = -1;
62
63     private static final String JavaDoc FILE_URL_PROTOCOL = "file"; //$NON-NLS-1$
64

65     private String JavaDoc id;
66     private URL JavaDoc url; // reference is either URL reference *OR*
67
private File JavaDoc file; // local file reference
68
private IResponse response;
69     private int permission;
70     private long length;
71     
72     // <true> if a copy of a Contentreferenec in a temp local directory
73
private boolean tempLocal = false;
74     
75     private long lastModified;
76
77     /**
78      * Create content reference from URL.
79      *
80      * @param id "symbolic" path identifier
81      * @param url actual referenced URL
82      * @since 2.0
83      */

84     public ContentReference(String JavaDoc id, URL JavaDoc url) {
85         this.id = (id == null ? "" : id); //$NON-NLS-1$
86
this.url = url; // can be null
87
this.file = null;
88     }
89
90     /**
91      * Create content reference from file.
92      *
93      * @param id "symbolic" path identifier
94      * @param file actual referenced file
95      * @since 2.0
96      */

97     public ContentReference(String JavaDoc id, File JavaDoc file) {
98         this.id = (id == null ? "" : id); //$NON-NLS-1$
99
this.file = file; // can be null
100
this.url = null;
101     }
102
103     /**
104      * A factory method to create a content reference of
105      * the same type.
106      *
107      * @param id "symbolic" path identifier
108      * @param file actual referenced file
109      * @return content reference of the same type
110      * @since 2.0
111      */

112     public ContentReference createContentReference(String JavaDoc id, File JavaDoc file) {
113         return new ContentReference(id, file,true);
114     }
115     /**
116      *
117      */

118     private ContentReference(String JavaDoc id, File JavaDoc file, boolean b) {
119         this(id,file);
120         setTempLocal(b);
121     }
122
123     /**
124      * Retrieves the "symbolic" path identifier for the reference.
125      *
126      * @return "symbolic" path identifier
127      * @since 2.0
128      */

129     public String JavaDoc getIdentifier() {
130         return id;
131     }
132
133     /**
134      * Creates an input stream for the reference.
135      *
136      * @return input stream
137      * @exception IOException unable to create stream
138      * @since 2.0
139      */

140     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
141         if (file != null)
142             return new FileInputStream JavaDoc(file);
143         else if (url != null) {
144             if (response == null) {
145                 URL JavaDoc resolvedURL = URLEncoder.encode(url);
146                 response = ConnectionFactory.get(resolvedURL);
147                 UpdateManagerUtils.checkConnectionResult(response,resolvedURL);
148             }
149             InputStream JavaDoc is=response.getInputStream();
150             length=response.getContentLength();
151             return is;
152         } else
153             throw new FatalIOException(NLS.bind(Messages.ContentReference_UnableToCreateInputStream, (new String JavaDoc[] { this.toString() })));
154     }
155     /**
156      * Creates an input stream for the reference.
157      *
158      * @return input stream
159      * @exception IOException unable to create stream
160      * @since 2.0
161      */

162     InputStream JavaDoc getPartialInputStream(long offset) throws IOException JavaDoc {
163         if (url != null && "http".equals(url.getProtocol())) { //$NON-NLS-1$
164
URL JavaDoc resolvedURL = URLEncoder.encode(url);
165             response = ConnectionFactory.get(resolvedURL);
166             if(response instanceof HttpResponse)
167                 ((HttpResponse)response).setOffset(offset);
168             UpdateManagerUtils.checkConnectionResult(response,resolvedURL);
169             InputStream JavaDoc is = response.getInputStream();
170             length=offset + response.getContentLength();
171             return is;
172         } else
173             throw new FatalIOException(NLS.bind(Messages.ContentReference_UnableToCreateInputStream, (new String JavaDoc[] { this.toString() })));
174     }
175     
176     /**
177      * Returns the size of the referenced input, if it can be determined.
178      *
179      * @return input size, or @see #UNKNOWN_SIZE if size cannot be determined.
180      * @since 2.0
181      */

182     public long getInputSize() throws IOException JavaDoc {
183         if (length>0)
184             return length;
185         if (file != null)
186             return file.length();
187         else if (url != null) {
188             if (response == null) {
189                 URL JavaDoc resolvedURL = null;
190                 try {
191                     resolvedURL = URLEncoder.encode(url);
192                     response = ConnectionFactory.get(resolvedURL);
193                 } catch (IOException JavaDoc e) {
194                     return ContentReference.UNKNOWN_SIZE;
195                 }
196                 UpdateManagerUtils.checkConnectionResult(response,resolvedURL);
197             }
198             long size = response.getContentLength();
199             return size == -1 ? ContentReference.UNKNOWN_SIZE : size;
200         } else
201             return ContentReference.UNKNOWN_SIZE;
202     }
203
204     /**
205      * Indicates whether the reference is a local file reference.
206      *
207      * @return <code>true</code> if the reference is local,
208      * otherwise <code>false</code>
209      * @since 2.0
210      */

211     public boolean isLocalReference() {
212         /*if (file != null)
213             return true;
214         else if (url != null)
215             return FILE_URL_PROTOCOL.equals(url.getProtocol());
216         else
217             return false;*/

218         // only temp files are considered local
219
return tempLocal;
220     }
221
222     /**
223      * Returns the content reference as a file. Note, that this method
224      * <b>does not</b> cause the file to be downloaded if it
225      * is not already local.
226      *
227      * @return reference as file
228      * @exception IOException reference cannot be returned as file
229      * @since 2.0
230      */

231     public File JavaDoc asFile() throws IOException JavaDoc {
232         if (file != null)
233             return file;
234
235         if (url != null && FILE_URL_PROTOCOL.equals(url.getProtocol())) {
236             File JavaDoc result = new File JavaDoc(url.getFile());
237             if (result.exists())
238                 return result;
239             else
240                 throw new IOException JavaDoc(NLS.bind(Messages.ContentReference_FileDoesNotExist, (new String JavaDoc[] { this.toString() })));
241         }
242
243         throw new IOException JavaDoc(NLS.bind(Messages.ContentReference_UnableToReturnReferenceAsFile, (new String JavaDoc[] { this.toString() })));
244     }
245
246     /**
247      * Returns the content reference as a URL.
248      *
249      * @return reference as URL
250      * @exception IOException reference cannot be returned as URL
251      * @since 2.0
252      */

253     public URL JavaDoc asURL() throws IOException JavaDoc {
254         if (url != null)
255             return url;
256
257         if (file != null)
258             return file.toURL();
259
260         throw new FatalIOException(NLS.bind(Messages.ContentReference_UnableToReturnReferenceAsURL, (new String JavaDoc[] { this.toString() })));
261     }
262
263     /**
264      * Return string representation of this reference.
265      *
266      * @return string representation
267      * @since 2.0
268      */

269     public String JavaDoc toString() {
270         if (file != null)
271             return file.getAbsolutePath();
272         else
273             return url.toExternalForm();
274     }
275     /**
276      * Returns the permission for this file.
277      *
278      * @return the content reference permission
279      * @see #DEFAULT_EXECUTABLE_PERMISSION
280      * @since 2.0.1
281      */

282     public int getPermission() {
283         return permission;
284     }
285
286     /**
287      * Sets the permission of this content reference.
288      *
289      * @param permission The permission to set
290      */

291     public void setPermission(int permission) {
292         this.permission = permission;
293     }
294
295     /**
296      * Sets if a content reference is considered local
297      *
298      * @param tempLocal <code>true</code> if the file is considered local
299      */

300     protected void setTempLocal(boolean tempLocal) {
301         this.tempLocal = tempLocal;
302     }
303     
304     /**
305      * Sets the timestamp the content was last modified.
306      * @param timestamp
307      * @since 3.0
308      */

309     public void setLastModified(long timestamp) {
310         this.lastModified = timestamp;
311     }
312     
313     /**
314      * Returns the timestamp when the content was last modified
315      * @return the timestamp
316      * @since 3.0
317      */

318     public long getLastModified() {
319         if (lastModified == 0) {
320             if (file != null)
321                 lastModified = file.lastModified();
322             else if (url != null) {
323                 if (response == null) {
324                     try {
325                         URL JavaDoc resolvedURL = URLEncoder.encode(url);
326                         response = ConnectionFactory.get(resolvedURL);
327                     } catch (MalformedURLException JavaDoc e) {
328                         // return 0
329
} catch (IOException JavaDoc e) {
330                         // return 0
331
}
332                 }
333                 lastModified = response.getLastModified();
334             }
335         }
336         return lastModified;
337     }
338 }
339
Popular Tags