1 17 18 package org.apache.james.userrepository; 19 20 import org.apache.avalon.cornerstone.services.store.ObjectRepository; 21 import org.apache.avalon.cornerstone.services.store.Store; 22 import org.apache.avalon.framework.activity.Initializable; 23 import org.apache.avalon.framework.component.Component; 24 import org.apache.avalon.framework.component.ComponentException; 25 import org.apache.avalon.framework.component.ComponentManager; 26 import org.apache.avalon.framework.component.Composable; 27 import org.apache.avalon.framework.configuration.Configurable; 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.configuration.DefaultConfiguration; 31 import org.apache.avalon.framework.logger.AbstractLogEnabled; 32 import org.apache.james.services.User; 33 import org.apache.james.services.UsersRepository; 34 35 import java.io.File ; 36 import java.util.Iterator ; 37 38 51 public class UsersFileRepository 52 extends AbstractLogEnabled 53 implements UsersRepository, Component, Configurable, Composable, Initializable { 54 55 58 protected static boolean DEEP_DEBUG = false; 59 60 61 private static final String TYPE = "USERS"; 62 63 private Store store; 64 private ObjectRepository or; 65 66 69 private String destination; 70 71 74 public void compose( final ComponentManager componentManager ) 75 throws ComponentException { 76 77 try { 78 store = (Store)componentManager. 79 lookup( "org.apache.avalon.cornerstone.services.store.Store" ); 80 } catch (Exception e) { 81 final String message = "Failed to retrieve Store component:" + e.getMessage(); 82 getLogger().error( message, e ); 83 throw new ComponentException( message, e ); 84 } 85 } 86 87 90 public void configure( final Configuration configuration ) 91 throws ConfigurationException { 92 93 destination = configuration.getChild( "destination" ).getAttribute( "URL" ); 94 95 if (!destination.endsWith(File.separator)) { 96 destination += File.separator; 97 } 98 } 99 100 103 public void initialize() 104 throws Exception { 105 106 try { 107 final DefaultConfiguration objectConfiguration 109 = new DefaultConfiguration( "repository", 110 "generated:UsersFileRepository.compose()" ); 111 112 objectConfiguration.setAttribute( "destinationURL", destination ); 113 objectConfiguration.setAttribute( "type", "OBJECT" ); 114 objectConfiguration.setAttribute( "model", "SYNCHRONOUS" ); 115 116 or = (ObjectRepository)store.select( objectConfiguration ); 117 if (getLogger().isDebugEnabled()) { 118 StringBuffer logBuffer = 119 new StringBuffer (192) 120 .append(this.getClass().getName()) 121 .append(" created in ") 122 .append(destination); 123 getLogger().debug(logBuffer.toString()); 124 } 125 } catch (Exception e) { 126 if (getLogger().isErrorEnabled()) { 127 getLogger().error("Failed to initialize repository:" + e.getMessage(), e ); 128 } 129 throw e; 130 } 131 } 132 133 138 public Iterator list() { 139 return or.list(); 140 } 141 142 150 public synchronized boolean addUser(User user) { 151 String username = user.getUserName(); 152 if (contains(username)) { 153 return false; 154 } 155 try { 156 or.put(username, user); 157 } catch (Exception e) { 158 throw new RuntimeException ("Exception caught while storing user: " + e ); 159 } 160 return true; 161 } 162 163 public void addUser(String name, Object attributes) { 164 if (attributes instanceof String ) { 165 User newbie = new DefaultUser(name, "SHA"); 166 newbie.setPassword( (String ) attributes); 167 addUser(newbie); 168 } 169 else { 170 throw new RuntimeException ("Improper use of deprecated method" 171 + " - use addUser(User user)"); 172 } 173 } 174 175 public synchronized User getUserByName(String name) { 176 if (contains(name)) { 177 try { 178 return (User)or.get(name); 179 } catch (Exception e) { 180 throw new RuntimeException ("Exception while retrieving user: " 181 + e.getMessage()); 182 } 183 } else { 184 return null; 185 } 186 } 187 188 public User getUserByNameCaseInsensitive(String name) { 189 String realName = getRealName(name); 190 if (realName == null ) { 191 return null; 192 } 193 return getUserByName(realName); 194 } 195 196 public String getRealName(String name) { 197 Iterator it = list(); 198 while (it.hasNext()) { 199 String temp = (String ) it.next(); 200 if (name.equalsIgnoreCase(temp)) { 201 return temp; 202 } 203 } 204 return null; 205 } 206 207 public Object getAttributes(String name) { 208 throw new UnsupportedOperationException ("Improper use of deprecated method - read javadocs"); 209 } 210 211 public boolean updateUser(User user) { 212 String username = user.getUserName(); 213 if (!contains(username)) { 214 return false; 215 } 216 try { 217 or.put(username, user); 218 } catch (Exception e) { 219 throw new RuntimeException ("Exception caught while storing user: " + e ); 220 } 221 return true; 222 } 223 224 public synchronized void removeUser(String name) { 225 or.remove(name); 226 } 227 228 public boolean contains(String name) { 229 return or.containsKey(name); 230 } 231 232 public boolean containsCaseInsensitive(String name) { 233 Iterator it = list(); 234 while (it.hasNext()) { 235 if (name.equalsIgnoreCase((String )it.next())) { 236 return true; 237 } 238 } 239 return false; 240 } 241 242 public boolean test(String name, Object attributes) { 243 try { 244 return attributes.equals(or.get(name)); 245 } catch (Exception e) { 246 return false; 247 } 248 } 249 250 public boolean test(String name, String password) { 251 User user; 252 try { 253 if (contains(name)) { 254 user = (User) or.get(name); 255 } else { 256 return false; 257 } 258 } catch (Exception e) { 259 throw new RuntimeException ("Exception retrieving User" + e); 260 } 261 return user.verifyPassword(password); 262 } 263 264 public int countUsers() { 265 int count = 0; 266 for (Iterator it = list(); it.hasNext(); it.next()) { 267 count++; 268 } 269 return count; 270 } 271 272 } 273 | Popular Tags |