FileDocCategorySizeDatePackage
CategoryManagerImpl.javaAPI DocAzureus 3.0.3.47650Tue Feb 06 21:43:42 GMT 2007org.gudy.azureus2.core3.category.impl

CategoryManagerImpl

public class CategoryManagerImpl extends Object

Fields Summary
private static CategoryManagerImpl
catMan
private static Category
catAll
private static Category
catUncategorized
private static boolean
doneLoading
private static AEMonitor
class_mon
private Map
categories
private AEMonitor
categories_mon
private static final int
LDT_CATEGORY_ADDED
private static final int
LDT_CATEGORY_REMOVED
private ListenerManager
category_listeners
Constructors Summary
protected CategoryManagerImpl()



  
  
  
  	loadCategories();
  
Methods Summary
public voidaddCategoryManagerListener(CategoryManagerListener l)

    category_listeners.addListener( l );
  
public CategorycreateCategory(java.lang.String name)

    makeSpecialCategories();
    Category newCategory = getCategory(name);
    if (newCategory == null) {
      newCategory = new CategoryImpl(name, 0, 0);
      categories.put(name, newCategory);
      saveCategories();

      category_listeners.dispatch( LDT_CATEGORY_ADDED, newCategory );
      return (Category)categories.get(name);
    }
    return newCategory;
  
public Category[]getCategories()

    if (categories.size() > 0)
      return (Category[])categories.values().toArray(new Category[categories.size()]);
    return (new Category[0]);
  
public CategorygetCategory(java.lang.String name)

    return (Category)categories.get(name);
  
public CategorygetCategory(int type)

    if (type == Category.TYPE_ALL)
      return catAll;
    if (type == Category.TYPE_UNCATEGORIZED)
      return catUncategorized;
    return null;
  
public static org.gudy.azureus2.core3.category.impl.CategoryManagerImplgetInstance()

  	try{
  		class_mon.enter();
	    if (catMan == null)
	      catMan = new CategoryManagerImpl();
	    return catMan;
  	}finally{
  		
  		class_mon.exit();
  	}
  
protected voidloadCategories()

    if (doneLoading)
      return;
    doneLoading = true;

    FileInputStream fin = null;
    BufferedInputStream bin = null;
 
    makeSpecialCategories();

   
    try {
      //open the file
      File configFile = FileUtil.getUserFile("categories.config");
      fin = new FileInputStream(configFile);
      bin = new BufferedInputStream(fin, 8192);
     
      Map map = BDecoder.decode(bin);

      List catList = (List) map.get("categories");
      for (int i = 0; i < catList.size(); i++) {
        Map mCategory = (Map) catList.get(i);
        try {
          String catName = new String((byte[]) mCategory.get("name"), Constants.DEFAULT_ENCODING);
          
          Long l_maxup 		= (Long)mCategory.get( "maxup" );
          Long l_maxdown 	= (Long)mCategory.get( "maxdown" );
          
          categories.put( 
        	catName,
        	  new CategoryImpl( 
        		  catName, 
        		  l_maxup==null?0:l_maxup.intValue(),
        		  l_maxdown==null?0:l_maxdown.intValue()));
        }
        catch (UnsupportedEncodingException e1) {
          //Do nothing and process next.
        }
      }
    }
    catch (FileNotFoundException e) {
      //Do nothing
    }
    catch (Exception e) {
    	Debug.printStackTrace( e );
    }
    finally {
      try {
        if (bin != null)
          bin.close();
      }
      catch (Exception e) {}
      try {
        if (fin != null)
          fin.close();
      }
      catch (Exception e) {}
    }
  
private voidmakeSpecialCategories()

    if (catAll == null) {
      catAll = new CategoryImpl("Categories.all", Category.TYPE_ALL);
      categories.put("Categories.all", catAll);
    }
    
    if (catUncategorized == null) {
      catUncategorized = new CategoryImpl("Categories.uncategorized", Category.TYPE_UNCATEGORIZED);
      categories.put("Categories.uncategorized", catUncategorized);
    }
  
public voidremoveCategory(Category category)

    if (categories.containsKey(category.getName())) {
      categories.remove(category.getName());
      saveCategories();
      category_listeners.dispatch( LDT_CATEGORY_REMOVED, category );
    }
  
public voidremoveCategoryManagerListener(CategoryManagerListener l)

    category_listeners.removeListener( l );
  
public voidsaveCategories()

    try{
    	categories_mon.enter();
    
      Map map = new HashMap();
      List list = new ArrayList(categories.size());

      Iterator iter = categories.values().iterator();
      while (iter.hasNext()) {
        Category cat = (Category) iter.next();

        // For now we are only putting in 1 thing.  Maybe more later, so we use a map
        if (cat.getType() == Category.TYPE_USER) {
          Map catMap = new HashMap();
          catMap.put( "name", cat.getName());
          catMap.put( "maxup", new Long(cat.getUploadSpeed()));
          catMap.put( "maxdown", new Long(cat.getUploadSpeed()));
          list.add(catMap);
        }
      }
      map.put("categories", list);


      FileOutputStream fos = null;

      try {
        //encode the data
        byte[] torrentData = BEncoder.encode(map);

         File oldFile = FileUtil.getUserFile("categories.config");
         File newFile = FileUtil.getUserFile("categories.config.new");

         //write the data out
        fos = new FileOutputStream(newFile);
        fos.write(torrentData);
         fos.flush();
         fos.getFD().sync();

          //close the output stream
         fos.close();
         fos = null;

         //delete the old file
         if ( !oldFile.exists() || oldFile.delete() ) {
            //rename the new one
            newFile.renameTo(oldFile);
         }

      }
      catch (Exception e) {
      	Debug.printStackTrace( e );
      }
      finally {
        try {
          if (fos != null)
            fos.close();
        }
        catch (Exception e) {}
      }
    }finally{
    	categories_mon.exit();
    }