Revision 47844

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.compat/org.gvsig.compat.se/src/main/java/org/gvsig/compat/se/net/downloader/se/SEDownloader.java
35 35
import java.util.Queue;
36 36
import java.util.concurrent.ConcurrentLinkedQueue;
37 37
import java.util.concurrent.Executor;
38
import org.apache.commons.collections4.map.LRUMap;
38 39
import org.apache.commons.io.FileUtils;
39 40
import org.apache.http.entity.ContentType;
40 41
import org.gvsig.compat.net.ICancellable;
......
54 55
//    private boolean canceled;
55 56
    private final long latency = 500;
56 57
    private static final Logger LOG = LoggerFactory.getLogger(SEDownloader.class);
58
    private static final int MAX_URL_TO_FILE_CACHE = 1000;
57 59

  
58 60
    /**
59 61
     * Used to cancel a group of files
......
152 154
    @Override
153 155
    public void addDownloadedURL(URL url, String filePath) {
154 156
        if (cachedURItoFile == null) {
155
            cachedURItoFile = new HashMap();
157
            cachedURItoFile = new LRUMap(MAX_URL_TO_FILE_CACHE);
156 158
        }
157 159

  
158 160
        URI theuri;
......
252 254
                }
253 255
            }
254 256
        } else {
255
            LOG.info(url.toString() + " cached at '" + f.getAbsolutePath() + "'");
257
            LOG.info("CACHED "+url.toString() + " at '" + f.getAbsolutePath() + "'");
258
            
256 259
        }
257 260

  
258 261
        return f;
......
287 290
    
288 291
    @Override
289 292
    public synchronized File downloadFile(URL url, String method, ContentType contenttype, String data, String name, ICancellable cancel,int maxbytes) throws IOException, ConnectException, UnknownHostException {
290
        File f;
291
        LOG.info("Download file "+Objects.toString(url));
293
        return downloadFile(url, method, contenttype, data, name, cancel, maxbytes, true);
294
    }
295
    
296
    @Override
297
    public synchronized File downloadFile(URL url, String method, ContentType contenttype, String data, String name, ICancellable cancel, int maxbytes, boolean useCache) throws IOException, ConnectException, UnknownHostException {
298
        LOG.info("Download file " + Objects.toString(url));
299
        if (useCache) {
300
            File target = getPreviousDownloadedURL(url, data);
301
            if (target != null) {
302
                return target;
303
            }
304
        }
305
        File f = getUniqueTemporaryFile(name);
292 306

  
293
        if ((f = getPreviousDownloadedURL(url, data)) == null) {
294
            f = getUniqueTemporaryFile(name);
307
        if (cancel == null) {
308
            cancel = new ICancellable() {
309
                @Override
310
                public boolean isCanceled() {
311
                    return false;
312
                }
295 313

  
296
            if (cancel == null) {
297
                cancel = new ICancellable() {
298
                    @Override
299
                    public boolean isCanceled() {
300
                        return false;
301
                    }
302

  
303
                    @Override
304
                    public Object getID() {
305
                        return SEDownloader.class.getName();
306
                    }
307
                };
308
            }
309
            SEMonitor monitorObj = new SEMonitor(this, cancel);
310
            Thread downloader = new Thread(
311
                    createDownloaderTask(this, url, method, contenttype, data, f, cancel.getID(),maxbytes)
312
            );
313
            
314
            Thread monitor = new Thread(monitorObj);
315
            downloader.setName("Downloader_"+downloader.getId());
316
            monitor.setName("DownloaderMon_"+monitor.getId());
317
            monitor.start();
318
            downloader.start();
319
            while (!getCanceled(cancel.getID()) && downloader.isAlive()) {
320
                try {
321
                    Thread.sleep(latency);
322
                    this.executorUI.runCommands();
323
                } catch (InterruptedException e) {
324
                    LOG.error("Error", e);
314
                @Override
315
                public Object getID() {
316
                    return SEDownloader.class.getName();
325 317
                }
326
            }
318
            };
319
        }
320
        SEMonitor monitorObj = new SEMonitor(this, cancel);
321
        Thread downloader = new Thread(
322
            createDownloaderTask(this, url, method, contenttype, data, f, cancel.getID(), maxbytes)
323
        );
327 324

  
325
        Thread monitor = new Thread(monitorObj);
326
        downloader.setName("Downloader_" + downloader.getId());
327
        monitor.setName("DownloaderMon_" + monitor.getId());
328
        monitor.start();
329
        downloader.start();
330
        while (!getCanceled(cancel.getID()) && downloader.isAlive()) {
328 331
            try {
329
                monitorObj.setFinish(true);
330
                monitor.join();
331
                downloader.join();
332
            } catch (InterruptedException e1) {
333
                LOG.warn(e1.getMessage());
332
                Thread.sleep(latency);
333
                this.executorUI.runCommands();
334
            } catch (InterruptedException e) {
335
                LOG.error("Error", e);
334 336
            }
335
            downloader = null;
336
            monitor = null;
337
        }
337 338

  
338
            if (getCanceled(cancel.getID())) {
339
                return null;
339
        try {
340
            monitorObj.setFinish(true);
341
            monitor.join();
342
            downloader.join();
343
        } catch (InterruptedException e1) {
344
            LOG.warn(e1.getMessage());
345
        }
346
        downloader = null;
347
        monitor = null;
348

  
349
        if (getCanceled(cancel.getID())) {
350
            return null;
351
        }
352
        if (this.downloadException != null) {
353
            Exception e = this.downloadException;
354
            if (e instanceof FileNotFoundException) {
355
                throw (IOException) e;
356
            } else if (e instanceof IOException) {
357
                throw (IOException) e;
358
            } else if (e instanceof ConnectException) {
359
                throw (ConnectException) e;
360
            } else if (e instanceof UnknownHostException) {
361
                throw (UnknownHostException) e;
340 362
            }
341
            if (this.downloadException != null) {
342
                Exception e = this.downloadException;
343
                if (e instanceof FileNotFoundException) {
344
                    throw (IOException) e;
345
                } else if (e instanceof IOException) {
346
                    throw (IOException) e;
347
                } else if (e instanceof ConnectException) {
348
                    throw (ConnectException) e;
349
                } else if (e instanceof UnknownHostException) {
350
                    throw (UnknownHostException) e;
351
                }
352
            }
353
        } else {
354
            LOG.info(url.toString() + " cached at '" + f.getAbsolutePath() + "'");
355 363
        }
356 364

  
357 365
        return f;
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.compat/org.gvsig.compat.se/pom.xml
36 36
            <artifactId>httpcore</artifactId>
37 37
            <scope>compile</scope>
38 38
        </dependency>
39
        <dependency>
40
            <groupId>org.apache.commons</groupId>
41
            <artifactId>commons-collections4</artifactId>
42
            <scope>compile</scope>
43
        </dependency>
39 44
    </dependencies>
40 45

  
41 46
    <description>This library contains the implementation for JavaSE 1.5 of the API definition of the library for compatibility with java 1.4 in JVM CDC</description>
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.compat/org.gvsig.compat.api/src/main/java/org/gvsig/compat/net/Downloader.java
77 77

  
78 78
    public File downloadFile(URL url, String method, ContentType contentType, String data, String name, ICancellable cancel,int maxbytes) throws IOException, ConnectException, UnknownHostException;
79 79

  
80
    public File downloadFile(URL url, String method, ContentType contentType, String data, String name, ICancellable cancel,int maxbytes, boolean useCache) throws IOException, ConnectException, UnknownHostException;
81

  
80 82
    public void removeURL(URL url);
81 83
    
82 84
    public void removeURL(Object url);

Also available in: Unified diff