FileDocCategorySizeDatePackage
DownloadManagerImpl.javaAPI DocAzureus 3.0.3.419456Fri Sep 07 15:45:12 BST 2007org.gudy.azureus2.pluginsimpl.local.download

DownloadManagerImpl

public class DownloadManagerImpl extends Object implements org.gudy.azureus2.plugins.download.DownloadManager, DownloadManagerInitialisationAdapter
author
parg

Fields Summary
protected static DownloadManagerImpl
singleton
protected static AEMonitor
class_mon
private GlobalManager
global_manager
private org.gudy.azureus2.plugins.download.DownloadManagerStats
stats
private DownloadEventNotifierImpl
global_dl_notifier
private List
listeners
private com.aelitis.azureus.core.util.CopyOnWriteList
dwba_listeners
private AEMonitor
listeners_mon
private List
downloads
private Map
pending_dls
private Map
download_map
Constructors Summary
protected DownloadManagerImpl(AzureusCore _azureus_core)

		
	
	
			 
	
		//azureus_core	= _azureus_core;
		global_manager	= _azureus_core.getGlobalManager();
		
		stats = new DownloadManagerStatsImpl( global_manager );
		global_dl_notifier = new DownloadEventNotifierImpl(this);
		
		global_manager.addListener(
			new GlobalManagerListener()
			{
				public void
				downloadManagerAdded(
					DownloadManager	dm )
				{
					addDownloadManager( dm );
				}
				
				public void
				downloadManagerRemoved(
					DownloadManager	dm )
				{
					List			listeners_ref	= null;
					DownloadImpl	dl				= null;
					
					try{
						listeners_mon.enter();
						
						dl = (DownloadImpl)download_map.get( dm );
						
						if ( dl == null ){
							
							System.out.println( "DownloadManager:unknown manager removed");
							
						}else{
						
							downloads.remove( dl );
							
							download_map.remove( dm );
							
							pending_dls.remove( dm );
							
							dl.destroy();
						
							listeners_ref = listeners;
						}
						
					}finally{
						
						listeners_mon.exit();
					}
					
					if ( dl != null ){
							
						for (int i=0;i<listeners_ref.size();i++){
								
							((DownloadManagerListener)listeners_ref.get(i)).downloadRemoved( dl );
						}
					}
				}
				
				public void
				destroyInitiated()
				{
				}				
				
				public void
				destroyed()
				{	
				}
                
                
                public void seedingStatusChanged( boolean seeding_only_mode ){
                  //TODO
                }           
			});
		
		global_manager.addDownloadWillBeRemovedListener(
			new GlobalManagerDownloadWillBeRemovedListener()
			{
				public void
				downloadWillBeRemoved(
					DownloadManager	dm,
					boolean remove_torrent,
					boolean remove_data )
				
					throws GlobalManagerDownloadRemovalVetoException
				{					
					DownloadImpl	download = (DownloadImpl)download_map.get( dm );
				
					if ( download != null ){
					
						try{ 
							download.isRemovable();
							
						}catch( DownloadRemovalVetoException e ){
													
							throw( new GlobalManagerDownloadRemovalVetoException( e.getMessage(),e.isSilent()));
						}		
					}
				}
			});
	
Methods Summary
public org.gudy.azureus2.plugins.download.DownloadaddDownload(Torrent torrent, java.io.File torrent_file, java.io.File data_location)

		return( addDownload( torrent, torrent_file, data_location, getInitialState()));
	
