FileDocCategorySizeDatePackage
ResourceDownloaderMetaRefreshImpl.javaAPI DocAzureus 3.0.3.46476Thu Feb 09 19:42:50 GMT 2006org.gudy.azureus2.pluginsimpl.local.utils.resourcedownloader

ResourceDownloaderMetaRefreshImpl

public class ResourceDownloaderMetaRefreshImpl extends ResourceDownloaderBaseImpl implements ResourceDownloaderListener
author
parg

Fields Summary
public static final int
MAX_FOLLOWS
protected ResourceDownloaderBaseImpl
delegate
protected ResourceDownloaderBaseImpl
current_delegate
protected long
size
protected boolean
cancelled
protected ResourceDownloader
current_downloader
protected Object
result
protected int
done_count
protected AESemaphore
done_sem
Constructors Summary
public ResourceDownloaderMetaRefreshImpl(ResourceDownloaderBaseImpl _parent, ResourceDownloader _delegate)

			
	
	
			
					 
	
		super( _parent );
		
		delegate		= (ResourceDownloaderBaseImpl)_delegate;
		
		delegate.setParent( this );
		
		current_delegate	= delegate;
	
Methods Summary
public voidasyncDownload()

		try{
			this_mon.enter();
		
			if ( cancelled ){
				
				done_sem.release();
				
				informFailed((ResourceDownloaderException)result);
				
			}else{
			
				done_count++;
							
				current_downloader = current_delegate.getClone( this );
				
				informActivity( getLogIndent() + "Downloading: " + getName());
	
				current_downloader.addListener( this );
				
				current_downloader.asyncDownload();
			}
		}finally{
			
			this_mon.exit();
		}
	
public voidcancel()

		setCancelled();
		
		try{
			this_mon.enter();
		
			result	= new ResourceDownloaderException( "Download cancelled");
			
			cancelled	= true;
			
			informFailed((ResourceDownloaderException)result );
			
			done_sem.release();
			
			if ( current_downloader != null ){
				
				current_downloader.cancel();
			}
		}finally{
			
			this_mon.exit();
		}
	
public booleancompleted(ResourceDownloader downloader, java.io.InputStream data)

		boolean	complete = false;
		
		try{
			if ( done_count == 1 ){
				
					// assumption is that there is a refresh tag
				
				boolean	marked = false;
				
				if ( data instanceof ByteArrayInputStream){
				
					data.mark(data.available());
					
					marked	= true;
				}
				
					// leave file open if marked so we can recover
				
				HTMLPage	page = HTMLPageFactory.loadPage( data, !marked );
				
				URL	redirect = page.getMetaRefreshURL();
				
				if ( redirect == null ){
					
					if ( !marked ){
						
						failed( downloader, new ResourceDownloaderException( "meta refresh tag not found and input stream not recoverable"));
						
					}else{
					
						data.reset();
					
						complete	= true;
					}
					
				}else{
				
					current_delegate = new ResourceDownloaderURLImpl( this, redirect );
					
					// informActivity( "meta-refresh -> " + current_delegate.getName());
					
					asyncDownload();
				}
				
				if ( marked && !complete){
					
					data.close();
				}
			}else{
				
				complete = true;
			}
			
			if ( complete ){
			
				if ( informComplete( data )){
					
					result	= data;
					
					done_sem.release();
				}
			}
		}catch( Throwable e ){
			
			failed( downloader, new ResourceDownloaderException("meta-refresh processing fails", e ));
		}
		
		return( true );
	
public java.io.InputStreamdownload()

		asyncDownload();
		
		done_sem.reserve();
		
		if ( result instanceof InputStream ){
			
			return((InputStream)result);
		}
		
		throw((ResourceDownloaderException)result);
	
public voidfailed(ResourceDownloader downloader, ResourceDownloaderException e)

		result		= e;
		
		done_sem.release();

		informFailed(e);
	
public ResourceDownloaderBaseImplgetClone(ResourceDownloaderBaseImpl parent)

		ResourceDownloaderMetaRefreshImpl c = new ResourceDownloaderMetaRefreshImpl( parent, delegate.getClone( this ));
		
		c.setSize( size );
		
		c.setProperties( this );

		return( c );
	
public java.lang.StringgetName()

		return( delegate.getName() + ": meta-refresh" );
	
public longgetSize()

	
		if ( size == -2 ){
			
			try{
				size = getSizeSupport();
				
			}finally{
				
				if ( size == -2 ){
					
					size = -1;
				}
				
				setSize( size );
			}
		}
	
		return( size );
	
protected longgetSizeSupport()

		try{
			ResourceDownloader	x = delegate.getClone( this );
			
			addReportListener( x );
			
			HTMLPage	page = HTMLPageFactory.loadPage( x.download());
			
			URL	redirect = page.getMetaRefreshURL();
	
			if ( redirect == null ){
				
				ResourceDownloaderBaseImpl c = delegate.getClone( this );
				
				addReportListener( c );
				
				long res = c.getSize();
				
				setProperties( c );
				
				return( res );
			}else{
				
				ResourceDownloaderURLImpl c =  new ResourceDownloaderURLImpl( getParent(), redirect );
				
				addReportListener( c );
				
				long res = c.getSize();
				
				setProperties( c );
				
				return( res );
			}
		}catch( HTMLException e ){
			
			throw( new ResourceDownloaderException( "getSize failed", e ));
		}
	
protected voidsetProperty(java.lang.String name, java.lang.Object value)

		setPropertySupport( name, value );
		
		delegate.setProperty( name, value );
	
protected voidsetSize(long l)

		size	= l;
		
		if ( size >= 0 ){
			
			delegate.setSize( size );
		}