1 17 18 package org.apache.james.userrepository; 19 20 import org.apache.avalon.framework.component.Component; 21 import org.apache.avalon.framework.logger.AbstractLogEnabled; 22 import org.apache.james.services.User; 23 import org.apache.james.services.UsersRepository; 24 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 29 35 public abstract class AbstractUsersRepository 36 extends AbstractLogEnabled 37 implements UsersRepository, Component { 38 39 43 47 protected abstract Iterator listAllUsers(); 48 49 53 protected abstract void doAddUser(User user); 54 55 59 protected abstract void doRemoveUser(User user); 60 61 64 protected abstract void doUpdateUser(User user); 65 66 76 protected List listUserNames() { 77 Iterator users = listAllUsers(); 78 List userNames = new LinkedList (); 79 while ( users.hasNext() ) { 80 User user = (User)users.next(); 81 userNames.add(user.getUserName()); 82 } 83 84 return userNames; 85 } 86 87 97 protected User getUserByName(String name, boolean ignoreCase) { 98 Iterator users = listAllUsers(); 100 while ( users.hasNext() ) { 101 User user = (User)users.next(); 102 String username = user.getUserName(); 103 if (( !ignoreCase && username.equals(name) ) || 104 ( ignoreCase && username.equalsIgnoreCase(name) )) { 105 return user; 106 } 107 } 108 return null; 110 } 111 112 124 public boolean addUser(User user) { 125 String username = user.getUserName(); 126 127 if ( containsCaseInsensitive(username) ) { 128 return false; 129 } 130 131 doAddUser(user); 132 return true; 133 } 134 135 142 public void addUser(String name, Object attributes) { 143 if (attributes instanceof String ) { 144 User newbie = new DefaultUser(name, "SHA"); 145 newbie.setPassword( (String ) attributes ); 146 addUser(newbie); 147 } else { 148 throw new RuntimeException ("Improper use of deprecated method" 149 + " - use addUser(User user)"); 150 } 151 } 152 153 161 public boolean updateUser(User user) { 162 if ( ! contains(user.getUserName()) ) { 164 return false; 165 } 166 else { 167 doUpdateUser(user); 168 return true; 169 } 170 } 171 172 177 public void removeUser(String name) { 178 User user = getUserByName(name); 179 if ( user != null ) { 180 doRemoveUser(user); 181 } 182 } 183 184 189 public Object getAttributes(String name) { 190 throw new RuntimeException ("Improper use of deprecated method - read javadocs"); 191 } 192 193 203 public User getUserByName(String name) { 204 return getUserByName(name, false); 205 } 206 207 217 public User getUserByNameCaseInsensitive(String name) { 218 return getUserByName(name, true); 219 } 220 221 229 public String getRealName(String name) { 230 User user = getUserByName(name, true); 232 if ( user == null ) { 233 return null; 234 } else { 235 return user.getUserName(); 236 } 237 } 238 239 242 public boolean contains(String name) { 243 User user = getUserByName(name, false); 244 return ( user != null ); 245 } 246 247 251 public boolean containsCaseInsensitive(String name) { 252 User user = getUserByName( name, true ); 253 return ( user != null ); 254 } 255 256 268 public boolean test(String name, Object attributes) { 269 throw new UnsupportedOperationException ("Improper use of deprecated method - read javadocs"); 270 } 271 272 283 public boolean test(String name, String password) { 284 User user = getUserByName(name, false); 285 if ( user == null ) { 286 return false; 287 } else { 288 return user.verifyPassword(password); 289 } 290 } 291 292 297 public int countUsers() { 298 List usernames = listUserNames(); 299 return usernames.size(); 300 } 301 302 307 public Iterator list() { 308 return listUserNames().iterator(); 309 } 310 } 311 | Popular Tags |