public org.gudy.azureus2.plugins.download.DownloadaddDownload(Torrent torrent, java.io.File torrent_file, java.io.File data_location, int initial_state)

		if ( torrent_file == null ){
			
		    String torrent_dir = null;
		    
		    if( COConfigurationManager.getBooleanParameter("Save Torrent Files")){
		    	
		      try{
		      	
		      	torrent_dir = COConfigurationManager.getDirectoryParameter("General_sDefaultTorrent_Directory");
		        
		      }catch(Exception egnore){}
		    }
		    
		    if ( torrent_dir == null || torrent_dir.length() == 0 ){
		    	
		    	throw( new DownloadException("DownloadManager::addDownload: default torrent save directory must be configured" ));
		    }
		
		    torrent_file = new File( torrent_dir + File.separator + torrent.getName() + ".torrent" );
		    
		    try{
		    	torrent.writeToFile( torrent_file );
		    	
		    }catch( TorrentException e ){
		    	
		    	throw( new DownloadException("DownloadManager::addDownload: failed to write torrent to '" + torrent_file.toString() + "'", e ));	    	
		    }
		}
		
		else {
			if (!torrent_file.exists()) {
				throw new DownloadException("DownloadManager::addDownload: torrent file does not exist - " + torrent_file.toString()); 
			}
			else if (!torrent_file.isFile()) {
				throw new DownloadException("DownloadManager::addDownload: torrent filepath given is not a file - " + torrent_file.toString());
			}
		}
		
		if ( data_location == null ){
			
		    String data_dir = COConfigurationManager.getStringParameter("Default save path");
		    
		    if ( data_dir == null || data_dir.length() == 0 ){
		    	
		    	throw( new DownloadException("DownloadManager::addDownload: default data save directory must be configured" ));
		    }
		    
		    data_location = new File(data_dir); 
		    
		    FileUtil.mkdirs(data_location);
		}

		byte[] hash = null;
		try {
			hash = torrent.getHash();
		} catch (Exception e) { }
		
		boolean	for_seeding = torrent.isComplete();
		
		DownloadManager dm = global_manager.addDownloadManager(
				torrent_file.toString(), hash, data_location.toString(),
				initial_state, true, for_seeding, null );
		
		if ( dm == null ){
			
			throw( new DownloadException( "DownloadManager::addDownload - failed, download may already in the process of being added"));
		}
		
		addDownloadManager( dm );
		
		return( getDownload( dm ));
	
public voidaddDownload(java.io.File fileName)

		UIManagerImpl.fireEvent( UIManagerEvent.ET_OPEN_TORRENT_VIA_FILE, fileName );
	
public voidaddDownload(java.net.URL url)

		addDownload(url,null,true);
	
public voidaddDownload(java.net.URL url, boolean auto_download)

		addDownload(url,null,auto_download);	
	
public voidaddDownload(java.net.URL url, java.net.URL referrer)

		addDownload(url,referrer,true);
	
public voidaddDownload(java.net.URL url, java.net.URL referrer, boolean auto_download)

		UIManagerImpl.fireEvent( UIManagerEvent.ET_OPEN_TORRENT_VIA_URL, new Object[]{ url, referrer, new Boolean( auto_download )});
	
public org.gudy.azureus2.plugins.download.DownloadaddDownload(Torrent torrent)

	 
	    return( addDownload( torrent, null, null ));
	
protected voidaddDownloadManager(DownloadManager dm)

		List			listeners_ref 	= null;
		DownloadImpl	dl				= null;
		
		try{
			listeners_mon.enter();
			
			if ( download_map.get(dm) == null ){
	
				dl = (DownloadImpl)pending_dls.remove( dm );
				
				if ( dl == null ){
					
					dl = new DownloadImpl(dm);
				}
				
				downloads.add( dl );
				
				download_map.put( dm, dl );
				
				listeners_ref = listeners;
			}
		}finally{
			
			listeners_mon.exit();
		}
		
		if ( dl != null ){
			
			for (int i=0;i<listeners_ref.size();i++){
					
				try{
					((DownloadManagerListener)listeners_ref.get(i)).downloadAdded( dl );
					
				}catch( Throwable e ){
						
					Debug.printStackTrace( e );
				}
			}
		}
	
public org.gudy.azureus2.plugins.download.DownloadaddDownloadStopped(Torrent torrent, java.io.File torrent_location, java.io.File data_location)

		return( addDownload( torrent, torrent_location, data_location, DownloadManager.STATE_STOPPED ));
	
