KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > OtherResponse


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.internal.core;
12
13 import java.io.*;
14 import java.net.*;
15
16 import org.eclipse.core.runtime.*;
17
18 public class OtherResponse implements Response {
19     private static final long POLLING_INTERVAL = 200;
20     protected URL url;
21     protected InputStream in;
22     protected URLConnection connection;
23     protected long lastModified;
24
25     public OtherResponse(URL url) throws IOException {
26         this.url = url;
27 // connection = url.openConnection();
28
}
29
30     public InputStream getInputStream() throws IOException {
31         if (in == null && url != null) {
32             if (connection == null)
33                 connection = url.openConnection();
34             in = connection.getInputStream();
35             this.lastModified = connection.getLastModified();
36         }
37         return in;
38     }
39     /**
40      * @see Response#getInputStream(IProgressMonitor)
41      */

42     public InputStream getInputStream(IProgressMonitor monitor)
43         throws IOException, CoreException {
44         if (in == null && url != null) {
45             if (connection == null)
46                 connection = url.openConnection();
47
48             if (monitor != null) {
49                 this.in =
50                     openStreamWithCancel(connection, monitor);
51             } else {
52                 this.in = connection.getInputStream();
53             }
54             if (in != null) {
55                 this.lastModified = connection.getLastModified();
56             }
57         }
58         return in;
59     }
60
61     public long getContentLength() {
62         if (connection != null)
63             return connection.getContentLength();
64         return 0;
65     }
66
67     public int getStatusCode() {
68         return IStatusCodes.HTTP_OK;
69     }
70
71     public String JavaDoc getStatusMessage() {
72         return ""; //$NON-NLS-1$
73
}
74
75     public long getLastModified() {
76         if (lastModified == 0 && connection != null) {
77             lastModified = connection.getLastModified();
78         }
79         return lastModified;
80     }
81     
82     private InputStream openStreamWithCancel(
83             URLConnection urlConnection,
84             IProgressMonitor monitor)
85             throws IOException, CoreException {
86             ConnectionThreadManager.StreamRunnable runnable =
87                 new ConnectionThreadManager.StreamRunnable(urlConnection);
88             Thread JavaDoc t =
89                 UpdateCore.getPlugin().getConnectionManager().createThread(
90                     runnable);
91             t.start();
92             InputStream is = null;
93             try {
94                 for (;;) {
95                     if (monitor.isCanceled()) {
96                         runnable.disconnect();
97                         connection = null;
98                         break;
99                     }
100                     if (runnable.getInputStream() != null) {
101                         is = runnable.getInputStream();
102                         break;
103                     }
104                     if (runnable.getException() != null) {
105                         if (runnable.getException() instanceof IOException)
106                             throw (IOException) runnable.getException();
107                         else
108                             throw new CoreException(new Status(IStatus.ERROR,
109                                     UpdateCore.getPlugin().getBundle()
110                                             .getSymbolicName(), IStatus.OK,
111                                     runnable.getException().getMessage(), runnable
112                                             .getException()));
113                     }
114                     t.join(POLLING_INTERVAL);
115                 }
116             } catch (InterruptedException JavaDoc e) {
117             }
118             return is;
119         }
120 }
121
Popular Tags