KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > examples > mailreader > memory > MemoryUser


1 /*
2  * $Id: MemoryUser.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.examples.mailreader.memory;
20
21 import java.util.HashMap JavaDoc;
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 /**
27  * <p>Concrete implementation of {@link User} for an in-memory
28  * database backed by an XML data file.</p>
29  *
30  * @version $Rev: 54929 $ $Date: 2004-10-16 09:38:42 -0700 (Sat, 16 Oct 2004) $
31  * @since Struts 1.1
32  */

33
34 public final class MemoryUser implements User {
35
36
37     // ----------------------------------------------------------- Constructors
38

39
40     /**
41      * <p>Construct a new User associated with the specified
42      * {@link UserDatabase}.
43      *
44      * @param database The user database with which we are associated
45      * @param username The username of this user
46      */

47     public MemoryUser(MemoryUserDatabase database, String JavaDoc username) {
48
49         super();
50         this.database = database;
51         this.username = username;
52
53     }
54
55
56     // ----------------------------------------------------- Instance Variables
57

58
59     /**
60      * The {@link UserDatabase} with which we are associated.
61      */

62     private MemoryUserDatabase database = null;
63
64
65     /**
66      * The {@link Subscription}s for this User, keyed by hostname.
67      */

68     private HashMap JavaDoc subscriptions = new HashMap JavaDoc();
69
70
71     /**
72      * The username for this user.
73      */

74     private String JavaDoc username = null;
75
76
77     // ------------------------------------------------------------- Properties
78

79
80     /**
81      * The {@link UserDatabase} with which we are associated.
82      */

83     public UserDatabase getDatabase() {
84         return (this.database);
85     }
86
87
88     /**
89      * The email address from which messages are sent.
90      */

91     private String JavaDoc fromAddress = null;
92
93     public String JavaDoc getFromAddress() {
94         return (this.fromAddress);
95     }
96
97     public void setFromAddress(String JavaDoc fromAddress) {
98         this.fromAddress = fromAddress;
99     }
100
101
102     /**
103      * The full name of this user, included in from addresses.
104      */

105     private String JavaDoc fullName = null;
106
107     public String JavaDoc getFullName() {
108         return (this.fullName);
109     }
110
111     public void setFullName(String JavaDoc fullName) {
112         this.fullName = fullName;
113     }
114
115
116     /**
117      * The password (in clear text).
118      */

119     private String JavaDoc password = null;
120
121     public String JavaDoc getPassword() {
122         return (this.password);
123     }
124
125     public void setPassword(String JavaDoc password) {
126         this.password = password;
127     }
128
129
130     /**
131      * The EMAIL address to which replies should be sent.
132      */

133     private String JavaDoc replyToAddress = null;
134
135     public String JavaDoc getReplyToAddress() {
136         return (this.replyToAddress);
137     }
138
139     public void setReplyToAddress(String JavaDoc replyToAddress) {
140         this.replyToAddress = replyToAddress;
141     }
142
143
144     /**
145      * Find and return all {@link Subscription}s associated with this user.
146      * If there are none, a zero-length array is returned.
147      */

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     /**
159      * The username (must be unique).
160      */

161     public String JavaDoc getUsername() {
162         return (this.username);
163     }
164
165
166     // --------------------------------------------------------- Public Methods
167

168
169     /**
170      * Create and return a new {@link Subscription} associated with this
171      * User, for the specified host name.
172      *
173      * @param host Host name for which to create a subscription
174      *
175      * @exception IllegalArgumentException if the host name is not unique
176      * for this user
177      */

178     public Subscription createSubscription(String JavaDoc host) {
179
180         synchronized (subscriptions) {
181             if (subscriptions.get(host) != null) {
182                 throw new IllegalArgumentException JavaDoc("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     /**
198      * Find and return the {@link Subscription} associated with the specified
199      * host. If none is found, return <code>null</code>.
200      *
201      * @param host Host name to look up
202      */

203     public Subscription findSubscription(String JavaDoc host) {
204
205         synchronized (subscriptions) {
206             return ((Subscription) subscriptions.get(host));
207         }
208
209     }
210
211
212     /**
213      * Remove the specified {@link Subscription} from being associated
214      * with this User.
215      *
216      * @param subscription Subscription to be removed
217      *
218      * @exception IllegalArgumentException if the specified subscription is not
219      * associated with this User
220      */

221     public void removeSubscription(Subscription subscription) {
222
223         if (!(this == subscription.getUser())) {
224             throw new IllegalArgumentException JavaDoc
225                 ("Subscription not associated with this user");
226         }
227         synchronized (subscriptions) {
228             subscriptions.remove(subscription.getHost());
229         }
230
231     }
232
233
234     /**
235      * Return a String representation of this object.
236      */

237     public String JavaDoc toString() {
238
239         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<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