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 General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.downloader.services;
038:
039: import java.io.File;
040: import java.net.URL;
041: import java.util.HashMap;
042: import java.util.Map;
043: import org.netbeans.installer.downloader.DownloadListener;
044: import org.netbeans.installer.downloader.DownloadManager;
045: import org.netbeans.installer.downloader.Pumping;
046: import org.netbeans.installer.downloader.Pumping.State;
047: import org.netbeans.installer.utils.exceptions.DownloadException;
048:
049: /**
050: *
051: * @author Danila_Dugurov
052: */
053: //todo: may be very general synchronization - optimize!
054: public class FileProvider {
055:
056: /////////////////////////////////////////////////////////////////////////////////
057: // Static
058: private static final FileProvider fileProvider = new FileProvider();
059:
060: public static FileProvider getProvider() {
061: return fileProvider;
062: }
063:
064: /////////////////////////////////////////////////////////////////////////////////
065: // Instance
066: private final DownloadManager downloadManager = DownloadManager.instance;
067:
068: private final DownloadListener listener = new MyListener();
069:
070: private final PersistentCache cache = new PersistentCache();
071:
072: private final Map<URL, State> scheduledURL2State = new HashMap<URL, State>();
073:
074: protected FileProvider() {
075: downloadManager.registerListener(listener);
076: }
077:
078: public synchronized void clearCaches() {
079: for (URL url : cache.keys()) {
080: cache.delete(url);
081: }
082: }
083:
084: public synchronized boolean isInCache(URL url) {
085: return cache.isIn(url);
086: }
087:
088: public synchronized void asynchDownload(URL url, File folder) {
089: if (isInCache(url))
090: return;
091: if (scheduledURL2State.containsKey(url))
092: return;
093: if (!downloadManager.isActive())
094: downloadManager.invoke();
095: scheduledURL2State.put(url, State.NOT_PROCESSED);
096: downloadManager.queue().add(
097: url,
098: folder != null ? folder : downloadManager
099: .defaultFolder());
100: }
101:
102: public synchronized File get(URL url) throws DownloadException {
103: return get(url, null, true);
104: }
105:
106: public synchronized File get(URL url, File folder)
107: throws DownloadException {
108: return get(url, folder, true);
109: }
110:
111: public synchronized File get(URL url, boolean useCache)
112: throws DownloadException {
113: return get(url, null, useCache);
114: }
115:
116: public synchronized File get(URL url, File folder, boolean useCache)
117: throws DownloadException {
118: while (true) {
119: final File file = tryGet(url);
120: if (file != null) {
121: if (useCache)
122: return file;
123: cache.delete(url);
124: useCache = true;
125: }
126: synchronized (url) {
127: asynchDownload(url, folder);
128: try {
129: url.wait();
130: } catch (InterruptedException interrupt) {
131: throw new DownloadException(
132: "download faild " + url, interrupt);
133: }
134: }
135: switch (scheduledURL2State.get(url)) {
136: case FAILED: {
137: scheduledURL2State.remove(url);
138: throw new DownloadException("download faild " + url);
139: }
140: case DELETED: {
141: scheduledURL2State.remove(url);
142: throw new DownloadException(
143: "download faild - externaly deleted " + url);
144: }
145: case FINISHED:
146: scheduledURL2State.remove(url);
147: }
148: }
149: }
150:
151: public synchronized File tryGet(URL url) {
152: if (cache.isIn(url))
153: return cache.getByURL(url);
154: return null;
155: }
156:
157: public synchronized void manuallyDelete(URL url) {
158: downloadManager.queue().delete(url);
159: cache.delete(url);
160: }
161:
162: /////////////////////////////////////////////////////////////////////////////////
163: // Inner Classes
164: private class MyListener extends EmptyQueueListener {
165: public void pumpingStateChange(String id) {
166: final Pumping pumping = downloadManager.queue().getById(id);
167: final URL url = pumping.declaredURL();
168: scheduledURL2State.put(url, pumping.state());
169: switch (pumping.state()) {
170: case FINISHED:
171: cache.put(url, pumping.outputFile());
172: case DELETED:
173: case FAILED:
174: synchronized (url) {
175: url.notifyAll();
176: }
177: }
178: }
179: }
180: }
|