public voidaddDownloadWillBeAddedListener(org.gudy.azureus2.plugins.download.DownloadWillBeAddedListener listener)

		try{
			listeners_mon.enter();
			
			dwba_listeners.add( listener );
			
			if ( dwba_listeners.size() == 1 ){
				
				global_manager.addDownloadManagerInitialisationAdapter( this );
			}
			
		}finally{
			listeners_mon.exit();
		}	
	
public voidaddExternalDownload(org.gudy.azureus2.plugins.download.Download download)

		List			listeners_ref 	= null;
		
		try{
			listeners_mon.enter();
			
			if ( downloads.contains( download )){
	
				return;
			}
	
			downloads.add( download );
								
			listeners_ref = listeners;
			
		}finally{
			
			listeners_mon.exit();
		}
					
		for (int i=0;i<listeners_ref.size();i++){
				
			try{
				((DownloadManagerListener)listeners_ref.get(i)).downloadAdded( download );
				
			}catch( Throwable e ){
					
				Debug.printStackTrace( e );
			}
		}
	
public voidaddListener(org.gudy.azureus2.plugins.download.DownloadManagerListener l)

addListener(l, true);
public voidaddListener(org.gudy.azureus2.plugins.download.DownloadManagerListener l, boolean notify_of_current_downloads)

		List downloads_copy = null;

		try {
			listeners_mon.enter();
			List new_listeners = new ArrayList(listeners);
			new_listeners.add(l);
			listeners = new_listeners;
			if (notify_of_current_downloads) {
				downloads_copy = new ArrayList(downloads);
			}
		}
		finally {
			listeners_mon.exit();
		}

		if (downloads_copy != null) {
			for (int i = 0; i < downloads_copy.size(); i++) {
				try {l.downloadAdded((Download) downloads_copy.get(i));}
				catch (Throwable e) {Debug.printStackTrace(e);}
			}
		}
	
public org.gudy.azureus2.plugins.download.DownloadaddNonPersistentDownload(Torrent torrent, java.io.File torrent_file, java.io.File data_location)


		byte[] hash = null;
		try {
			hash = torrent.getHash();
		} catch (Exception e) { }

		DownloadManager dm = global_manager.addDownloadManager(
				torrent_file.toString(), hash, data_location.toString(),
				getInitialState(), false);
		
		if ( dm == null ){
			
			throw( new DownloadException( "DownloadManager::addDownload - failed"));
		}
		
		addDownloadManager( dm );
		
		return( getDownload( dm ));
	
public booleancanPauseDownloads()

		return global_manager.canPauseDownloads();
	
public booleancanResumeDownloads()

		return global_manager.canResumeDownloads();
	
protected DownloadImplgetDownload(DownloadManager dm)

		DownloadImpl dl = (DownloadImpl)download_map.get(dm);
		
		if ( dl == null ){
			
			throw( new DownloadException("DownloadManager::getDownload: download not found"));
		}
		
		return( dl );
	
public org.gudy.azureus2.plugins.download.DownloadgetDownload(org.gudy.azureus2.core3.disk.DiskManager dm)

		List	dls = global_manager.getDownloadManagers();

		for (int i=0;i<dls.size();i++){
			
			DownloadManager	man = (DownloadManager)dls.get(i);
			
			if ( man.getDiskManager() == dm ){
				
				return( getDownload( man.getTorrent()));
			}
		}
		
		return( null );
	
protected org.gudy.azureus2.plugins.download.DownloadgetDownload(TOTorrent torrent)

		if ( torrent != null ){
			
			for (int i=0;i<downloads.size();i++){
				
				Download	dl = (Download)downloads.get(i);
				
				TorrentImpl	t = (TorrentImpl)dl.getTorrent();
				
					// can be null if broken torrent
				
				if ( t == null ){
					
					continue;
				}
				
				if ( t.getTorrent().hasSameHashAs( torrent )){
					
					return( dl );
				}
			}
		}
		
		throw( new DownloadException("DownloadManager::getDownload: download not found"));
	
