KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > usermanager > xstream > XStreamUserManager


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.master.usermanager.xstream;
19
20 import java.io.File JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.log4j.Level;
31 import org.apache.log4j.Logger;
32
33 import net.sf.drftpd.DuplicateElementException;
34 import net.sf.drftpd.FatalException;
35 import net.sf.drftpd.ObjectExistsException;
36 import net.sf.drftpd.master.usermanager.UserExistsException;
37
38 import net.sf.drftpd.master.ConnectionManager;
39 import net.sf.drftpd.master.usermanager.NoSuchUserException;
40 import net.sf.drftpd.master.usermanager.User;
41 import net.sf.drftpd.master.usermanager.UserFileException;
42 import net.sf.drftpd.master.usermanager.UserManager;
43
44 import com.thoughtworks.xstream.XStream;
45 import com.thoughtworks.xstream.io.xml.DomDriver;
46
47 /**
48  * @author mog
49  * @version $Id: XStreamUserManager.java,v 1.4 2004/06/01 17:16:50 mog Exp $
50  */

51 public class XStreamUserManager implements UserManager {
52     private ConnectionManager _connManager;
53     private static final Logger logger =
54         Logger.getLogger(XStreamUserManager.class.getName());
55     String JavaDoc userpath = "users/xstream/";
56     File JavaDoc userpathFile = new File JavaDoc(userpath);
57
58     Hashtable JavaDoc users = new Hashtable JavaDoc();
59
60     public XStreamUserManager() throws UserFileException {
61
62         if (!userpathFile.exists() && !userpathFile.mkdirs()) {
63             throw new UserFileException(
64                 new IOException JavaDoc("Error creating folders: " + userpathFile));
65         }
66
67         String JavaDoc userfilenames[] = userpathFile.list();
68         int numUsers = 0;
69         for (int i = 0; i < userfilenames.length; i++) {
70             String JavaDoc string = userfilenames[i];
71             if (string.endsWith(".xml")) {
72                 numUsers++;
73             }
74         }
75         if (numUsers == 0) {
76             User user = this.create("drftpd");
77             user.setGroup("drftpd");
78             user.setPassword("drftpd");
79             try {
80                 user.addIPMask("*@127.0.0.1");
81             } catch (DuplicateElementException e) {
82             }
83             try {
84                 user.addGroup("siteop");
85             } catch (DuplicateElementException e1) {
86             }
87             user.commit();
88         }
89     }
90
91     public User create(String JavaDoc username) throws UserFileException {
92         try {
93             getUserByName(username);
94             //bad
95
throw new ObjectExistsException("User already exists");
96         } catch (IOException JavaDoc e) {
97             //bad
98
throw new UserFileException(e);
99         } catch (NoSuchUserException e) {
100             //good
101
}
102         XStreamUser user = new XStreamUser(this, username);
103         users.put(user.getUsername(), user);
104         return user;
105     }
106
107     public boolean exists(String JavaDoc username) {
108         return getUserFile(username).exists();
109     }
110
111     public Collection JavaDoc getAllGroups() throws UserFileException {
112         Collection JavaDoc users = this.getAllUsers();
113         ArrayList JavaDoc ret = new ArrayList JavaDoc();
114
115         for (Iterator JavaDoc iter = users.iterator(); iter.hasNext();) {
116             User myUser = (User) iter.next();
117             Collection JavaDoc myGroups = myUser.getGroups();
118             for (Iterator JavaDoc iterator = myGroups.iterator();
119                 iterator.hasNext();
120                 ) {
121                 String JavaDoc myGroup = (String JavaDoc) iterator.next();
122                 if (!ret.contains(myGroup))
123                     ret.add(myGroup);
124             }
125         }
126
127         return ret;
128     }
129
130     public List JavaDoc getAllUsers() throws UserFileException {
131         ArrayList JavaDoc users = new ArrayList JavaDoc();
132
133         String JavaDoc userpaths[] = userpathFile.list();
134         for (int i = 0; i < userpaths.length; i++) {
135             String JavaDoc userpath = userpaths[i];
136             if (!userpath.endsWith(".xml"))
137                 continue;
138             String JavaDoc username =
139                 userpath.substring(0, userpath.length() - ".xml".length());
140             try {
141                 users.add((XStreamUser) getUserByNameUnchecked(username));
142                 // throws IOException
143
} catch (NoSuchUserException e) {
144             } // continue
145
}
146         return users;
147     }
148
149     public Collection JavaDoc getAllUsersByGroup(String JavaDoc group)
150         throws UserFileException {
151         Collection JavaDoc users = getAllUsers();
152         for (Iterator JavaDoc iter = users.iterator(); iter.hasNext();) {
153             XStreamUser user = (XStreamUser) iter.next();
154             if (!user.getGroupName().equals(group))
155                 iter.remove();
156         }
157         return users;
158     }
159
160     public User getUserByNameUnchecked(String JavaDoc username)
161         throws NoSuchUserException, UserFileException {
162         try {
163             XStreamUser user = (XStreamUser) users.get(username);
164             if (user != null) {
165                 return user;
166             }
167
168             XStream inp = new XStream(new DomDriver());
169             FileReader JavaDoc in;
170             try {
171                 in = new FileReader JavaDoc(getUserFile(username));
172             } catch (FileNotFoundException JavaDoc ex) {
173                 throw new NoSuchUserException("No such user");
174             }
175             try {
176                 user = (XStreamUser) inp.fromXML(in);
177                 //throws RuntimeException
178
user.usermanager = this;
179                 users.put(user.getUsername(), user);
180                 user.reset(_connManager);
181                 return user;
182             } catch (Exception JavaDoc e) {
183                 throw new FatalException(e);
184             }
185         } catch (Throwable JavaDoc ex) {
186             if (ex instanceof NoSuchUserException)
187                 throw (NoSuchUserException) ex;
188             throw new UserFileException("Error loading " + username, ex);
189         }
190     }
191
192     public User getUserByName(String JavaDoc username)
193         throws NoSuchUserException, UserFileException {
194         XStreamUser user = (XStreamUser) getUserByNameUnchecked(username);
195         if (user.isDeleted())
196             throw new NoSuchUserException(user.getUsername() + " is deleted");
197         user.reset(_connManager);
198         return user;
199     }
200
201     public File JavaDoc getUserFile(String JavaDoc username) {
202         return new File JavaDoc(userpath + username + ".xml");
203     }
204
205     void remove(XStreamUser user) {
206         this.users.remove(user.getUsername());
207     }
208
209     void rename(XStreamUser oldUser, String JavaDoc newUsername)
210         throws UserExistsException {
211         if (users.contains(newUsername))
212             throw new UserExistsException("user " + newUsername + " exists");
213         users.remove(oldUser.getUsername());
214         users.put(newUsername, oldUser);
215     }
216
217     public void saveAll() throws UserFileException {
218         logger.log(Level.INFO, "Saving userfiles: " + users);
219         for (Iterator JavaDoc iter = users.values().iterator(); iter.hasNext();) {
220             Object JavaDoc obj = iter.next();
221             if (!(obj instanceof XStreamUser))
222                 throw new ClassCastException JavaDoc("not instanceof XStreamUser");
223             XStreamUser user = (XStreamUser) obj;
224             user.commit();
225         }
226     }
227
228     public void init(ConnectionManager mgr) {
229         _connManager = mgr;
230     }
231 }
232
Popular Tags