1 /*
2  * File    : TrackersUtil.java
3  * Created : 7 nov. 2003 12:09:56
4  * By      : Olivier
5  *
6  * Azureus - a Java Bittorrent client
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details ( see the LICENSE file ).
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 package org.gudy.azureus2.core3.util;
24 
25 import java.io.BufferedInputStream;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34 
35 /**
36  * @author Olivier
37  *
38  */
39 public class TrackersUtil {
40 
41   private List<String> trackers;
42   private Map<String,List<List<String>>> multiTrackers;
43   private Map<String,Map> webseeds;
44 
45   private static TrackersUtil 	instance;
46   private static AEMonitor		class_mon 	= new AEMonitor( "TrackersUtil:class" );
47 
48 
49 
TrackersUtil()50   private TrackersUtil() {
51     trackers = new ArrayList<String>();
52     multiTrackers = new HashMap<String,List<List<String>>>();
53     webseeds = new HashMap<String,Map>();
54     loadList();
55   }
56 
57 
getInstance()58   public static TrackersUtil getInstance() {
59   	try{
60   		class_mon.enter();
61 
62   		if(instance == null)
63   			instance = new TrackersUtil();
64   		return instance;
65 
66   	}finally{
67 
68   		class_mon.exit();
69   	}
70   }
71 
getTrackersList()72   public List<String> getTrackersList() {
73     if(trackers != null)
74       return new ArrayList<String>(trackers);
75     else
76       return null;
77   }
78 
addTracker(String trackerAnnounceUrl)79   public void addTracker(String trackerAnnounceUrl) {
80     if(trackers.contains(trackerAnnounceUrl))
81       return;
82     trackers.add(0,trackerAnnounceUrl);
83     saveList();
84   }
85 
addMultiTracker(String configName, List<List<String>> groups)86   public void addMultiTracker(String configName, List<List<String>> groups) {
87     multiTrackers.put(configName,groups);
88     saveList();
89   }
90 
removeMultiTracker(String configName)91   public void removeMultiTracker(String configName) {
92     multiTrackers.remove(configName);
93     saveList();
94   }
95 
getMultiTrackers()96   public Map<String,List<List<String>>> getMultiTrackers() {
97     return new HashMap<String,List<List<String>>>(multiTrackers);
98   }
addWebSeed(String configName, Map ws)99   public void addWebSeed(String configName, Map ws) {
100 	  webseeds.put(configName,ws);
101 	  saveList();
102   }
103 
removeWebSeed(String configName)104   public void removeWebSeed(String configName) {
105 	  webseeds.remove(configName);
106 	  saveList();
107   }
108 
getWebSeeds()109   public Map<String,Map> getWebSeeds() {
110 	  return new HashMap<String,Map>(webseeds);
111   }
112 
clearAllTrackers(boolean save)113   public void clearAllTrackers(boolean save) {
114 	  trackers = new ArrayList<String>();
115 	  multiTrackers = new HashMap<String,List<List<String>>>();
116 	  webseeds = new HashMap<String,Map>();
117 	  if (save) {saveList();}
118   }
119 
loadList()120   private void loadList() {
121     File fTrackers = FileUtil.getUserFile("trackers.config");
122     if(fTrackers.exists() && fTrackers.isFile()) {
123       FileInputStream fin = null;
124       BufferedInputStream bin = null;
125       try {
126         fin = new FileInputStream(fTrackers);
127         bin = new BufferedInputStream(fin, 8192);
128         Map map = BDecoder.decode(bin);
129         List list = (List) map.get("trackers");
130         if(list != null) {
131 	        Iterator iter = list.iterator();
132 	        while(iter.hasNext()) {
133 	          String tracker =  new String((byte[])iter.next());
134 	          trackers.add(tracker);
135 	        }
136         }
137         Map mapMT = (Map) map.get("multi-trackers");
138         if(mapMT != null) {
139           Iterator iter = mapMT.keySet().iterator();
140           while(iter.hasNext()) {
141             String configName =  (String) iter.next();
142             List groups = (List) mapMT.get(configName);
143             List resGroups = new ArrayList(groups.size());
144             Iterator iterGroups = groups.iterator();
145             while(iterGroups.hasNext()) {
146               List theseTrackers = (List) iterGroups.next();
147               List resTrackers = new ArrayList(theseTrackers.size());
148               Iterator iterTrackers = theseTrackers.iterator();
149               while(iterTrackers.hasNext()) {
150                 String tracker = new String((byte[]) iterTrackers.next());
151                 resTrackers.add(tracker);
152               }
153               resGroups.add(resTrackers);
154             }
155             this.multiTrackers.put(configName,resGroups);
156           }
157         }
158         webseeds = (Map)map.get( "webseeds" );
159 
160         if ( webseeds == null ){
161         	webseeds = new HashMap();
162         }else{
163         	BDecoder.decodeStrings( webseeds );
164         }
165       } catch(Exception e) {
166 
167       	Debug.printStackTrace( e );
168 
169 	  }finally{
170 
171 		if ( bin != null ){
172 			try{
173 			    bin.close();
174 			}catch( Throwable e ){
175 			}
176 		}
177 		if ( fin != null ){
178 			try{
179 				fin.close();
180 			}catch( Throwable e ){
181 			}
182 		}
183 	  }
184     }
185   }
186 
saveList()187   private void saveList() {
188     Map map = new HashMap();
189     map.put("trackers",trackers);
190     map.put("multi-trackers",multiTrackers);
191     map.put("webseeds",webseeds );
192     FileOutputStream fos = null;
193     try {
194       //  Open the file
195       File fTrackers = FileUtil.getUserFile("trackers.config");
196       fos = new FileOutputStream(fTrackers);
197       fos.write(BEncoder.encode(map));
198       fos.close();
199     } catch (Exception e) {
200     	Debug.printStackTrace( e );
201     } finally{
202 		if ( fos != null ){
203 			try{
204 				fos.close();
205 			}catch( Throwable e ){
206 			}
207 		}
208     }
209   }
210 
211 
212 
213 
214 }
215