1 /*
2 * $Id: AbstractUser.java 471754 2006-11-06 14:55:09Z husted $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22
23 package org.apache.struts.apps.mailreader.dao.impl;
24
25 import java.util.HashMap;
26
27 import org.apache.struts.apps.mailreader.dao.Subscription;
28 import org.apache.struts.apps.mailreader.dao.User;
29 import org.apache.struts.apps.mailreader.dao.UserDatabase;
30
31
32 /**
33 * <p>Concrete implementation of {@link AbstractUser}.</p>
34 *
35 * @version $Rev: 471754 $
36 * @since Struts 1.1
37 */
38
39 public abstract class AbstractUser implements User {
40
41
42 // ----------------------------------------------------------- Constructors
43
44
45 /**
46 * <p>Construct a new User associated with the specified
47 * {@link UserDatabase}.
48 *
49 * @param database The user database with which we are associated
50 * @param username The username of this user
51 */
52 public AbstractUser(UserDatabase database, String username) {
53
54 super();
55 this.database = database;
56 this.username = username;
57
58 }
59
60
61 // ----------------------------------------------------- Instance Variables
62
63
64 /**
65 * The {@link UserDatabase} with which we are associated.
66 */
67 private UserDatabase database = null;
68
69
70 /**
71 * The {@link Subscription}s for this User, keyed by hostname.
72 */
73 private HashMap subscriptions = new HashMap();
74
75
76 /**
77 * The username for this user.
78 */
79 private String username = null;
80
81
82 // ------------------------------------------------------------- Properties
83
84
85 /**
86 * The {@link UserDatabase} with which we are associated.
87 */
88 public UserDatabase getDatabase() {
89 return (this.database);
90 }
91
92
93 /**
94 * The email address from which messages are sent.
95 */
96 private String fromAddress = null;
97
98 public String getFromAddress() {
99 return (this.fromAddress);
100 }
101
102 public void setFromAddress(String fromAddress) {
103 this.fromAddress = fromAddress;
104 }
105
106
107 /**
108 * The full name of this user, included in from addresses.
109 */
110 private String fullName = null;
111
112 public String getFullName() {
113 return (this.fullName);
114 }
115
116 public void setFullName(String fullName) {
117 this.fullName = fullName;
118 }
119
120
121 /**
122 * The password (in clear text).
123 */
124 private String password = null;
125
126 public String getPassword() {
127 return (this.password);
128 }
129
130 public void setPassword(String password) {
131 this.password = password;
132 }
133
134
135 /**
136 * The EMAIL address to which replies should be sent.
137 */
138 private String replyToAddress = null;
139
140 public String getReplyToAddress() {
141 return (this.replyToAddress);
142 }
143
144 public void setReplyToAddress(String replyToAddress) {
145 this.replyToAddress = replyToAddress;
146 }
147
148
149 /**
150 * Find and return all {@link Subscription}s associated with this user.
151 * If there are none, a zero-length array is returned.
152 */
153 public Subscription[] getSubscriptions() {
154
155 synchronized (subscriptions) {
156 Subscription results[] = new Subscription[subscriptions.size()];
157 return ((Subscription[]) subscriptions.values().toArray(results));
158 }
159
160 }
161
162
163 /**
164 * The username (must be unique).
165 */
166 public String getUsername() {
167 return (this.username);
168 }
169
170
171 // --------------------------------------------------------- Public Methods
172
173
174 /**
175 * Create and return a new {@link Subscription} associated with this
176 * User, for the specified host name.
177 *
178 * @param host Host name for which to create a subscription
179 *
180 * @exception IllegalArgumentException if the host name is not unique
181 * for this user
182 */
183 public Subscription createSubscription(String host) {
184
185 synchronized (subscriptions) {
186 if (subscriptions.get(host) != null) {
187 throw new IllegalArgumentException("Duplicate host '" + host
188 + "' for user '" +
189 username + "'");
190 }
191 Subscription subscription =
192 new AbstractSubscription(this, host);
193 synchronized (subscriptions) {
194 subscriptions.put(host, subscription);
195 }
196 return (subscription);
197 }
198
199 }
200
201
202 /**
203 * Find and return the {@link Subscription} associated with the specified
204 * host. If none is found, return <code>null</code>.
205 *
206 * @param host Host name to look up
207 */
208 public Subscription findSubscription(String host) {
209
210 synchronized (subscriptions) {
211 return ((Subscription) subscriptions.get(host));
212 }
213
214 }
215
216
217 /**
218 * Remove the specified {@link Subscription} from being associated
219 * with this User.
220 *
221 * @param subscription Subscription to be removed
222 *
223 * @exception IllegalArgumentException if the specified subscription is not
224 * associated with this User
225 */
226 public void removeSubscription(Subscription subscription) {
227
228 if (!(this == subscription.getUser())) {
229 throw new IllegalArgumentException
230 ("Subscription not associated with this user");
231 }
232 synchronized (subscriptions) {
233 subscriptions.remove(subscription.getHost());
234 }
235
236 }
237
238
239 }