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

ResourceDownloaderAlternateImpl

public class ResourceDownloaderAlternateImpl extends ResourceDownloaderBaseImpl implements ResourceDownloaderListener
author
parg

Fields Summary
protected ResourceDownloader[]
delegates
protected int
max_to_try
protected boolean
random
protected boolean
cancelled
protected ResourceDownloader
current_downloader
protected int
current_index
protected Object
result
protected AESemaphore
done_sem
protected long
size
Constructors Summary
public ResourceDownloaderAlternateImpl(ResourceDownloaderBaseImpl _parent, ResourceDownloader[] _delegates, int _max_to_try, boolean _random)

	
	
	
			
				
									
								 
	
		super( _parent );
		
		delegates		= _delegates;
		max_to_try		= _max_to_try;
		random			= _random;
		
		for (int i=0;i<delegates.length;i++){
			
			((ResourceDownloaderBaseImpl)delegates[i]).setParent( this );
		}

		if ( max_to_try < 0 ){
			
			max_to_try = delegates.length;
			
		}else{
			
			max_to_try = Math.min( max_to_try, delegates.length );
		}
		
		if ( random ){
			
			List	l = new ArrayList(Arrays.asList( delegates ));
			
			delegates = new ResourceDownloader[delegates.length];
			
			for (int i=0;i<delegates.length;i++){
								
				delegates[i] = (ResourceDownloader)l.remove((int)(Math.random()*l.size()));
			}
		}
	
Methods Summary
public voidasyncDownload()

		try{
			this_mon.enter();
		
			if ( current_index == max_to_try || cancelled ){
				
				done_sem.release();
				
				informFailed((ResourceDownloaderException)result);
				
			}else{
			
				current_downloader = ((ResourceDownloaderBaseImpl)delegates[current_index]).getClone( this );
								
				informActivity( getLogIndent() + "Downloading: " + getName());
				
				current_index++;			
	
				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)

		if ( informComplete( data )){
			
			result	= data;
			
			done_sem.release();
			
			return( true );
		}
		
		return( false );
	
public java.io.InputStreamdownload()

		if( delegates.length == 0 ){
			
			ResourceDownloaderException error = new ResourceDownloaderException( "Alternate download fails - 0 alteratives");
			
			informFailed( error );
			
			throw( error );
		}
		
		asyncDownload();
		
		done_sem.reserve();
		
		if ( result instanceof InputStream ){
			
			return((InputStream)result);
		}
		
		throw((ResourceDownloaderException)result);
	
public voidfailed(ResourceDownloader downloader, ResourceDownloaderException e)

		result		= e;
		
		asyncDownload();
	
public ResourceDownloaderBaseImplgetClone(ResourceDownloaderBaseImpl parent)

		ResourceDownloader[]	clones = new ResourceDownloader[delegates.length];
		
		for (int i=0;i<delegates.length;i++){
			
			clones[i] = ((ResourceDownloaderBaseImpl)delegates[i]).getClone( this );
		}
		
		ResourceDownloaderAlternateImpl c = 
			new ResourceDownloaderAlternateImpl( parent, clones, max_to_try, random );
		
		c.setSize(size);
		
		c.setProperties( this );

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

		String	res = "[";
		
		for (int i=0;i<delegates.length;i++){
			
			res += (i==0?"":",") + delegates[i].getName();
		}
		
		return( res + "]");
	
public longgetSize()

		
		if( delegates.length == 0 ){
			
			ResourceDownloaderException error = new ResourceDownloaderException( "Alternate download fails - 0 alteratives");
			
			informFailed( error );
			
			throw( error );
		}
		
		if ( size != -2 ){
			
			return( size );
		}
		
		try{
			for (int i=0;i<max_to_try;i++){
				
				try{
					ResourceDownloaderBaseImpl c = ((ResourceDownloaderBaseImpl)delegates[i]).getClone( this );
					
					addReportListener( c );
					
					size = c.getSize();
					
					setProperties( c );

					break;
					
				}catch( ResourceDownloaderException e ){
					
					if ( i == delegates.length-1 ){
						
						throw( e );
					}
				}
			}
		}finally{
			
			if ( size == -2 ){
				
				size = -1;
			}
			
			setSize( size );
		}

		return( size );
	
protected voidsetProperty(java.lang.String name, java.lang.Object value)

		setPropertySupport( name, value );
		
		for (int i=0;i<delegates.length;i++){
			
			((ResourceDownloaderBaseImpl)delegates[i]).setProperty( name, value );
		}
	
protected voidsetSize(long l)

		size	= l;
		
		if ( size >= 0 ){
			
			for (int i=0;i<delegates.length;i++){
				
				((ResourceDownloaderBaseImpl)delegates[i]).setSize( size );
			}
		}