FileDocCategorySizeDatePackage
TorrentManagerImpl.javaAPI DocAzureus 3.0.3.413398Tue Jun 12 00:49:22 BST 2007org.gudy.azureus2.pluginsimpl.local.torrent

TorrentManagerImpl

public class TorrentManagerImpl extends Object implements TorrentManager, TOTorrentProgressListener
author
parg

Fields Summary
private static TorrentManagerImpl
singleton
private static AEMonitor
class_mon
private static TorrentAttribute
category_attribute
private static TorrentAttribute
share_properties_attribute
private static TorrentAttribute
networks_attribute
private static TorrentAttribute
peer_sources_attribute
private static TorrentAttribute
tr_ext_attribute
private static TorrentAttribute
disp_name_attribute
private static TorrentAttribute
comment_attribute
private static TorrentAttribute
relative_save_path_attribute
private static Map
attribute_map
protected static List
listeners
protected org.gudy.azureus2.plugins.PluginInterface
plugin_interface
Constructors Summary
protected TorrentManagerImpl(org.gudy.azureus2.plugins.PluginInterface _pi)


	
	
				 
	
		plugin_interface	= _pi;
	
Methods Summary
public voidaddListener(TorrentManagerListener l)

		try{
			class_mon.enter();

			ArrayList	new_listeners = new ArrayList( listeners );

			new_listeners.add( l );

			listeners	= new_listeners;
		}finally{

			class_mon.exit();
		}
	
public TorrentcreateFromBEncodedData(byte[] data, int preserve)

		ByteArrayInputStream bais = new ByteArrayInputStream(data);
		try {
			TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedInputStream( bais );
			return new TorrentImpl(plugin_interface, preserveFields(torrent,preserve));
		} catch (TOTorrentException e) {
			throw new TorrentException ("Failed to read TorrentData", e);
		} finally {
			try {
				bais.close();
			} catch (IOException e) {}
		}
	
public TorrentcreateFromBEncodedData(byte[] data)

		ByteArrayInputStream	is = null;

		try{
			is = new ByteArrayInputStream( data );

			return( new TorrentImpl(plugin_interface,TorrentUtils.readFromBEncodedInputStream(is)));

		}catch( TOTorrentException e ){

			throw( new TorrentException( "TorrentManager::createFromBEncodedData Fails", e ));

		}finally{

			try{
				is.close();

			}catch( Throwable e ){

				Debug.printStackTrace( e );
			}
		}
	
public TorrentcreateFromBEncodedFile(java.io.File file, int preserve)

		FileInputStream fis = null;
		try {
			fis = new FileInputStream (file);
			TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedInputStream( fis );
			return new TorrentImpl(plugin_interface, preserveFields(torrent,preserve));
		} catch (FileNotFoundException e) {
			throw new TorrentException ("Failed to read from TorrentFile", e);
		} catch (TOTorrentException e) {
			throw new TorrentException ("Failed to read TorrentData", e);
		} finally {
			if (fis != null)
				try {
					fis.close();
				} catch (IOException e) {}
		}
	
public TorrentcreateFromBEncodedFile(java.io.File file)

		return( createFromBEncodedFile( file, false ));
	
public TorrentcreateFromBEncodedFile(java.io.File file, boolean for_seeding)

		try{
			TOTorrent	torrent;

			if ( for_seeding ){

				torrent = TorrentUtils.readFromFile( file, true, true );

			}else{

				torrent = TorrentUtils.readFromFile( file, false );
			}

			return( new TorrentImpl(plugin_interface,torrent));

		}catch( TOTorrentException e ){

			throw( new TorrentException( "TorrentManager::createFromBEncodedFile Fails", e ));
		}
	
public TorrentcreateFromBEncodedInputStream(java.io.InputStream data, int preserve)

		try {
			TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedInputStream( data );
			return new TorrentImpl(plugin_interface, preserveFields(torrent,preserve));
		} catch (TOTorrentException e) {
			throw new TorrentException ("Failed to read TorrentData", e);
		}

	
