View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.opennap.user;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.Hashtable;
25  import java.util.Iterator;
26  import java.util.LinkedList;
27  
28  import org.apache.log4j.Logger;
29  import org.xnap.util.FileHelper;
30  
31  public class OpenNapUserManager {
32  
33      //--- Constant(s) ---
34  
35      public static String FILENAME 
36  		= FileHelper.getHomeDir() + "openap_hotlist";
37  
38      //--- Data field(s) ---
39  
40      private static Logger logger = Logger.getLogger(OpenNapUserManager.class);
41  
42      /***
43       * A list of {@link GlobalOpenNapUser} objects.
44       */
45      private Hashtable userByName = new Hashtable();
46  
47      //--- Constructor(s) ---
48  
49      public OpenNapUserManager()
50      {
51      }
52  
53      //--- Method(s) ---
54  
55      public void save()
56      {
57  		LinkedList items = new LinkedList();
58  		for (Iterator i = userByName.values().iterator(); i.hasNext();) {
59  			OpenNapGlobalUser u = (OpenNapGlobalUser)i.next();
60  			if (u.isInHotlist()) {
61  				items.add(u.getData());
62  				u.setInHotlist(false);
63  			}
64  		}
65  		try {
66  			FileHelper.writeBinary(new File(FILENAME), items);
67  		}
68  		catch (IOException e) {
69  			logger.debug("could not write " + FILENAME, e);
70  		}
71      }
72  
73      public OpenNapGlobalUser get(String name)
74      {
75  		OpenNapGlobalUser user = (OpenNapGlobalUser)userByName.get(name);
76  		if (user == null) {
77  			user = new OpenNapGlobalUser(name);
78  			userByName.put(name, user);
79  		}
80  		return user;
81      }	
82  
83      public OpenNapGlobalUser[] getHotlistUsers()
84      {
85  		LinkedList items = new LinkedList();
86  		for (Iterator i = userByName.values().iterator(); i.hasNext();) {
87  			OpenNapGlobalUser u = (OpenNapGlobalUser)i.next();
88  			if (u.isInHotlist()) {
89  				items.add(u);
90  			}
91  		}
92  		return (OpenNapGlobalUser[])items.toArray(new OpenNapGlobalUser[0]);
93      }
94  
95      public void restore() 
96      {
97  		LinkedList items = new LinkedList();
98  		try {
99  			FileHelper.readBinary(new File(FILENAME), items);
100 			for (Iterator i = items.iterator(); i.hasNext();) {
101 				Object o = i.next();
102 				if (o instanceof GlobalOpenNapUserData) {
103 					OpenNapGlobalUser user 
104 						= new OpenNapGlobalUser((GlobalOpenNapUserData)o);
105 					userByName.put(user.getName(), user);
106 				}
107 			}
108 		}
109 		catch (IOException e) {
110 			logger.debug("could not read " + FILENAME, e);
111 		}
112     }
113 
114 }