KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > connection > AbstractResponse


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
12 package org.eclipse.update.internal.core.connection;
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.URLConnection JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.update.internal.core.UpdateCore;
23
24 /**
25  * @author btripkov
26  *
27  */

28 public abstract class AbstractResponse implements IResponse {
29
30     private static final long POLLING_INTERVAL = 200;
31     protected URLConnection JavaDoc connection;
32
33     protected InputStream JavaDoc openStreamWithCancel(URLConnection JavaDoc urlConnection, IProgressMonitor monitor) throws IOException JavaDoc, CoreException, TooManyOpenConnectionsException {
34     
35         ConnectionThreadManager.StreamRunnable runnable =
36             new ConnectionThreadManager.StreamRunnable(urlConnection);
37         Thread JavaDoc t = ConnectionThreadManagerFactory.getConnectionManager().getConnectionThread(
38                 runnable);
39         t.start();
40         InputStream JavaDoc is = null;
41         try {
42             for (;;) {
43                 if (monitor.isCanceled()) {
44                     runnable.disconnect();
45                     connection = null;
46                     break;
47                 }
48                 if (runnable.getInputStream() != null || !t.isAlive()) {
49                     is = runnable.getInputStream();
50                     break;
51                 }
52                 if (runnable.getIOException() != null)
53                     throw runnable.getIOException();
54                 if (runnable.getException() != null)
55                         throw new CoreException(new Status(IStatus.ERROR,
56                                                             UpdateCore.getPlugin().getBundle().getSymbolicName(),
57                                                             IStatus.OK,
58                                                             runnable.getException().getMessage(),
59                                                             runnable.getException()));
60                 t.join(POLLING_INTERVAL);
61                 }
62         } catch (InterruptedException JavaDoc e) {
63         }
64         return is;
65     }
66
67     
68     
69 }
70
Popular Tags