31 |
31 |
import java.net.UnknownHostException;
|
32 |
32 |
import java.util.HashMap;
|
33 |
33 |
import java.util.Map;
|
|
34 |
import java.util.Objects;
|
|
35 |
import java.util.Queue;
|
|
36 |
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
37 |
import java.util.concurrent.Executor;
|
34 |
38 |
import org.apache.commons.io.FileUtils;
|
35 |
|
|
36 |
|
import org.slf4j.Logger;
|
37 |
|
import org.slf4j.LoggerFactory;
|
38 |
|
|
39 |
39 |
import org.gvsig.compat.net.ICancellable;
|
40 |
40 |
import org.gvsig.compat.se.net.downloader.Downloader;
|
41 |
41 |
import org.gvsig.tools.ToolsLocator;
|
42 |
42 |
import org.gvsig.tools.folders.FoldersManager;
|
|
43 |
import org.slf4j.Logger;
|
|
44 |
import org.slf4j.LoggerFactory;
|
43 |
45 |
|
44 |
46 |
/**
|
45 |
47 |
* Clase con m?todos de utilidad en el protocolo WMS
|
... | ... | |
63 |
65 |
private Map<Object,String> cachedURItoFile;
|
64 |
66 |
private Exception downloadException;
|
65 |
67 |
|
|
68 |
private ExecutorUI executorUI;
|
|
69 |
|
66 |
70 |
public SEDownloader() {
|
67 |
71 |
super();
|
|
72 |
executorUI = new ExecutorUI();
|
68 |
73 |
}
|
69 |
74 |
|
|
75 |
public ExecutorUI getExecutorUI() {
|
|
76 |
return executorUI;
|
|
77 |
}
|
|
78 |
|
70 |
79 |
/**
|
71 |
80 |
* Return the content of a file that has been created from a URL using the
|
72 |
81 |
* HTTP GET protocol
|
... | ... | |
179 |
188 |
@Override
|
180 |
189 |
public synchronized File downloadFile(URL url, String name, ICancellable cancel) throws IOException, ConnectException, UnknownHostException {
|
181 |
190 |
File f;
|
182 |
|
|
|
191 |
LOG.info("Download file "+Objects.toString(url));
|
183 |
192 |
if ((f = getPreviousDownloadedURL(url)) == null) {
|
184 |
193 |
f = getUniqueTemporaryFile(name);
|
185 |
194 |
|
... | ... | |
200 |
209 |
SEMonitor monitorObj = new SEMonitor(this, cancel);
|
201 |
210 |
Thread downloader = new Thread(createDownloaderTask(this, url, null, f, cancel.getID().getClass(),-1));
|
202 |
211 |
Thread monitor = new Thread(monitorObj);
|
203 |
|
|
|
212 |
downloader.setName("Downloader_"+downloader.getId());
|
|
213 |
monitor.setName("DownloaderMon_"+monitor.getId());
|
204 |
214 |
monitor.start();
|
205 |
215 |
downloader.start();
|
206 |
216 |
while (!getCanceled(cancel.getID()) && downloader.isAlive()) {
|
207 |
217 |
try {
|
208 |
218 |
Thread.sleep(latency);
|
|
219 |
this.executorUI.runCommands();
|
209 |
220 |
} catch (InterruptedException e) {
|
210 |
221 |
LOG.error("Error", e);
|
211 |
222 |
}
|
212 |
223 |
}
|
213 |
|
|
|
224 |
this.executorUI.clear();
|
214 |
225 |
try {
|
215 |
226 |
monitorObj.setFinish(true);
|
216 |
227 |
monitor.join();
|
... | ... | |
265 |
276 |
@Override
|
266 |
277 |
public synchronized File downloadFile(URL url, String data, String name, ICancellable cancel,int maxbytes) throws IOException, ConnectException, UnknownHostException {
|
267 |
278 |
File f;
|
|
279 |
LOG.info("Download file "+Objects.toString(url));
|
268 |
280 |
|
269 |
281 |
if ((f = getPreviousDownloadedURL(url, data)) == null) {
|
270 |
282 |
f = getUniqueTemporaryFile(name);
|
... | ... | |
288 |
300 |
);
|
289 |
301 |
|
290 |
302 |
Thread monitor = new Thread(monitorObj);
|
|
303 |
downloader.setName("Downloader_"+downloader.getId());
|
|
304 |
monitor.setName("DownloaderMon_"+monitor.getId());
|
291 |
305 |
monitor.start();
|
292 |
306 |
downloader.start();
|
293 |
307 |
while (!getCanceled(cancel.getID()) && downloader.isAlive()) {
|
294 |
308 |
try {
|
295 |
309 |
Thread.sleep(latency);
|
|
310 |
this.executorUI.runCommands();
|
296 |
311 |
} catch (InterruptedException e) {
|
297 |
312 |
LOG.error("Error", e);
|
298 |
313 |
}
|
... | ... | |
405 |
420 |
return this.latency;
|
406 |
421 |
}
|
407 |
422 |
|
408 |
|
protected Runnable createDownloaderTask(Downloader downloader, URL url, String data, File target, Object groupID,int maxbytes){
|
|
423 |
protected Runnable createDownloaderTask(SEDownloader downloader, URL url, String data, File target, Object groupID,int maxbytes){
|
409 |
424 |
SEDownloaderTask t = new SEDownloaderTask(downloader, url, data, target, groupID);
|
410 |
425 |
t.setMaxbytes(maxbytes);
|
411 |
426 |
return t;
|
... | ... | |
435 |
450 |
File f = foldersManager.getUniqueTemporaryFile("downloader",name);
|
436 |
451 |
return f;
|
437 |
452 |
}
|
|
453 |
|
|
454 |
private static class ExecutorUI implements Executor {
|
|
455 |
|
|
456 |
private final Queue<Runnable> queue = new ConcurrentLinkedQueue<>();
|
|
457 |
|
|
458 |
@Override
|
|
459 |
public void execute(Runnable command) {
|
|
460 |
try {
|
|
461 |
synchronized(command){
|
|
462 |
LOG.info("ExecuteUI add "+Objects.toString(command));
|
|
463 |
queue.add(command);
|
|
464 |
command.wait();
|
|
465 |
}
|
|
466 |
} catch (InterruptedException ex) {
|
|
467 |
LOG.warn("Can't execute command", ex);
|
|
468 |
}
|
|
469 |
}
|
|
470 |
|
|
471 |
public void runCommands() {
|
|
472 |
Runnable command;
|
|
473 |
while ((command = queue.poll())!=null) {
|
|
474 |
synchronized(command){
|
|
475 |
LOG.info("ExecuteUI run "+Objects.toString(command));
|
|
476 |
command.run();
|
|
477 |
command.notifyAll();
|
|
478 |
}
|
|
479 |
}
|
|
480 |
}
|
|
481 |
|
|
482 |
public void clear() {
|
|
483 |
queue.clear();
|
|
484 |
}
|
|
485 |
}
|
438 |
486 |
}
|