FileDocCategorySizeDatePackage
ShareResourceImpl.javaAPI DocAzureus 3.0.3.48806Sun Feb 12 03:05:58 GMT 2006org.gudy.azureus2.pluginsimpl.local.sharing

ShareResourceImpl

public abstract class ShareResourceImpl extends Object implements ShareResource
author
parg

Fields Summary
protected static BrokenMd5Hasher
hasher
protected ShareManagerImpl
manager
protected int
type
protected ShareResourceDirContents
parent
protected Map
attributes
protected List
change_listeners
protected List
deletion_listeners
Constructors Summary
protected ShareResourceImpl(ShareManagerImpl _manager, int _type)

	
		// new constructor
	
	
	
			
							 
	
		manager	= _manager;
		type 	= _type;
	
protected ShareResourceImpl(ShareManagerImpl _manager, int _type, Map _map)

		manager	= _manager;
		type 	= _type;
		
		Map	attrs = (Map)_map.get( "attributes" );
		
		if ( attrs != null ){
			
			Iterator	keys = attrs.keySet().iterator();
			
			while( keys.hasNext()){
				
				String	key = (String)keys.next();
				
				try{
					String	value = new String((byte[])attrs.get(key), Constants.DEFAULT_ENCODING );
				
					TorrentAttribute ta = TorrentManagerImpl.getSingleton().getAttribute( key );
					
					if ( ta == null ){
						
						Debug.out( "Invalid attribute '" + key );
					}else{
						
						attributes.put( ta, value );
					}
				}catch( Throwable e ){
					
					Debug.printStackTrace(e);
				}
			}
		}
	
Methods Summary
public voidaddChangeListener(ShareResourceListener l)

		change_listeners.add( l );
	
public voidaddDeletionListener(ShareResourceWillBeDeletedListener l)

		deletion_listeners.add( l );
	
public abstract booleancanBeDeleted()

protected abstract voidcheckConsistency()

public voiddelete()

		if ( getParent() != null ){
			
		
			throw( new ShareResourceDeletionVetoException( MessageText.getString("plugin.sharing.remove.veto")));
		}
		
		delete( false );
	
protected voiddelete(boolean force)

		if ( !force ){
	
			canBeDeleted();
		}
		
		manager.delete(this);
	
protected abstract voiddeleteInternal()

protected voiddeleteTorrent(ShareItemImpl item)

		manager.deleteTorrent( item );
	
public java.lang.StringgetAttribute(org.gudy.azureus2.plugins.torrent.TorrentAttribute attribute)

		return((String)attributes.get( attribute ));
	
public org.gudy.azureus2.plugins.torrent.TorrentAttribute[]getAttributes()

		TorrentAttribute[]	res = new TorrentAttribute[attributes.size()];
		
		attributes.keySet().toArray( res );
		
		return( res );
	
public ShareResource[]getChildren()

		return( new ShareResource[0] );
	
protected byte[]getFingerPrint(java.io.File file)

		try{
			StringBuffer	buffer = new StringBuffer();
			
			getFingerPrintSupport( buffer, file, TorrentUtils.getIgnoreSet());
							
			return( hasher.calculateHash(buffer.toString().getBytes()));
			
		}catch( ShareException e ){
			
			throw( e );
			
		}catch( Throwable e ){
			
			throw( new ShareException( "ShareResource::getFingerPrint: fails", e ));
		}
	
