1 16 17 18 package org.apache.struts.webapp.example2.memory; 19 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.webapp.example2.Subscription; 34 import org.apache.struts.webapp.example2.User; 35 import org.apache.struts.webapp.example2.UserDatabase; 36 import org.xml.sax.Attributes ; 37 38 39 47 48 public final class MemoryUserDatabase implements UserDatabase { 49 50 51 53 54 56 57 60 private Log log = LogFactory.getLog(this.getClass()); 61 62 63 66 private HashMap users = new HashMap (); 67 68 69 71 72 76 private String pathname = null; 77 78 private String pathnameOld = null; 79 80 private String pathnameNew = null; 81 82 public String getPathname() { 83 return (this.pathname); 84 } 85 86 public void setPathname(String pathname) { 87 this.pathname = pathname; 88 pathnameOld = pathname + ".old"; 89 pathnameNew = pathname + ".new"; 90 } 91 92 93 95 96 101 public void close() throws Exception { 102 103 save(); 104 105 } 106 107 108 117 public User createUser(String username) { 118 119 synchronized (users) { 120 if (users.get(username) != null) { 121 throw new IllegalArgumentException ("Duplicate user '" + 122 username + "'"); 123 } 124 if (log.isTraceEnabled()) { 125 log.trace("Creating user '" + username + "'"); 126 } 127 MemoryUser user = new MemoryUser(this, username); 128 synchronized (users) { 129 users.put(username, user); 130 } 131 return (user); 132 } 133 134 } 135 136 137 143 public User findUser(String username) { 144 145 synchronized (users) { 146 return ((User) users.get(username)); 147 } 148 149 } 150 151 152 155 public User[] findUsers() { 156 157 synchronized (users) { 158 User results[] = new User[users.size()]; 159 return ((User[]) users.values().toArray(results)); 160 } 161 162 } 163 164 165 170 public void open() throws Exception { 171 172 FileInputStream fis = null; 173 BufferedInputStream bis = null; 174 175 try { 176 177 if (log.isDebugEnabled()) { 179 log.debug("Loading database from '" + pathname + "'"); 180 } 181 fis = new FileInputStream (pathname); 182 bis = new BufferedInputStream (fis); 183 184 Digester digester = new Digester(); 186 digester.push(this); 187 digester.setValidating(false); 188 digester.addFactoryCreate 189 ("database/user", 190 new MemoryUserCreationFactory(this)); 191 digester.addFactoryCreate 192 ("database/user/subscription", 193 new MemorySubscriptionCreationFactory(this)); 194 195 digester.parse(bis); 197 bis.close(); 198 bis = null; 199 fis = null; 200 201 } catch (Exception e) { 202 203 log.error("Loading database from '" + pathname + "':", e); 204 throw e; 205 206 } finally { 207 208 if (bis != null) { 209 try { 210 bis.close(); 211 } catch (Throwable t) { 212 ; 213 } 214 bis = null; 215 fis = null; 216 } 217 218 } 219 220 } 221 222 223 231 public void removeUser(User user) { 232 233 if (!(this == user.getDatabase())) { 234 throw new IllegalArgumentException 235 ("User not associated with this database"); 236 } 237 if (log.isTraceEnabled()) { 238 log.trace("Removing user '" + user.getUsername() + "'"); 239 } 240 synchronized (users) { 241 users.remove(user.getUsername()); 242 } 243 244 } 245 246 247 252 public void save() throws Exception { 253 254 if (log.isDebugEnabled()) { 255 log.debug("Saving database to '" + pathname + "'"); 256 } 257 File fileNew = new File (pathnameNew); 258 PrintWriter writer = null; 259 260 try { 261 262 FileOutputStream fos = new FileOutputStream (fileNew); 264 OutputStreamWriter osw = new OutputStreamWriter (fos); 265 writer = new PrintWriter (osw); 266 267 writer.println("<?xml version='1.0'?>"); 269 writer.println("<database>"); 270 271 User users[] = findUsers(); 273 for (int i = 0; i < users.length; i++) { 274 writer.print(" "); 275 writer.println(users[i]); 276 Subscription subscriptions[] = 277 users[i].getSubscriptions(); 278 for (int j = 0; j < subscriptions.length; j++) { 279 writer.print(" "); 280 writer.println(subscriptions[j]); 281 writer.print(" "); 282 writer.println("</subscription>"); 283 } 284 writer.print(" "); 285 writer.println("</user>"); 286 } 287 288 writer.println("</database>"); 290 291 if (writer.checkError()) { 293 writer.close(); 294 fileNew.delete(); 295 throw new IOException 296 ("Saving database to '" + pathname + "'"); 297 } 298 writer.close(); 299 writer = null; 300 301 } catch (IOException e) { 302 303 if (writer != null) { 304 writer.close(); 305 } 306 fileNew.delete(); 307 throw e; 308 309 } 310 311 312 File fileOrig = new File (pathname); 314 File fileOld = new File (pathnameOld); 315 if (fileOrig.exists()) { 316 fileOld.delete(); 317 if (!fileOrig.renameTo(fileOld)) { 318 throw new IOException 319 ("Renaming '" + pathname + "' to '" + pathnameOld + "'"); 320 } 321 } 322 if (!fileNew.renameTo(fileOrig)) { 323 if (fileOld.exists()) { 324 fileOld.renameTo(fileOrig); 325 } 326 throw new IOException 327 ("Renaming '" + pathnameNew + "' to '" + pathname + "'"); 328 } 329 fileOld.delete(); 330 331 } 332 333 334 } 335 336 337 340 class MemorySubscriptionCreationFactory implements ObjectCreationFactory { 341 342 public MemorySubscriptionCreationFactory(MemoryUserDatabase database) { 343 this.database = database; 344 } 345 346 private MemoryUserDatabase database = null; 347 348 private Digester digester = null; 349 350 public Digester getDigester() { 351 return (this.digester); 352 } 353 354 public void setDigester(Digester digester) { 355 this.digester = digester; 356 } 357 358 public Object createObject(Attributes attributes) { 359 String host = attributes.getValue("host"); 360 User user = (User) digester.peek(); 361 Subscription subscription = user.createSubscription(host); 362 String autoConnect = attributes.getValue("autoConnect"); 363 if (autoConnect == null) { 364 autoConnect = "false"; 365 } 366 if ("true".equalsIgnoreCase(autoConnect) || 367 "yes".equalsIgnoreCase(autoConnect)) { 368 subscription.setAutoConnect(true); 369 } else { 370 subscription.setAutoConnect(false); 371 } 372 subscription.setPassword(attributes.getValue("password")); 373 subscription.setType(attributes.getValue("type")); 374 subscription.setUsername(attributes.getValue("username")); 375 return (subscription); 376 } 377 378 } 379 380 381 384 class MemoryUserCreationFactory implements ObjectCreationFactory { 385 386 public MemoryUserCreationFactory(MemoryUserDatabase database) { 387 this.database = database; 388 } 389 390 private MemoryUserDatabase database = null; 391 392 private Digester digester = null; 393 394 public Digester getDigester() { 395 return (this.digester); 396 } 397 398 public void setDigester(Digester digester) { 399 this.digester = digester; 400 } 401 402 public Object createObject(Attributes attributes) { 403 String username = attributes.getValue("username"); 404 User user = database.createUser(username); 405 user.setFromAddress(attributes.getValue("fromAddress")); 406 user.setFullName(attributes.getValue("fullName")); 407 user.setPassword(attributes.getValue("password")); 408 user.setReplyToAddress(attributes.getValue("replyToAddress")); 409 return (user); 410 } 411 412 } 413 | Popular Tags |