1 18 19 20 package org.apache.struts.webapp.example.memory; 21 22 import java.util.HashMap ; 23 import org.apache.struts.webapp.example.Subscription; 24 import org.apache.struts.webapp.example.User; 25 import org.apache.struts.webapp.example.UserDatabase; 26 27 28 35 36 public final class MemoryUser implements User { 37 38 39 41 42 49 public MemoryUser(MemoryUserDatabase database, String username) { 50 51 super(); 52 this.database = database; 53 this.username = username; 54 55 } 56 57 58 60 61 64 private MemoryUserDatabase database = null; 65 66 67 70 private HashMap subscriptions = new HashMap (); 71 72 73 76 private String username = null; 77 78 79 81 82 85 public UserDatabase getDatabase() { 86 return (this.database); 87 } 88 89 90 93 private String fromAddress = null; 94 95 public String getFromAddress() { 96 return (this.fromAddress); 97 } 98 99 public void setFromAddress(String fromAddress) { 100 this.fromAddress = fromAddress; 101 } 102 103 104 107 private String fullName = null; 108 109 public String getFullName() { 110 return (this.fullName); 111 } 112 113 public void setFullName(String fullName) { 114 this.fullName = fullName; 115 } 116 117 118 121 private String password = null; 122 123 public String getPassword() { 124 return (this.password); 125 } 126 127 public void setPassword(String password) { 128 this.password = password; 129 } 130 131 132 135 private String replyToAddress = null; 136 137 public String getReplyToAddress() { 138 return (this.replyToAddress); 139 } 140 141 public void setReplyToAddress(String replyToAddress) { 142 this.replyToAddress = replyToAddress; 143 } 144 145 146 150 public Subscription[] getSubscriptions() { 151 152 synchronized (subscriptions) { 153 Subscription results[] = new Subscription[subscriptions.size()]; 154 return ((Subscription[]) subscriptions.values().toArray(results)); 155 } 156 157 } 158 159 160 163 public String getUsername() { 164 return (this.username); 165 } 166 167 168 170 171 180 public Subscription createSubscription(String host) { 181 182 synchronized (subscriptions) { 183 if (subscriptions.get(host) != null) { 184 throw new IllegalArgumentException ("Duplicate host '" + host 185 + "' for user '" + 186 username + "'"); 187 } 188 MemorySubscription subscription = 189 new MemorySubscription(this, host); 190 synchronized (subscriptions) { 191 subscriptions.put(host, subscription); 192 } 193 return (subscription); 194 } 195 196 } 197 198 199 205 public Subscription findSubscription(String host) { 206 207 synchronized (subscriptions) { 208 return ((Subscription) subscriptions.get(host)); 209 } 210 211 } 212 213 214 223 public void removeSubscription(Subscription subscription) { 224 225 if (!(this == subscription.getUser())) { 226 throw new IllegalArgumentException 227 ("Subscription not associated with this user"); 228 } 229 synchronized (subscriptions) { 230 subscriptions.remove(subscription.getHost()); 231 } 232 233 } 234 235 236 239 public String toString() { 240 241 StringBuffer sb = new StringBuffer ("<user username=\""); 242 sb.append(username); 243 sb.append("\""); 244 if (fromAddress != null) { 245 sb.append(" fromAddress=\""); 246 sb.append(fromAddress); 247 sb.append("\""); 248 } 249 if (fullName != null) { 250 sb.append(" fullName=\""); 251 sb.append(fullName); 252 sb.append("\""); 253 } 254 if (password != null) { 255 sb.append(" password=\""); 256 sb.append(password); 257 sb.append("\""); 258 } 259 if (replyToAddress != null) { 260 sb.append(" replyToAddress=\""); 261 sb.append(replyToAddress); 262 sb.append("\""); 263 } 264 sb.append(">"); 265 return (sb.toString()); 266 267 } 268 269 270 } 271 | Popular Tags |