FileDocCategorySizeDatePackage
ShareResourceFileOrDirImpl.javaAPI DocAzureus 3.0.3.48476Thu Jan 11 11:47:38 GMT 2007org.gudy.azureus2.pluginsimpl.local.sharing

ShareResourceFileOrDirImpl

public abstract class ShareResourceFileOrDirImpl extends ShareResourceImpl
author
parg

Fields Summary
protected File
file
protected ShareItemImpl
item
Constructors Summary
protected ShareResourceFileOrDirImpl(ShareManagerImpl _manager, ShareResourceDirContentsImpl _parent, int _type, File _file)

		super( _manager, _type );
		
		file		= _file;
		
		if ( getType() == ST_FILE ){
			
			if ( !file.exists()){
			
				throw( new ShareException( "File '".concat(file.getName()).concat("' not found")));
			}
		
			if ( !file.isFile()){
			
				throw( new ShareException( "Not a file"));
			}
		}else{
			
			if ( !file.exists()){
				
				throw( new ShareException( "Dir '".concat(file.getName()).concat("' not found")));
			}
			
			if ( file.isFile()){
				
				throw( new ShareException( "Not a directory"));
			}		
		}
		
		try{
			file = file.getCanonicalFile();
						
		}catch( IOException e ){
	
			throw( new ShareException("ShareResourceFile: failed to get canonical name", e));
		}
		
		
		if ( _parent != null ){
			
			setParent( _parent );
			
			inheritAttributes( _parent );
		}
		
		createTorrent();
	
protected ShareResourceFileOrDirImpl(ShareManagerImpl _manager, int _type, File _file, Map _map)

		super( _manager, _type, _map );
		
		file		= _file;
		
		item = ShareItemImpl.deserialiseItem( this, _map );
	
Methods Summary
public booleancanBeDeleted()

		
		for (int i=0;i<deletion_listeners.size();i++){
			
			((ShareResourceWillBeDeletedListener)deletion_listeners.get(i)).resourceWillBeDeleted( this );
		}	
		
		return( true );
	
protected voidcheckConsistency()

		try{
			if ( Arrays.equals(getFingerPrint(), item.getFingerPrint())){
				
				// check torrent file still exists
				
				if ( !manager.torrentExists( item )){
					
					createTorrent();
				}
			}else{
				
				manager.addFileOrDir( null, file, getType(), true );
			}
		}catch( Throwable e ){
						
			manager.delete( this );
		}
	
protected voidcreateTorrent()

		try{
			manager.reportCurrentTask( (item==null?"Creating":"Re-creating").concat(" torrent for '").concat(file.toString()).concat("'" ));
			
			URL[]	urls = manager.getAnnounceURLs();
			
			TOTorrentCreator creator = TOTorrentFactory.createFromFileOrDirWithComputedPieceLength( 
										file,
										urls[0],
										manager.getAddHashes());
										
			creator.addListener( manager );
			
			TOTorrent	to_torrent;
			
			try{
				manager.setTorrentCreator( creator );
			
				to_torrent = creator.create();
	
			}finally{
					
				manager.setTorrentCreator( null );
			}
			
			LocaleTorrentUtil.setDefaultTorrentEncoding( to_torrent );
							
			for (int i=1;i<urls.length;i++){
				
				TorrentUtils.announceGroupsInsertLast( to_torrent, new URL[]{ urls[i]});
			}
			
			String	comment = COConfigurationManager.getStringParameter( "Sharing Torrent Comment" ).trim();

			boolean	private_torrent = COConfigurationManager.getBooleanParameter( "Sharing Torrent Private" );
			
			boolean	dht_backup_enabled = COConfigurationManager.getBooleanParameter( "Sharing Permit DHT", true );

			TorrentAttribute ta_props = TorrentManagerImpl.getSingleton().getAttribute( TorrentAttribute.TA_SHARE_PROPERTIES );
			
			String	props = getAttribute( ta_props );
			
			if ( props != null ){
			
				StringTokenizer	tok = new StringTokenizer( props, ";" );
				
				while( tok.hasMoreTokens()){
					
					String	token = tok.nextToken();
					
					int	pos = token.indexOf('=");
					
					if ( pos == -1 ){
						
						Debug.out( "ShareProperty invalid: " + props );
						
					}else{
						
						String	lhs = token.substring(0,pos).trim().toLowerCase();
						String	rhs = token.substring(pos+1).trim().toLowerCase();
						
						boolean	set = rhs.equals("true");
						
						if ( lhs.equals("private")){
							
							private_torrent	= set;
							
						}else if ( lhs.equals("dht_backup")){
							
							dht_backup_enabled	= set;
							
						}else if ( lhs.equals("comment")){
							
							comment = rhs;
							
						}else{
							
							Debug.out( "ShareProperty invalid: " + props );
							
							break;
						}
					}
				}
			}
			
			if ( comment.length() > 0 ){
				
				to_torrent.setComment( comment );
			}
			
			TorrentUtils.setDHTBackupEnabled( to_torrent, dht_backup_enabled );
		
			TorrentUtils.setPrivate( to_torrent, private_torrent );
			
			if ( TorrentUtils.isDecentralised(to_torrent)){
				
				TorrentUtils.setDecentralised( to_torrent );
			}
						
			DownloadManagerState	download_manager_state = 
				DownloadManagerStateFactory.getDownloadState( to_torrent ); 

			TorrentUtils.setResumeDataCompletelyValid( download_manager_state );

			download_manager_state.save();
			
			if ( item == null ){
				
				byte[] fingerprint = getFingerPrint();
			
				item = new ShareItemImpl(this, fingerprint, new TorrentImpl(to_torrent));
				
			}else{
				
				item.setTorrent( new TorrentImpl(to_torrent));
				
				item.writeTorrent();
			}
			
		}catch( TOTorrentException e ){
			
			if ( e.getReason() == TOTorrentException.RT_CANCELLED ){
				
				throw( new ShareException("ShareResource: Operation cancelled", e));
				
			}else{
				
				throw( new ShareException("ShareResource: Torrent create failed", e));
			}
		}catch( Throwable e ){
			
			throw( new ShareException("ShareResource: Torrent create failed", e));
		}
	
protected voiddeleteInternal()

		item.delete();
	
protected static ShareResourceImpldeserialiseResource(ShareManagerImpl manager, java.util.Map map, int type)

		try{
			File file = new File(new String((byte[])map.get("file"), Constants.DEFAULT_ENCODING ));
			
			if ( type == ST_FILE ){
				
				return( new ShareResourceFileImpl( manager, file, map ));
				
			}else{
				return( new ShareResourceDirImpl( manager, file, map ));
				
			}
		}catch( UnsupportedEncodingException e ){
			
			throw( new ShareException( "internal error", e ));
		}
	
public java.io.FilegetFile()

		return( file );
	
protected abstract byte[]getFingerPrint()

public ShareItemgetItem()

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

		return( file.toString());
	
protected static ShareResourceImplgetResourceSupport(ShareManagerImpl _manager, java.io.File _file)

		try{
			return( _manager.getResource( _file.getCanonicalFile() ));
			
		}catch( IOException e ){
	
			throw( new ShareException( "getCanonicalFile fails", e ));
		}
	
protected voidserialiseResource(java.util.Map map)

		super.serialiseResource( map );
		
		map.put( "type", new Long(getType()));
		
		try{
			map.put( "file", file.toString().getBytes( Constants.DEFAULT_ENCODING));
			
		}catch( UnsupportedEncodingException e ){
			
			Debug.printStackTrace( e );
		}
		
		item.serialiseItem( map );