KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > usermanager > jsx > JSXUserManager


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.jsx;
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.FileExistsException;
36 import net.sf.drftpd.master.ConnectionManager;
37 import net.sf.drftpd.master.usermanager.NoSuchUserException;
38 import net.sf.drftpd.master.usermanager.User;
39 import net.sf.drftpd.master.usermanager.UserExistsException;
40 import net.sf.drftpd.master.usermanager.UserFileException;
41 import net.sf.drftpd.master.usermanager.UserManager;
42 import JSX.ObjIn;
43
44 /**
45  * @author mog
46  * @version $Id: JSXUserManager.java,v 1.31 2004/06/01 17:16:49 mog Exp $
47  */

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