public org.gudy.azureus2.plugins.download.DownloadgetDownload(Torrent _torrent)

		TorrentImpl	torrent = (TorrentImpl)_torrent;
		
		try{
			return( getDownload( torrent.getTorrent()));
			
		}catch( DownloadException e ){
		}
		
		return( null );
	
public org.gudy.azureus2.plugins.download.DownloadgetDownload(byte[] hash)

		List	dls = global_manager.getDownloadManagers();

		for (int i=0;i<dls.size();i++){
			
			DownloadManager	man = (DownloadManager)dls.get(i);
			
				// torrent can be null if download manager torrent file read fails
			
			TOTorrent	torrent = man.getTorrent();
			
			if ( torrent != null ){
				
				try{
					if ( Arrays.equals( torrent.getHash(), hash )){
				
						return( getDownload( torrent ));
					}
				}catch( DownloadException e ){
					
						// not found
					
				}catch( TOTorrentException e ){
					
					Debug.printStackTrace( e );
				}
			}
		}
		
		return( null );
	
public static DownloadImpl[]getDownloadStatic(DownloadManager[] dm)

		ArrayList res = new ArrayList(dm.length);
		for (int i=0; i<dm.length; i++) {
			try {res.add(getDownloadStatic(dm[i]));}
			catch (DownloadException de) {}
		}
		return (DownloadImpl[])res.toArray(new DownloadImpl[res.size()]);
	
public static DownloadImplgetDownloadStatic(DownloadManager dm)
Retrieve the plugin Downlaod object related to the DownloadManager

param
dm DownloadManager to find
return
plugin object
throws
DownloadException

		if ( singleton != null ){
			
			return( singleton.getDownload( dm ));
		}
		
		throw( new DownloadException( "DownloadManager not initialised"));
	
public static org.gudy.azureus2.plugins.download.DownloadgetDownloadStatic(org.gudy.azureus2.core3.disk.DiskManager dm)

		if ( singleton != null ){
			
			return( singleton.getDownload( dm ));
		}
		
		throw( new DownloadException( "DownloadManager not initialised"));
	
public static org.gudy.azureus2.plugins.download.DownloadgetDownloadStatic(TOTorrent torrent)

		if ( singleton != null ){
			
			return( singleton.getDownload( torrent ));
		}
		
		throw( new DownloadException( "DownloadManager not initialised"));
	
public org.gudy.azureus2.plugins.download.Download[]getDownloads()

		List	res_l = new ArrayList();
	
		// we have to use the global manager's ordering as it
		// hold this

		List dms = global_manager.getDownloadManagers();

		try{
			listeners_mon.enter();

			for (int i=0;i<dms.size();i++){
			
				Object	dl = download_map.get( dms.get(i));
				
				if ( dl != null ){
					
					res_l.add( dl );
				}
			}
			
			if ( res_l.size() < downloads.size()){
				
					// now add in any external downloads 
				
				for (int i=0;i<downloads.size();i++){
					
					Download	download = (Download)downloads.get(i);
			
					if ( !res_l.contains( download )){
						
						res_l.add( download );
					}
				}
			}
		}finally{
			
			listeners_mon.exit();
		}
		
		Download[]	res = new Download[res_l.size()];
			
		res_l.toArray( res );
			
		return( res );
	
public org.gudy.azureus2.plugins.download.Download[]getDownloads(boolean bSorted)

		if (bSorted){
	  
			return getDownloads();
		}
	  
		try{
			listeners_mon.enter();
		
			Download[]	res = new Download[downloads.size()];
			
			downloads.toArray( res );
			
			return( res );
			
		}finally{
			
			listeners_mon.exit();
		}
	