protected voidgetFingerPrintSupport(java.lang.StringBuffer buffer, java.io.File file, java.util.Set ignore_set)

		try{
			if ( file.isFile()){
				
				long	mod 	= file.lastModified();
				long	size	= file.length();
			
				String	file_name = file.getName();
				
				if  ( ignore_set.contains( file_name.toLowerCase())){
										
				}else{
					
					buffer.append( file_name ).append( ":" ).append( mod ).append( ":" ).append( size );
				}	
			}else if ( file.isDirectory()){
								
				File[]	dir_file_list = file.listFiles();
										
				List file_list = new ArrayList(Arrays.asList(dir_file_list));
				
				Collections.sort(file_list);
				
				for (int i=0;i<file_list.size();i++){
					
					File	f = (File)file_list.get(i);
					
					String	file_name = f.getName();
					
					if ( !(file_name.equals( "." ) || file_name.equals( ".." ))){
						
						StringBuffer	sub_print	= new StringBuffer();
						
						getFingerPrintSupport( sub_print, f, ignore_set );
						
						if  ( sub_print.length() > 0 ){
							
							buffer.append( ":" ).append( sub_print );
						}
					}
				}
			}else{
				
				throw( new ShareException( "ShareResource::getFingetPrint: '" + file.toString() + "' doesn't exist" ));
			}
			
		}catch( Throwable e ){
			
			if ( e instanceof ShareException ){
				
				throw((ShareException)e);
			}
			
			Debug.printStackTrace( e );
			
			throw( new ShareException( "ShareResource::getFingerPrint: fails", e ));
		}
	
protected java.lang.StringgetNewTorrentLocation()

		return( manager.getNewTorrentLocation());
	
public ShareResourceDirContentsgetParent()

		return( parent );
	
public java.io.FilegetTorrentFile(ShareItemImpl item)

		return( manager.getTorrentFile(item));
	
public intgetType()

		return( type );
	
protected voidinheritAttributes(org.gudy.azureus2.pluginsimpl.local.sharing.ShareResourceImpl source)

		TorrentAttribute[]	attrs = source.getAttributes();
		
		for ( int i=0;i<attrs.length;i++ ){
			
			setAttribute( attrs[i], source.getAttribute( attrs[i] ));
		}
	
protected voidreadTorrent(ShareItemImpl item)

		manager.readTorrent( item );
	
public voidremoveChangeListener(ShareResourceListener l)

		change_listeners.remove( l );
	
public voidremoveDeletionListener(ShareResourceWillBeDeletedListener l)

		deletion_listeners.remove( l );
	
protected voidserialiseResource(java.util.Map map)

		Iterator	it = attributes.keySet().iterator();
		
		Map	attrs = new HashMap();
		
		map.put( "attributes", attrs );
		
		while( it.hasNext()){
			
			TorrentAttribute	ta = (TorrentAttribute)it.next();
			
			String	value = (String)attributes.get(ta);
			
			try{
				if ( value != null ){
					
					attrs.put( ta.getName(), value.getBytes( Constants.DEFAULT_ENCODING ));
					
				}
			}catch( Throwable e ){
				
				Debug.printStackTrace(e);
			}
		}
	
public voidsetAttribute(org.gudy.azureus2.plugins.torrent.TorrentAttribute attribute, java.lang.String value)

		ShareConfigImpl	config = manager.getShareConfig();
				
		try{
			config.suspendSaving();
		
			ShareResource[]	kids = getChildren();
			
			for (int i=0;i<kids.length;i++){
				
				kids[i].setAttribute( attribute, value );
			}
			
			String	old_value = (String)attributes.get( attribute );
			
			if( old_value == null && value == null ){
				
				return;
			}
			
			if ( old_value != null && value != null && old_value.equals( value )){
				
				return;
			}
			
			attributes.put( attribute, value );
			
			try{
				config.saveConfig();
				
			}catch( ShareException e ){
				
				Debug.printStackTrace( e );
			}
			
		}finally{
				
			try{
				config.resumeSaving();
					
			}catch( ShareException e ){
					
				Debug.printStackTrace( e );
			}
		}
		
		for (int i=0;i<change_listeners.size();i++){
			
			try{
				((ShareResourceListener)change_listeners.get(i)).shareResourceChanged(
						this,
						new ShareResourceEvent()
						{
							public int
							getType()
							{
								return( ShareResourceEvent.ET_ATTRIBUTE_CHANGED);
							}
							
							public Object
							getData()
							{
								return( attribute );
							}
						});
				
			}catch( Throwable e ){
				
				Debug.printStackTrace(e);
			}
		}
	
protected voidsetParent(ShareResourceDirContents _parent)

		parent	= _parent;
	
protected voidwriteTorrent(ShareItemImpl item)

		manager.writeTorrent( item );