1 18 package net.sf.drftpd.master.usermanager.jsx; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.io.FileReader ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Hashtable ; 27 import java.util.Iterator ; 28 import java.util.List ; 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 48 public class JSXUserManager implements UserManager { 49 private ConnectionManager _connManager; 50 private static final Logger logger = 51 Logger.getLogger(JSXUserManager.class.getName()); 52 String userpath = "users/jsx/"; 53 File userpathFile = new File (userpath); 54 55 Hashtable users = new Hashtable (); 56 57 public JSXUserManager() throws UserFileException { 58 59 if (!userpathFile.exists() && !userpathFile.mkdirs()) { 60 throw new UserFileException( 61 new IOException ("Error creating folders: " + userpathFile)); 62 } 63 64 String userfilenames[] = userpathFile.list(); 65 int numUsers = 0; 66 for (int i = 0; i < userfilenames.length; i++) { 67 String 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 username) throws UserFileException { 91 try { 92 getUserByName(username); 93 throw new FileExistsException("User already exists"); 95 } catch (IOException e) { 96 throw new UserFileException(e); 98 } catch (NoSuchUserException e) { 99 } 101 JSXUser user = new JSXUser(this, username); 102 users.put(user.getUsername(), user); 103 return user; 104 } 105 106 public boolean exists(String username) { 107 return getUserFile(username).exists(); 108 } 109 110 public Collection getAllGroups() throws UserFileException { 111 Collection users = this.getAllUsers(); 112 ArrayList ret = new ArrayList (); 113 114 for (Iterator iter = users.iterator(); iter.hasNext();) { 115 User myUser = (User) iter.next(); 116 Collection myGroups = myUser.getGroups(); 117 for (Iterator iterator = myGroups.iterator(); 118 iterator.hasNext(); 119 ) { 120 String myGroup = (String ) iterator.next(); 121 if (!ret.contains(myGroup)) 122 ret.add(myGroup); 123 } 124 } 125 126 return ret; 127 } 128 129 public List getAllUsers() throws UserFileException { 130 ArrayList users = new ArrayList (); 131 132 String userpaths[] = userpathFile.list(); 133 for (int i = 0; i < userpaths.length; i++) { 134 String userpath = userpaths[i]; 135 if (!userpath.endsWith(".xml")) 136 continue; 137 String username = 138 userpath.substring(0, userpath.length() - ".xml".length()); 139 try { 140 users.add((JSXUser) getUserByNameUnchecked(username)); 141 } catch (NoSuchUserException e) { 143 } } 145 return users; 146 } 147 148 public Collection getAllUsersByGroup(String group) 149 throws UserFileException { 150 Collection users = getAllUsers(); 151 for (Iterator 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 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 (getUserFile(username))); 170 } catch (FileNotFoundException ex) { 171 throw new NoSuchUserException("No such user"); 172 } 173 try { 174 user = (JSXUser) in.readObject(); 175 user.usermanager = this; 177 users.put(user.getUsername(), user); 178 user.reset(_connManager); 179 return user; 180 } catch (ClassNotFoundException e) { 181 throw new FatalException(e); 182 } 183 } catch (Throwable 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 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 getUserFile(String username) { 200 return new File (userpath + username + ".xml"); 201 } 202 203 void remove(JSXUser user) { 204 this.users.remove(user.getUsername()); 205 } 206 207 void rename(JSXUser oldUser, String 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 iter = users.values().iterator(); iter.hasNext();) { 218 Object obj = iter.next(); 219 if(!(obj instanceof JSXUser)) throw new ClassCastException ("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 |