Revision 34 1.10/trunk/libraries/lib3DMap/src/org/gvsig/gvsig3d/cacheservices/FLayerCacheService.java

View differences:

FLayerCacheService.java
73 73
	WCSStatus _wcsStatus = null;
74 74

  
75 75
	Thread _requestThead = null;
76
	Thread _downloadMonitorThread = null;
76 77
	private RequestTileFromLayerThread _requestTileFromLayerThread;
78
	private DownloadMonitor _downloadMonitor;
77 79

  
78 80
	private static Logger _logger = Logger.getLogger(FLayerCacheService.class
79 81
			.getName());
......
112 114

  
113 115
	}
114 116

  
115
	protected class MyCancel implements Cancellable, ICancellable {
116

  
117
		public boolean isCanceled() {
118
			return false;
117
	protected class MyCancel implements Cancellable {
118
		private boolean cancel = false;
119
		
120
		public MyCancel()
121
		{
119 122
		}
120

  
123
		
121 124
		public void setCanceled(boolean canceled) {
122

  
125
			cancel = canceled;
123 126
		}
124

  
125
		/* if you don�t put and ID the wms donwload will be fail */
127
		
128
		public boolean isCanceled() {
129
			return cancel;
130
		}
131
	}
132
	
133
	protected class MyCancellable implements ICancellable
134
	{
135
		private Cancellable original;
136
		public MyCancellable(Cancellable cancelOriginal)
137
		{
138
			this.original = cancelOriginal;
139
		}
140
		public void setCanceled(boolean canceled)
141
		{
142
			original.setCanceled(canceled);
143
		}
144
		public boolean isCanceled() {
145
			if (original == null) return false;
146
			return original.isCanceled();
147
		}
126 148
		public Object getID() {
127 149
			return this;
128 150
		}
......
261 283
							_wmsStatus.setOnlineResource(getMapStr);
262 284
						}
263 285
						_wmsStatus.setExtent(tileExtent);
264
						File f = _wmsDriver.getMap(_wmsStatus, new MyCancel());
286
						
287
						// Build a cancellable
288
						MyCancellable cancelDownload = new MyCancellable(new MyCancel());
289
						
290
						//Run monitor thread for remote services
291
						if(_downloadMonitorThread == null) {
292
							_downloadMonitor = new DownloadMonitor();
293
							_downloadMonitorThread = new Thread(_downloadMonitor);
294
							_downloadMonitorThread.start();
295
						}
296
						_downloadMonitor.monitorThis(cancelDownload, System.currentTimeMillis());
297
						File f = _wmsDriver.getMap(_wmsStatus, cancelDownload);
298
						_downloadMonitor.endMonitor();
265 299
						if (f == null)
266 300
							return;
267 301
						synchronized (cacheMutex) {
......
376 410
							_wmsStatus.setOnlineResource(getMapStr);
377 411
						}
378 412
						_wmsStatus.setExtent(tileExtent);
379
						File f = _wmsDriver.getMap(_wmsStatus, new MyCancel());
413
						
414
						// Build a cancellable
415
						MyCancellable cancelDownload = new MyCancellable(new MyCancel());
416
						
417
						//Run monitor thread for remote services
418
						if(_downloadMonitorThread == null) {
419
							_downloadMonitor = new DownloadMonitor();
420
							_downloadMonitorThread = new Thread(_downloadMonitor);
421
							_downloadMonitorThread.start();
422
						}
423
						_downloadMonitor.monitorThis(cancelDownload, System.currentTimeMillis());
424
						File f = _wmsDriver.getMap(_wmsStatus, cancelDownload);
425
						_downloadMonitor.endMonitor();
380 426
						if (f == null)
381 427
							return;
382 428

  
......
414 460
						}
415 461
						_wcsStatus.setExtent(tileExtent);
416 462

  
417
						File f = _wcsDriver.getCoverage(_wcsStatus, new MyCancel());
463
						// Build a cancellable
464
						MyCancellable cancelDownload = new MyCancellable(new MyCancel());
465
						
466
						//Run monitor thread for remote services
467
						if(_downloadMonitorThread == null) {
468
							_downloadMonitor = new DownloadMonitor();
469
							_downloadMonitorThread = new Thread(_downloadMonitor);
470
							_downloadMonitorThread.start();
471
						}
472
						_downloadMonitor.monitorThis(cancelDownload, System.currentTimeMillis());
473
						File f = _wcsDriver.getCoverage(_wcsStatus, new MyCancellable(new MyCancel()));
474
						_downloadMonitor.endMonitor();
418 475
						if (f == null)
419 476
							return;
420 477
						synchronized (cacheMutex) {
......
555 612
		return image;
556 613
	}
557 614

  
615

  
558 616
	public boolean intersectsLayer(Rectangle2D extent) {
559 617
		return _lyrExtentRect.intersects(extent);
560 618
	}
561 619

  
562 620
	public void free() throws Throwable {
563 621
		_logger.info("finalizing cache service and thread");
564
		_requestTileFromLayerThread.setDone(true);
622
		if(_requestTileFromLayerThread != null)
623
			_requestTileFromLayerThread.setDone(true);
624
		if(_downloadMonitor != null)
625
			_downloadMonitor.setDone(true);
565 626
		_requestThead.join();
627
		_downloadMonitorThread.join();
566 628
	}
567 629

  
568 630
	public WMSStatus getWMSStatus(){
......
577 639
		_requestThead.join();
578 640
		super.finalize();
579 641
	}*/
642
	
643
	protected class DownloadMonitor implements Runnable 
644
	{
645
		private volatile boolean _done = false;
646
		private volatile boolean _monitoring = false;
647
		private volatile long _time = 0;
648
		private volatile MyCancellable _cancellable = null;
580 649

  
650
		public void run() {
651
			while(!_done) {
652
				try {
653
					if(_monitoring && (System.currentTimeMillis() - _time > 5000))
654
					{
655
						if(_cancellable != null) _cancellable.setCanceled(true);
656
					}
657
					Thread.sleep(500);
658
				} catch (InterruptedException e) {
659
					// TODO Auto-generated catch block
660
					e.printStackTrace();
661
				}
662
			}
663
		}
664
		
665
		public void monitorThis(MyCancellable cancellable, long initDownloadTime)
666
		{
667
			_monitoring = true;
668
			_time = initDownloadTime;
669
			_cancellable = cancellable;
670
		}
671
		
672
		public void endMonitor()
673
		{
674
			_monitoring = false;
675
			// Cancel the download to finish the gvSIG monitor because is buged and 
676
			// it never ends.
677
			if(_cancellable != null) _cancellable.setCanceled(true);
678
		}
679

  
680
		public void setDone(boolean value) { _done = value; }
681
	}
581 682
}

Also available in: Unified diff