FileDocCategorySizeDatePackage
TrackersUtil.javaAPI DocAzureus 3.0.3.45503Fri Sep 14 11:21:30 BST 2007org.gudy.azureus2.core3.util

TrackersUtil

public class TrackersUtil extends Object
author
Olivier

Fields Summary
private List
trackers
private Map
multiTrackers
private Map
webseeds
private static TrackersUtil
instance
private static AEMonitor
class_mon
Constructors Summary
private TrackersUtil()


  
  
    
    trackers = new ArrayList();
    multiTrackers = new HashMap();
    webseeds = new HashMap();
    loadList();
  
Methods Summary
public voidaddMultiTracker(java.lang.String configName, java.util.List groups)

    multiTrackers.put(configName,groups);
    saveList();
  
public voidaddTracker(java.lang.String trackerAnnounceUrl)

    if(trackers.contains(trackerAnnounceUrl))
      return;
    trackers.add(0,trackerAnnounceUrl);
    saveList();
  
public voidaddWebSeed(java.lang.String configName, java.util.Map ws)

	  webseeds.put(configName,ws);
	  saveList();
  
public voidclearAllTrackers(boolean save)

	  trackers = new ArrayList();
	  multiTrackers = new HashMap();
	  webseeds = new HashMap();
	  if (save) {saveList();}
  
public static org.gudy.azureus2.core3.util.TrackersUtilgetInstance()

  	try{
  		class_mon.enter();
  	
  		if(instance == null)
  			instance = new TrackersUtil();
  		return instance;
  		
  	}finally{
  		
  		class_mon.exit();
  	}
  
public java.util.MapgetMultiTrackers()

    return new HashMap(multiTrackers);
  
public java.util.ListgetTrackersList()

    if(trackers != null)
      return new ArrayList(trackers);
    else
      return null;
  
public java.util.MapgetWebSeeds()

	  return new HashMap(webseeds);
  
private voidloadList()

    
    File fTrackers = FileUtil.getUserFile("trackers.config");
    if(fTrackers.exists() && fTrackers.isFile()) {
      FileInputStream fin = null;
      BufferedInputStream bin = null;
      try {
        fin = new FileInputStream(fTrackers);
        bin = new BufferedInputStream(fin, 8192);
        Map map = BDecoder.decode(bin);
        List list = (List) map.get("trackers");
        if(list != null) {
	        Iterator iter = list.iterator();
	        while(iter.hasNext()) {
	          String tracker =  new String((byte[])iter.next());
	          trackers.add(tracker);
	        }
        }
        Map mapMT = (Map) map.get("multi-trackers");
        if(mapMT != null) {
          Iterator iter = mapMT.keySet().iterator();
          while(iter.hasNext()) {
            String configName =  (String) iter.next();            
            List groups = (List) mapMT.get(configName);
            List resGroups = new ArrayList(groups.size());
            Iterator iterGroups = groups.iterator();
            while(iterGroups.hasNext()) {
              List theseTrackers = (List) iterGroups.next();
              List resTrackers = new ArrayList(theseTrackers.size());
              Iterator iterTrackers = theseTrackers.iterator();
              while(iterTrackers.hasNext()) {
                String tracker = new String((byte[]) iterTrackers.next());
                resTrackers.add(tracker);
              }
              resGroups.add(resTrackers);
            }
            this.multiTrackers.put(configName,resGroups);
          }
        }
        webseeds = (Map)map.get( "webseeds" );
        
        if ( webseeds == null ){
        	webseeds = new HashMap();
        }else{
        	BDecoder.decodeStrings( webseeds );
        }
      } catch(Exception e) {
    	  
      	Debug.printStackTrace( e );
      	
	  }finally{
		  
		if ( bin != null ){
			try{
			    bin.close();
			}catch( Throwable e ){
			}
		}
		if ( fin != null ){
			try{
				fin.close();
			}catch( Throwable e ){
			}
		}
	  }
    }
  
public voidremoveMultiTracker(java.lang.String configName)

    multiTrackers.remove(configName);
    saveList();
  
public voidremoveWebSeed(java.lang.String configName)

	  webseeds.remove(configName);
	  saveList();
  
private voidsaveList()

    Map map = new HashMap();
    map.put("trackers",trackers);
    map.put("multi-trackers",multiTrackers);
    map.put("webseeds",webseeds );
    FileOutputStream fos = null;
    try {
      //  Open the file
      File fTrackers = FileUtil.getUserFile("trackers.config");
      fos = new FileOutputStream(fTrackers);
      fos.write(BEncoder.encode(map));
      fos.close();     
    } catch (Exception e) {
    	Debug.printStackTrace( e );
    } finally{
		if ( fos != null ){
			try{
				fos.close();
			}catch( Throwable e ){
			}
		}   	
    }