1 18 19 package org.apache.struts.examples.mailreader.memory; 20 21 import java.io.BufferedInputStream ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.OutputStreamWriter ; 27 import java.io.PrintWriter ; 28 import java.util.HashMap ; 29 import org.apache.commons.digester.Digester; 30 import org.apache.commons.digester.ObjectCreationFactory; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.struts.examples.mailreader.Subscription; 34 import org.apache.struts.examples.mailreader.User; 35 import org.apache.struts.examples.mailreader.UserDatabase; 36 import org.apache.struts.examples.mailreader.ExpiredPasswordException; 37 import org.xml.sax.Attributes ; 38 39 46 47 public class MemoryUserDatabase implements UserDatabase { 48 49 50 52 53 55 56 59 private Log log = LogFactory.getLog(this.getClass()); 60 61 62 65 private HashMap users = new HashMap (); 66 67 68 70 71 75 private String pathname = null; 76 77 private String pathnameOld = null; 78 79 private String pathnameNew = null; 80 81 public String getPathname() { 82 return (this.pathname); 83 } 84 85 public void setPathname(String pathname) { 86 this.pathname = pathname; 87 pathnameOld = pathname + ".old"; 88 pathnameNew = pathname + ".new"; 89 } 90 91 92 94 95 public void close() throws Exception { 97 98 save(); 99 100 } 101 102 103 public User createUser(String username) { 105 106 synchronized (users) { 107 if (users.get(username) != null) { 108 throw new IllegalArgumentException ("Duplicate user '" + 109 username + "'"); 110 } 111 if (log.isTraceEnabled()) { 112 log.trace("Creating user '" + username + "'"); 113 } 114 MemoryUser user = new MemoryUser(this, username); 115 synchronized (users) { 116 users.put(username, user); 117 } 118 return (user); 119 } 120 121 } 122 123 124 public User findUser(String username) throws ExpiredPasswordException { 126 127 synchronized (users) { 128 return ((User) users.get(username)); 129 } 130 131 } 132 133 134 public User[] findUsers() { 136 137 synchronized (users) { 138 User results[] = new User[users.size()]; 139 return ((User[]) users.values().toArray(results)); 140 } 141 142 } 143 144 145 public void open() throws Exception { 147 148 FileInputStream fis = null; 149 BufferedInputStream bis = null; 150 151 try { 152 153 if (log.isDebugEnabled()) { 155 log.debug("Loading database from '" + pathname + "'"); 156 } 157 fis = new FileInputStream (pathname); 158 bis = new BufferedInputStream (fis); 159 160 Digester digester = new Digester(); 162 digester.push(this); 163 digester.setValidating(false); 164 digester.addFactoryCreate 165 ("database/user", 166 new MemoryUserCreationFactory(this)); 167 digester.addFactoryCreate 168 ("database/user/subscription", 169 new MemorySubscriptionCreationFactory()); 170 171 digester.parse(bis); 173 bis.close(); 174 bis = null; 175 fis = null; 176 177 } catch (Exception e) { 178 179 log.error("Loading database from '" + pathname + "':", e); 180 throw e; 181 182 } finally { 183 184 if (bis != null) { 185 try { 186 bis.close(); 187 } catch (Throwable t) { 188 ; 189 } 190 bis = null; 191 fis = null; 192 } 193 194 } 195 196 } 197 198 199 public void removeUser(User user) { 201 202 if (!(this == user.getDatabase())) { 203 throw new IllegalArgumentException 204 ("User not associated with this database"); 205 } 206 if (log.isTraceEnabled()) { 207 log.trace("Removing user '" + user.getUsername() + "'"); 208 } 209 synchronized (users) { 210 users.remove(user.getUsername()); 211 } 212 213 } 214 215 216 public void save() throws Exception { 218 219 if (log.isDebugEnabled()) { 220 log.debug("Saving database to '" + pathname + "'"); 221 } 222 File fileNew = new File (pathnameNew); 223 PrintWriter writer = null; 224 225 try { 226 227 FileOutputStream fos = new FileOutputStream (fileNew); 229 OutputStreamWriter osw = new OutputStreamWriter (fos); 230 writer = new PrintWriter (osw); 231 232 writer.println("<?xml version='1.0'?>"); 234 writer.println("<database>"); 235 236 User users[] = findUsers(); 238 for (int i = 0; i < users.length; i++) { 239 writer.print(" "); 240 writer.println(users[i]); 241 Subscription subscriptions[] = 242 users[i].getSubscriptions(); 243 for (int j = 0; j < subscriptions.length; j++) { 244 writer.print(" "); 245 writer.println(subscriptions[j]); 246 writer.print(" "); 247 writer.println("</subscription>"); 248 } 249 writer.print(" "); 250 writer.println("</user>"); 251 } 252 253 writer.println("</database>"); 255 256 if (writer.checkError()) { 258 writer.close(); 259 fileNew.delete(); 260 throw new IOException 261 ("Saving database to '" + pathname + "'"); 262 } 263 writer.close(); 264 writer = null; 265 266 } catch (IOException e) { 267 268 if (writer != null) { 269 writer.close(); 270 } 271 fileNew.delete(); 272 throw e; 273 274 } 275 276 277 File fileOrig = new File (pathname); 279 File fileOld = new File (pathnameOld); 280 if (fileOrig.exists()) { 281 fileOld.delete(); 282 if (!fileOrig.renameTo(fileOld)) { 283 throw new IOException 284 ("Renaming '" + pathname + "' to '" + pathnameOld + "'"); 285 } 286 } 287 if (!fileNew.renameTo(fileOrig)) { 288 if (fileOld.exists()) { 289 fileOld.renameTo(fileOrig); 290 } 291 throw new IOException 292 ("Renaming '" + pathnameNew + "' to '" + pathname + "'"); 293 } 294 fileOld.delete(); 295 296 } 297 298 299 } 300 301 302 305 class MemorySubscriptionCreationFactory implements ObjectCreationFactory { 306 307 private Digester digester = null; 308 309 public Digester getDigester() { 310 return (this.digester); 311 } 312 313 public void setDigester(Digester digester) { 314 this.digester = digester; 315 } 316 317 public Object createObject(Attributes attributes) { 318 String host = attributes.getValue("host"); 319 User user = (User) digester.peek(); 320 Subscription subscription = user.createSubscription(host); 321 String autoConnect = attributes.getValue("autoConnect"); 322 if (autoConnect == null) { 323 autoConnect = "false"; 324 } 325 if ("true".equalsIgnoreCase(autoConnect) || 326 "yes".equalsIgnoreCase(autoConnect)) { 327 subscription.setAutoConnect(true); 328 } else { 329 subscription.setAutoConnect(false); 330 } 331 subscription.setPassword(attributes.getValue("password")); 332 subscription.setType(attributes.getValue("type")); 333 subscription.setUsername(attributes.getValue("username")); 334 return (subscription); 335 } 336 337 } 338 339 340 343 class MemoryUserCreationFactory implements ObjectCreationFactory { 344 345 public MemoryUserCreationFactory(MemoryUserDatabase database) { 346 this.database = database; 347 } 348 349 private MemoryUserDatabase database = null; 350 351 private Digester digester = null; 352 353 public Digester getDigester() { 354 return (this.digester); 355 } 356 357 public void setDigester(Digester digester) { 358 this.digester = digester; 359 } 360 361 public Object createObject(Attributes attributes) { 362 String username = attributes.getValue("username"); 363 User user = database.createUser(username); 364 user.setFromAddress(attributes.getValue("fromAddress")); 365 user.setFullName(attributes.getValue("fullName")); 366 user.setPassword(attributes.getValue("password")); 367 user.setReplyToAddress(attributes.getValue("replyToAddress")); 368 return (user); 369 } 370 371 } 372 | Popular Tags |