1 18 19 package org.apache.struts.examples.mailreader.memory; 20 21 import java.util.HashMap ; 22 import org.apache.struts.examples.mailreader.Subscription; 23 import org.apache.struts.examples.mailreader.User; 24 import org.apache.struts.examples.mailreader.UserDatabase; 25 26 33 34 public final class MemoryUser implements User { 35 36 37 39 40 47 public MemoryUser(MemoryUserDatabase database, String username) { 48 49 super(); 50 this.database = database; 51 this.username = username; 52 53 } 54 55 56 58 59 62 private MemoryUserDatabase database = null; 63 64 65 68 private HashMap subscriptions = new HashMap (); 69 70 71 74 private String username = null; 75 76 77 79 80 83 public UserDatabase getDatabase() { 84 return (this.database); 85 } 86 87 88 91 private String fromAddress = null; 92 93 public String getFromAddress() { 94 return (this.fromAddress); 95 } 96 97 public void setFromAddress(String fromAddress) { 98 this.fromAddress = fromAddress; 99 } 100 101 102 105 private String fullName = null; 106 107 public String getFullName() { 108 return (this.fullName); 109 } 110 111 public void setFullName(String fullName) { 112 this.fullName = fullName; 113 } 114 115 116 119 private String password = null; 120 121 public String getPassword() { 122 return (this.password); 123 } 124 125 public void setPassword(String password) { 126 this.password = password; 127 } 128 129 130 133 private String replyToAddress = null; 134 135 public String getReplyToAddress() { 136 return (this.replyToAddress); 137 } 138 139 public void setReplyToAddress(String replyToAddress) { 140 this.replyToAddress = replyToAddress; 141 } 142 143 144 148 public Subscription[] getSubscriptions() { 149 150 synchronized (subscriptions) { 151 Subscription results[] = new Subscription[subscriptions.size()]; 152 return ((Subscription[]) subscriptions.values().toArray(results)); 153 } 154 155 } 156 157 158 161 public String getUsername() { 162 return (this.username); 163 } 164 165 166 168 169 178 public Subscription createSubscription(String host) { 179 180 synchronized (subscriptions) { 181 if (subscriptions.get(host) != null) { 182 throw new IllegalArgumentException ("Duplicate host '" + host 183 + "' for user '" + 184 username + "'"); 185 } 186 MemorySubscription subscription = 187 new MemorySubscription(this, host); 188 synchronized (subscriptions) { 189 subscriptions.put(host, subscription); 190 } 191 return (subscription); 192 } 193 194 } 195 196 197 203 public Subscription findSubscription(String host) { 204 205 synchronized (subscriptions) { 206 return ((Subscription) subscriptions.get(host)); 207 } 208 209 } 210 211 212 221 public void removeSubscription(Subscription subscription) { 222 223 if (!(this == subscription.getUser())) { 224 throw new IllegalArgumentException 225 ("Subscription not associated with this user"); 226 } 227 synchronized (subscriptions) { 228 subscriptions.remove(subscription.getHost()); 229 } 230 231 } 232 233 234 237 public String toString() { 238 239 StringBuffer sb = new StringBuffer ("<user username=\""); 240 sb.append(username); 241 sb.append("\""); 242 if (fromAddress != null) { 243 sb.append(" fromAddress=\""); 244 sb.append(fromAddress); 245 sb.append("\""); 246 } 247 if (fullName != null) { 248 sb.append(" fullName=\""); 249 sb.append(fullName); 250 sb.append("\""); 251 } 252 if (password != null) { 253 sb.append(" password=\""); 254 sb.append(password); 255 sb.append("\""); 256 } 257 if (replyToAddress != null) { 258 sb.append(" replyToAddress=\""); 259 sb.append(replyToAddress); 260 sb.append("\""); 261 } 262 sb.append(">"); 263 return (sb.toString()); 264 265 } 266 267 268 } 269 | Popular Tags |