public TorrentcreateFromBEncodedInputStream(java.io.InputStream data)

		try{
			return( new TorrentImpl(plugin_interface,TorrentUtils.readFromBEncodedInputStream( data )));

		}catch( TOTorrentException e ){

			throw( new TorrentException( "TorrentManager::createFromBEncodedFile Fails", e ));
		}
	
public TorrentcreateFromDataFile(java.io.File data, java.net.URL announce_url)

		return( createFromDataFile( data, announce_url, false ));
	
public TorrentcreateFromDataFile(java.io.File data, java.net.URL announce_url, boolean include_other_hashes)

		try{
			TOTorrentCreator c = TOTorrentFactory.createFromFileOrDirWithComputedPieceLength( data, announce_url, include_other_hashes);

			c.addListener( this );

			return( new TorrentImpl(plugin_interface,c.create()));

		}catch( TOTorrentException e ){

			throw( new TorrentException( "TorrentManager::createFromDataFile Fails", e ));
		}
	
public TorrentCreatorcreateFromDataFileEx(java.io.File data, java.net.URL announce_url, boolean include_other_hashes)

		try{
			final TOTorrentCreator c = TOTorrentFactory.createFromFileOrDirWithComputedPieceLength( data, announce_url, include_other_hashes);

			return(
				new TorrentCreator()
				{
					private CopyOnWriteList	listeners = new CopyOnWriteList();

					public void
					start()
					{
						c.addListener(
							new TOTorrentProgressListener()
							{
								public void
								reportProgress(
									int		percent_complete )
								{
									for (Iterator it=listeners.iterator();it.hasNext();){

										((TorrentCreatorListener)it.next()).reportPercentageDone( percent_complete );
									}
								}

								public void
								reportCurrentTask(
									String	task_description )
								{
									for (Iterator it=listeners.iterator();it.hasNext();){

										((TorrentCreatorListener)it.next()).reportActivity( task_description );
									}
								}
							});

						new AEThread( "TorrentManager::create", true )
						{
							public void
							runSupport()
							{
								try{
									TOTorrent	t = c.create();

									Torrent	torrent = new TorrentImpl( plugin_interface, t );

									for (Iterator it=listeners.iterator();it.hasNext();){

										((TorrentCreatorListener)it.next()).complete( torrent );
									}

								}catch( TOTorrentException e ){

									for (Iterator it=listeners.iterator();it.hasNext();){

										((TorrentCreatorListener)it.next()).failed( new TorrentException( e ));
									}

								}
							}
						}.start();
					}

					public void
					cancel()
					{
						c.cancel();
					}

					public void
					addListener(
						TorrentCreatorListener listener )
					{
						listeners.add( listener );
					}

					public void
					removeListener(
						TorrentCreatorListener listener )
					{
						listeners.remove( listener );
					}
				});

		}catch( TOTorrentException e ){

			throw( new TorrentException( "TorrentManager::createFromDataFile Fails", e ));
		}
	
public TorrentAttributegetAttribute(java.lang.String name)

		try{
			class_mon.enter();

			TorrentAttribute	res = (TorrentAttribute)attribute_map.get(name);

			if ( res == null && name.startsWith( "Plugin." )){

				res = new TorrentAttributePluginImpl( name );

				attribute_map.put( name, res );
			}

			return( res );

		}finally{

			class_mon.exit();
		}
	
public TorrentAttribute[]getDefinedAttributes()

		try{
			class_mon.enter();

			Collection	entries = attribute_map.values();

			TorrentAttribute[]	res = new TorrentAttribute[entries.size()];

			entries.toArray( res );

			return( res );

		}finally{

			class_mon.exit();
		}
	
public TorrentAttributegetPluginAttribute(java.lang.String name)

			// this prefix is RELIED ON ELSEWHERE!!!!

		name	= "Plugin." + plugin_interface.getPluginID() + "." + name;

		try{
			class_mon.enter();

			TorrentAttribute	res = (TorrentAttribute)attribute_map.get(name);

			if ( res != null ){

				return( res );
			}

			res = new TorrentAttributePluginImpl( name );

			attribute_map.put( name, res );

			return( res );

		}finally{

			class_mon.exit();
		}
	
