1 18 package net.sf.drftpd.master.usermanager.xstream; 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.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 51 public class XStreamUserManager implements UserManager { 52 private ConnectionManager _connManager; 53 private static final Logger logger = 54 Logger.getLogger(XStreamUserManager.class.getName()); 55 String userpath = "users/xstream/"; 56 File userpathFile = new File (userpath); 57 58 Hashtable users = new Hashtable (); 59 60 public XStreamUserManager() throws UserFileException { 61 62 if (!userpathFile.exists() && !userpathFile.mkdirs()) { 63 throw new UserFileException( 64 new IOException ("Error creating folders: " + userpathFile)); 65 } 66 67 String userfilenames[] = userpathFile.list(); 68 int numUsers = 0; 69 for (int i = 0; i < userfilenames.length; i++) { 70 String 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 username) throws UserFileException { 92 try { 93 getUserByName(username); 94 throw new ObjectExistsException("User already exists"); 96 } catch (IOException e) { 97 throw new UserFileException(e); 99 } catch (NoSuchUserException e) { 100 } 102 XStreamUser user = new XStreamUser(this, username); 103 users.put(user.getUsername(), user); 104 return user; 105 } 106 107 public boolean exists(String username) { 108 return getUserFile(username).exists(); 109 } 110 111 public Collection getAllGroups() throws UserFileException { 112 Collection users = this.getAllUsers(); 113 ArrayList ret = new ArrayList (); 114 115 for (Iterator iter = users.iterator(); iter.hasNext();) { 116 User myUser = (User) iter.next(); 117 Collection myGroups = myUser.getGroups(); 118 for (Iterator iterator = myGroups.iterator(); 119 iterator.hasNext(); 120 ) { 121 String myGroup = (String ) iterator.next(); 122 if (!ret.contains(myGroup)) 123 ret.add(myGroup); 124 } 125 } 126 127 return ret; 128 } 129 130 public List getAllUsers() throws UserFileException { 131 ArrayList users = new ArrayList (); 132 133 String userpaths[] = userpathFile.list(); 134 for (int i = 0; i < userpaths.length; i++) { 135 String userpath = userpaths[i]; 136 if (!userpath.endsWith(".xml")) 137 continue; 138 String username = 139 userpath.substring(0, userpath.length() - ".xml".length()); 140 try { 141 users.add((XStreamUser) getUserByNameUnchecked(username)); 142 } catch (NoSuchUserException e) { 144 } } 146 return users; 147 } 148 149 public Collection getAllUsersByGroup(String group) 150 throws UserFileException { 151 Collection users = getAllUsers(); 152 for (Iterator 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 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 in; 170 try { 171 in = new FileReader (getUserFile(username)); 172 } catch (FileNotFoundException ex) { 173 throw new NoSuchUserException("No such user"); 174 } 175 try { 176 user = (XStreamUser) inp.fromXML(in); 177 user.usermanager = this; 179 users.put(user.getUsername(), user); 180 user.reset(_connManager); 181 return user; 182 } catch (Exception e) { 183 throw new FatalException(e); 184 } 185 } catch (Throwable 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 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 getUserFile(String username) { 202 return new File (userpath + username + ".xml"); 203 } 204 205 void remove(XStreamUser user) { 206 this.users.remove(user.getUsername()); 207 } 208 209 void rename(XStreamUser oldUser, String 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 iter = users.values().iterator(); iter.hasNext();) { 220 Object obj = iter.next(); 221 if (!(obj instanceof XStreamUser)) 222 throw new ClassCastException ("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 |