001: /*
002: * $Id: AbstractUser.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.apps.mailreader.dao.impl;
023:
024: import java.util.HashMap;
025:
026: import org.apache.struts.apps.mailreader.dao.Subscription;
027: import org.apache.struts.apps.mailreader.dao.User;
028: import org.apache.struts.apps.mailreader.dao.UserDatabase;
029:
030: /**
031: * <p>Concrete implementation of {@link AbstractUser}.</p>
032: *
033: * @version $Rev: 471754 $
034: * @since Struts 1.1
035: */
036:
037: public abstract class AbstractUser implements User {
038:
039: // ----------------------------------------------------------- Constructors
040:
041: /**
042: * <p>Construct a new User associated with the specified
043: * {@link UserDatabase}.
044: *
045: * @param database The user database with which we are associated
046: * @param username The username of this user
047: */
048: public AbstractUser(UserDatabase database, String username) {
049:
050: super ();
051: this .database = database;
052: this .username = username;
053:
054: }
055:
056: // ----------------------------------------------------- Instance Variables
057:
058: /**
059: * The {@link UserDatabase} with which we are associated.
060: */
061: private UserDatabase database = null;
062:
063: /**
064: * The {@link Subscription}s for this User, keyed by hostname.
065: */
066: private HashMap subscriptions = new HashMap();
067:
068: /**
069: * The username for this user.
070: */
071: private String username = null;
072:
073: // ------------------------------------------------------------- Properties
074:
075: /**
076: * The {@link UserDatabase} with which we are associated.
077: */
078: public UserDatabase getDatabase() {
079: return (this .database);
080: }
081:
082: /**
083: * The email address from which messages are sent.
084: */
085: private String fromAddress = null;
086:
087: public String getFromAddress() {
088: return (this .fromAddress);
089: }
090:
091: public void setFromAddress(String fromAddress) {
092: this .fromAddress = fromAddress;
093: }
094:
095: /**
096: * The full name of this user, included in from addresses.
097: */
098: private String fullName = null;
099:
100: public String getFullName() {
101: return (this .fullName);
102: }
103:
104: public void setFullName(String fullName) {
105: this .fullName = fullName;
106: }
107:
108: /**
109: * The password (in clear text).
110: */
111: private String password = null;
112:
113: public String getPassword() {
114: return (this .password);
115: }
116:
117: public void setPassword(String password) {
118: this .password = password;
119: }
120:
121: /**
122: * The EMAIL address to which replies should be sent.
123: */
124: private String replyToAddress = null;
125:
126: public String getReplyToAddress() {
127: return (this .replyToAddress);
128: }
129:
130: public void setReplyToAddress(String replyToAddress) {
131: this .replyToAddress = replyToAddress;
132: }
133:
134: /**
135: * Find and return all {@link Subscription}s associated with this user.
136: * If there are none, a zero-length array is returned.
137: */
138: public Subscription[] getSubscriptions() {
139:
140: synchronized (subscriptions) {
141: Subscription results[] = new Subscription[subscriptions
142: .size()];
143: return ((Subscription[]) subscriptions.values().toArray(
144: results));
145: }
146:
147: }
148:
149: /**
150: * The username (must be unique).
151: */
152: public String getUsername() {
153: return (this .username);
154: }
155:
156: // --------------------------------------------------------- Public Methods
157:
158: /**
159: * Create and return a new {@link Subscription} associated with this
160: * User, for the specified host name.
161: *
162: * @param host Host name for which to create a subscription
163: *
164: * @exception IllegalArgumentException if the host name is not unique
165: * for this user
166: */
167: public Subscription createSubscription(String host) {
168:
169: synchronized (subscriptions) {
170: if (subscriptions.get(host) != null) {
171: throw new IllegalArgumentException("Duplicate host '"
172: + host + "' for user '" + username + "'");
173: }
174: Subscription subscription = new AbstractSubscription(this ,
175: host);
176: synchronized (subscriptions) {
177: subscriptions.put(host, subscription);
178: }
179: return (subscription);
180: }
181:
182: }
183:
184: /**
185: * Find and return the {@link Subscription} associated with the specified
186: * host. If none is found, return <code>null</code>.
187: *
188: * @param host Host name to look up
189: */
190: public Subscription findSubscription(String host) {
191:
192: synchronized (subscriptions) {
193: return ((Subscription) subscriptions.get(host));
194: }
195:
196: }
197:
198: /**
199: * Remove the specified {@link Subscription} from being associated
200: * with this User.
201: *
202: * @param subscription Subscription to be removed
203: *
204: * @exception IllegalArgumentException if the specified subscription is not
205: * associated with this User
206: */
207: public void removeSubscription(Subscription subscription) {
208:
209: if (!(this == subscription.getUser())) {
210: throw new IllegalArgumentException(
211: "Subscription not associated with this user");
212: }
213: synchronized (subscriptions) {
214: subscriptions.remove(subscription.getHost());
215: }
216:
217: }
218:
219: }
|