public static org.gudy.azureus2.pluginsimpl.local.torrent.TorrentManagerImplgetSingleton()


	
		attribute_map.put( TorrentAttribute.TA_CATEGORY, 				category_attribute );
		attribute_map.put( TorrentAttribute.TA_SHARE_PROPERTIES, 		share_properties_attribute );
		attribute_map.put( TorrentAttribute.TA_NETWORKS, 				networks_attribute );
		attribute_map.put( TorrentAttribute.TA_PEER_SOURCES, 			peer_sources_attribute );
		attribute_map.put( TorrentAttribute.TA_TRACKER_CLIENT_EXTENSIONS, tr_ext_attribute );
		attribute_map.put( TorrentAttribute.TA_DISPLAY_NAME,            disp_name_attribute );
		attribute_map.put( TorrentAttribute.TA_USER_COMMENT,            comment_attribute);
		attribute_map.put( TorrentAttribute.TA_RELATIVE_SAVE_PATH,      relative_save_path_attribute);
	
		try{
			class_mon.enter();

			if ( singleton == null ){

					// default singleton not attached to a plugin

				singleton = new TorrentManagerImpl(null);
			}

			return( singleton );

		}finally{

			class_mon.exit();
		}
	
public TorrentDownloadergetURLDownloader(java.net.URL url)

		return( new TorrentDownloaderImpl( this, url ));
	
public TorrentDownloadergetURLDownloader(java.net.URL url, java.lang.String user_name, java.lang.String password)

		return( new TorrentDownloaderImpl( this, url, user_name, password ));
	
private TOTorrentpreserveFields(TOTorrent torrent, int preserve)

		if (preserve == TorrentManager.PRESERVE_ALL) {
			return torrent;
		} else if ((preserve & TorrentManager.PRESERVE_ENCODING) > 0) {
			String encoding = torrent.getAdditionalStringProperty("encoding");
			torrent.removeAdditionalProperties();
			if (encoding != null)
				torrent.setAdditionalStringProperty("encoding", encoding);
		} else if (preserve == TorrentManager.PRESERVE_NONE) {
			torrent.removeAdditionalProperties();
		}
		return torrent;
	
public voidremoveListener(TorrentManagerListener l)

		try{
			class_mon.enter();

			ArrayList	new_listeners = new ArrayList( listeners );

			new_listeners.remove( l );

			listeners	= new_listeners;

		}finally{

			class_mon.exit();
		}
	
public voidreportCurrentTask(java.lang.String task_description)

		for (Iterator it = listeners.iterator();it.hasNext();){

			((TorrentManagerListener)it.next()).event(
					new TorrentManagerEvent()
					{
						public Object
						getData()
						{
							return( task_description );
						}
					});
		}
	
public voidreportProgress(int percent_complete)

	
public TorrentManagerspecialise(org.gudy.azureus2.plugins.PluginInterface _pi)

			// specialised one attached to plugin

		return( new TorrentManagerImpl( _pi ));
	
protected voidtryToSetDefaultTorrentEncoding(TOTorrent torrent)

		try{
			LocaleTorrentUtil.setDefaultTorrentEncoding( torrent );

		}catch( LocaleUtilEncodingException e ){

			String[]	charsets = e.getValidCharsets();

			if ( charsets == null ){

				throw( new TorrentEncodingException("Failed to set default encoding", e));

			}else{

				throw( new TorrentEncodingException(charsets,e.getValidTorrentNames()));
			}
		}
	
protected voidtryToSetTorrentEncoding(TOTorrent torrent, java.lang.String encoding)

		try{
			LocaleTorrentUtil.setTorrentEncoding( torrent, encoding );

		}catch( LocaleUtilEncodingException e ){

			String[]	charsets = e.getValidCharsets();

			if ( charsets == null ){

				throw( new TorrentEncodingException("Failed to set requested encoding", e));

			}else{

				throw( new TorrentEncodingException(charsets,e.getValidTorrentNames()));
			}
		}