public org.gudy.azureus2.plugins.download.DownloadEventNotifiergetGlobalDownloadEventNotifier()

		return this.global_dl_notifier;
	
protected intgetInitialState()

	  	boolean	default_start_stopped = COConfigurationManager.getBooleanParameter( "Default Start Torrents Stopped" );

        return( default_start_stopped?DownloadManager.STATE_STOPPED:DownloadManager.STATE_WAITING);
	
public static org.gudy.azureus2.pluginsimpl.local.download.DownloadManagerImplgetSingleton(AzureusCore azureus_core)

	
	  
	
			 
	
		try{
			class_mon.enter();
	
			if ( singleton == null ){
				
				singleton = new DownloadManagerImpl( azureus_core );
			}
			
			return( singleton );
			
		}finally{
			
			class_mon.exit();
		}
	
public org.gudy.azureus2.plugins.download.DownloadManagerStatsgetStats()

		return( stats );
	
public voidinitialised(DownloadManager manager)

		DownloadImpl	dl;
		
		try{
			listeners_mon.enter();
			
			dl = new DownloadImpl( manager );
			
			pending_dls.put( manager, dl );
			
		}finally{
			
			listeners_mon.exit();
		}
		
		Iterator	it = dwba_listeners.iterator();
		
		while( it.hasNext()){
			
			try{
				((DownloadWillBeAddedListener)it.next()).initialised(dl);
				
			}catch( Throwable e ){
				
				Debug.printStackTrace(e);
			}
		}
	
public booleanisExteralDownload(org.gudy.azureus2.plugins.download.Download download)

		return( !( download instanceof DownloadImpl ));
	
public booleanisSeedingOnly()

		return( global_manager.isSeedingOnly());
	
public voidpauseDownloads()

		global_manager.pauseDownloads();
	
public voidremoveDownloadWillBeAddedListener(org.gudy.azureus2.plugins.download.DownloadWillBeAddedListener listener)

		try{
			listeners_mon.enter();
			
			dwba_listeners.remove( listener );
			
			if ( dwba_listeners.size() == 0 ){
				
				global_manager.removeDownloadManagerInitialisationAdapter( this );
			}
			
		}finally{
			listeners_mon.exit();
		}	
	
public voidremoveExternalDownload(org.gudy.azureus2.plugins.download.Download download)

		List			listeners_ref 	= null;
		
		try{
			listeners_mon.enter();
			
			if ( !downloads.contains( download )){
	
				return;
			}
	
			downloads.remove( download );
								
			listeners_ref = listeners;
			
		}finally{
			
			listeners_mon.exit();
		}
					
		for (int i=0;i<listeners_ref.size();i++){
				
			try{
				((DownloadManagerListener)listeners_ref.get(i)).downloadRemoved( download );
				
			}catch( Throwable e ){
					
				Debug.printStackTrace( e );
			}
		}
	
public voidremoveListener(org.gudy.azureus2.plugins.download.DownloadManagerListener l)

removeListener(l, false);
public voidremoveListener(org.gudy.azureus2.plugins.download.DownloadManagerListener l, boolean notify_of_current_downloads)

		List downloads_copy = null;
		
		try {
			listeners_mon.enter();
			List new_listeners = new ArrayList(listeners);
			new_listeners.remove(l);
			listeners = new_listeners;
			if (notify_of_current_downloads) {
				downloads_copy = new ArrayList(downloads);
			}
		}
		finally {
			listeners_mon.exit();
		}

		if (downloads_copy != null) {
			for (int i = 0; i < downloads_copy.size(); i++) {
				try {l.downloadRemoved((Download) downloads_copy.get(i));}
				catch (Throwable e) {Debug.printStackTrace(e);}
			}
		}
	
	
public voidresumeDownloads()

		global_manager.resumeDownloads();
	
public voidstartAllDownloads()

		global_manager.startAllDownloads();
	
public voidstopAllDownloads()

		global_manager.stopAllDownloads();