KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > example > 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
20 package org.apache.struts.webapp.example.memory;
21
22 import java.util.HashMap JavaDoc;
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 /**
29  * <p>Concrete implementation of {@link User} for an in-memory
30  * database backed by an XML data file.</p>
31  *
32  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
33  * @since Struts 1.1
34  */

35
36 public final class MemoryUser implements User {
37
38
39     // ----------------------------------------------------------- Constructors
40

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

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

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

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

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

76     private String JavaDoc username = null;
77
78
79     // ------------------------------------------------------------- Properties
80

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

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

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

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

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

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

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

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

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

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

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

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

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