001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.autoupdate.updateprovider;
029:
030: import java.io.BufferedInputStream;
031: import java.io.InputStream;
032: import java.net.URL;
033: import java.net.URLConnection;
034: import java.util.concurrent.Callable;
035: import java.util.concurrent.ExecutionException;
036: import java.util.concurrent.ExecutorService;
037: import java.util.concurrent.Executors;
038: import java.util.concurrent.Future;
039: import org.openide.util.Cancellable;
040: import org.openide.util.RequestProcessor;
041:
042: /**
043: *
044: * @author Jirka Rechtacek
045: */
046: public class NetworkAccess {
047:
048: private NetworkAccess() {
049: }
050:
051: public static Task createNetworkAcessTask(URL url, int timeout,
052: NetworkListener networkAcesssListener) {
053: return new Task(url, timeout, networkAcesssListener);
054: }
055:
056: public static class Task implements Cancellable {
057: private URL url;
058: private int timeout;
059: private NetworkListener listener;
060: private ExecutorService es = Executors
061: .newSingleThreadExecutor();
062: private Future<InputStream> connect = null;
063: private RequestProcessor.Task rpTask = null;
064:
065: private Task(URL url, int timeout, NetworkListener listener) {
066: if (url == null) {
067: throw new IllegalArgumentException(
068: "URL cannot be null.");
069: }
070: if (listener == null) {
071: throw new IllegalArgumentException(
072: "NetworkListener cannot be null.");
073: }
074: this .url = url;
075: this .timeout = timeout;
076: this .listener = listener;
077: postTask();
078: }
079:
080: private void postTask() {
081: final Callable<InputStream> connectTask = createCallableNetwork(
082: url, timeout);
083: rpTask = RequestProcessor.getDefault().post(new Runnable() {
084: public void run() {
085: connect = es.submit(connectTask);
086: InputStream is = null;
087: try {
088: is = connect.get();
089: if (connect.isDone()) {
090: listener.streamOpened(is);
091: } else if (connect.isCancelled()) {
092: listener.accessCanceled();
093: } else {
094: listener.accessTimeOut();
095: }
096: } catch (InterruptedException ix) {
097: listener.notifyException(ix);
098: } catch (ExecutionException ex) {
099: listener.notifyException(ex);
100: }
101: }
102: });
103: }
104:
105: public void waitFinished() {
106: assert rpTask != null : "RequestProcessor.Task must be initialized.";
107: rpTask.waitFinished();
108: }
109:
110: private Callable<InputStream> createCallableNetwork(
111: final URL url, final int timeout) {
112: return new Callable<InputStream>() {
113: public InputStream call() throws Exception {
114: URLConnection conn = url.openConnection();
115: conn.setConnectTimeout(timeout);
116: return new BufferedInputStream(conn
117: .getInputStream());
118: }
119: };
120: }
121:
122: public boolean cancel() {
123: return connect.cancel(true);
124: }
125:
126: }
127:
128: public interface NetworkListener {
129: public void streamOpened(InputStream stream);
130:
131: public void accessCanceled();
132:
133: public void accessTimeOut();
134:
135: public void notifyException(Exception x);
136: }
137:
138: }
|