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.utils;
038:
039: import java.io.File;
040: import java.io.FileOutputStream;
041: import java.io.IOException;
042: import java.io.InputStream;
043: import java.io.OutputStream;
044: import java.net.MalformedURLException;
045: import java.net.URI;
046: import java.net.URISyntaxException;
047: import java.net.URL;
048: import java.util.HashMap;
049: import java.util.Map;
050: import org.netbeans.installer.downloader.DownloadManager;
051: import org.netbeans.installer.downloader.services.FileProvider;
052: import org.netbeans.installer.utils.exceptions.DownloadException;
053: import org.netbeans.installer.downloader.DownloadProgress;
054: import org.netbeans.installer.utils.helper.UiMode;
055: import org.netbeans.installer.utils.progress.Progress;
056: import org.netbeans.installer.downloader.ui.ProxySettingsDialog;
057: import org.netbeans.installer.utils.helper.ExtendedUri;
058:
059: /**
060: *
061: * @author Danila_Dugurov
062: */
063: //todo: This class already ready to die.
064: public class FileProxy {
065: private static final String RESOURCE_SCHEME = "resource";
066: public static final String RESOURCE_SCHEME_PREFIX = RESOURCE_SCHEME
067: + ":";
068:
069: private final File tmpDir = new File(DownloadManager.getInstance()
070: .getLocalDirectory(), "tmp");
071: private final Map<String, File> cache = new HashMap<String, File>();
072: {
073: tmpDir.mkdirs();
074: tmpDir.deleteOnExit();
075: }
076:
077: public static final FileProxy proxy = new FileProxy();
078:
079: public static FileProxy getInstance() {
080: return proxy;
081: }
082:
083: public void deleteFile(String uri) throws IOException {
084: final File file = cache.get(uri);
085: if (uri != null && uri.startsWith("file"))
086: return;
087: if (file != null)
088: FileUtils.deleteFile(file);
089: cache.remove(uri);
090: }
091:
092: public void deleteFile(ExtendedUri uri) throws IOException {
093: if ((uri.getLocal() != null)
094: && !uri.getLocal().equals(uri.getRemote())
095: && !uri.getAlternates().contains(uri.getLocal())) {
096: deleteFile(uri.getRemote());
097: uri.setLocal(null);
098: }
099: }
100:
101: public void deleteFile(URI uri) throws IOException {
102: deleteFile(uri.toString());
103: }
104:
105: public void deleteFile(URL url) throws IOException {
106: deleteFile(url.toString());
107: }
108:
109: public File getFile(URL url) throws DownloadException {
110: return getFile(url, null, false);
111: }
112:
113: public File getFile(String uri) throws DownloadException {
114: return getFile(uri, null, null);
115: }
116:
117: public File getFile(String uri, boolean deleteOnExit)
118: throws DownloadException {
119: return getFile(uri, null, null, deleteOnExit);
120: }
121:
122: public File getFile(ExtendedUri uri, Progress progress)
123: throws DownloadException {
124: return getFile(uri.getRemote(), progress, null, false);
125: }
126:
127: public File getFile(String uri, ClassLoader loader)
128: throws DownloadException {
129: return getFile(uri, null, loader);
130: }
131:
132: public File getFile(String uri, ClassLoader loader,
133: boolean deleteOnExit) throws DownloadException {
134: return getFile(uri, null, loader, deleteOnExit);
135: }
136:
137: public File getFile(URI uri, Progress progress)
138: throws DownloadException {
139: return getFile(uri, progress, null, false);
140: }
141:
142: public File getFile(String uri, Progress progress,
143: ClassLoader loader) throws DownloadException {
144: return getFile(uri, progress, loader, false);
145: }
146:
147: public File getFile(String uri, Progress progress,
148: ClassLoader loader, boolean deleteOnExit)
149: throws DownloadException {
150: final URI myUri;
151: try {
152: myUri = new URI(uri);
153: } catch (URISyntaxException ex) {
154: throw new DownloadException("uri:" + uri, ex);
155: }
156: return getFile(myUri, progress, loader, deleteOnExit);
157: }
158:
159: public File getFile(URI uri, boolean deleteOnExit)
160: throws DownloadException {
161: return getFile(uri, null, null, deleteOnExit);
162: }
163:
164: public File getFile(URI uri) throws DownloadException {
165: return getFile(uri, null, null, false);
166: }
167:
168: public File getFile(URI uri, Progress progress, ClassLoader loader,
169: boolean deleteOnExit) throws DownloadException {
170: final String cacheKey = uri.toString()
171: + (loader != null ? "#" + loader.toString() : "");
172:
173: if (cache.containsKey(cacheKey) && cache.get(cacheKey).exists()) {
174: return cache.get(cacheKey);
175: }
176: if (uri.getScheme().equals("file")) {
177: File file = new File(uri);
178: if (!file.exists())
179: throw new DownloadException("file not exist: " + uri);
180: return file;
181: } else if (uri.getScheme().equals(RESOURCE_SCHEME)) {
182: OutputStream out = null;
183: try {
184: String path = uri.getSchemeSpecificPart();
185: File file = new File(tmpDir, path.substring(path
186: .lastIndexOf('/')));
187: String fileName = file.getName();
188: File parent = file.getParentFile();
189: for (int i = 0; file.exists(); i++) {
190: file = new File(parent, fileName + "." + i);
191: }
192: file.createNewFile();
193: if (deleteOnExit) {
194: file.deleteOnExit();
195: }
196: final InputStream resource = (loader != null ? loader
197: : getClass().getClassLoader())
198: .getResourceAsStream(uri
199: .getSchemeSpecificPart());
200: out = new FileOutputStream(file);
201: if (resource == null)
202: throw new DownloadException(RESOURCE_SCHEME_PREFIX
203: + uri.getSchemeSpecificPart()
204: + " not found");
205: StreamUtils.transferData(resource, out);
206: cache.put(cacheKey, file);
207: return file;
208: } catch (IOException ex) {
209: throw new DownloadException("I/O error has occures", ex);
210: } finally {
211: if (out != null)
212: try {
213: out.close();
214: } catch (IOException ignord) {
215: }
216: }
217: } else if (uri.getScheme().startsWith("http")) {
218: try {
219: final File file = getFile(uri.toURL(), progress,
220: deleteOnExit);
221: cache.put(cacheKey, file);
222: return file;
223: } catch (MalformedURLException ex) {
224: throw new DownloadException("malformed url: " + uri, ex);
225: }
226: }
227: throw new DownloadException("unsupported sheme: "
228: + uri.getScheme());
229: }
230:
231: protected File getFile(final URL url, final Progress progress,
232: boolean deleteOnExit) throws DownloadException {
233: try {
234: final DownloadProgress dlProgress = new DownloadProgress(
235: progress, url);
236: DownloadManager.instance.registerListener(dlProgress);
237: File file = null;
238: file = FileProvider.getProvider().get(url);
239: if (deleteOnExit)
240: file.deleteOnExit();
241: return file;
242: } catch (DownloadException e) {
243: if (UiMode.getCurrentUiMode() == UiMode.SWING) {
244: new ProxySettingsDialog().execute();
245: return getFile(url, progress, deleteOnExit);
246: } else {
247: throw e;
248: }
249: }
250: }
251: }
|