001: /*
002: * $Id: MemoryUser.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.webapp.example2.memory;
023:
024: import java.util.HashMap;
025: import org.apache.struts.webapp.example2.Subscription;
026: import org.apache.struts.webapp.example2.User;
027: import org.apache.struts.webapp.example2.UserDatabase;
028:
029: /**
030: * <p>Concrete implementation of {@link User} for an in-memory
031: * database backed by an XML data file.</p>
032: *
033: * @author Craig R. McClanahan
034: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
035: * @since Struts 1.1
036: */
037:
038: public final class MemoryUser implements User {
039:
040: // ----------------------------------------------------------- Constructors
041:
042: /**
043: * <p>Construct a new User associated with the specified
044: * {@link UserDatabase}.
045: *
046: * @param database The user database with which we are associated
047: * @param username The username of this user
048: */
049: public MemoryUser(MemoryUserDatabase database, String username) {
050:
051: super ();
052: this .database = database;
053: this .username = username;
054:
055: }
056:
057: // ----------------------------------------------------- Instance Variables
058:
059: /**
060: * The {@link UserDatabase} with which we are associated.
061: */
062: private MemoryUserDatabase database = null;
063:
064: /**
065: * The {@link Subscription}s for this User, keyed by hostname.
066: */
067: private HashMap subscriptions = new HashMap();
068:
069: /**
070: * The username for this user.
071: */
072: private String username = null;
073:
074: // ------------------------------------------------------------- Properties
075:
076: /**
077: * The {@link UserDatabase} with which we are associated.
078: */
079: public UserDatabase getDatabase() {
080: return (this .database);
081: }
082:
083: /**
084: * The email address from which messages are sent.
085: */
086: private String fromAddress = null;
087:
088: public String getFromAddress() {
089: return (this .fromAddress);
090: }
091:
092: public void setFromAddress(String fromAddress) {
093: this .fromAddress = fromAddress;
094: }
095:
096: /**
097: * The full name of this user, included in from addresses.
098: */
099: private String fullName = null;
100:
101: public String getFullName() {
102: return (this .fullName);
103: }
104:
105: public void setFullName(String fullName) {
106: this .fullName = fullName;
107: }
108:
109: /**
110: * The password (in clear text).
111: */
112: private String password = null;
113:
114: public String getPassword() {
115: return (this .password);
116: }
117:
118: public void setPassword(String password) {
119: this .password = password;
120: }
121:
122: /**
123: * The EMAIL address to which replies should be sent.
124: */
125: private String replyToAddress = null;
126:
127: public String getReplyToAddress() {
128: return (this .replyToAddress);
129: }
130:
131: public void setReplyToAddress(String replyToAddress) {
132: this .replyToAddress = replyToAddress;
133: }
134:
135: /**
136: * Find and return all {@link Subscription}s associated with this user.
137: * If there are none, a zero-length array is returned.
138: */
139: public Subscription[] getSubscriptions() {
140:
141: synchronized (subscriptions) {
142: Subscription results[] = new Subscription[subscriptions
143: .size()];
144: return ((Subscription[]) subscriptions.values().toArray(
145: results));
146: }
147:
148: }
149:
150: /**
151: * The username (must be unique).
152: */
153: public String getUsername() {
154: return (this .username);
155: }
156:
157: // --------------------------------------------------------- Public Methods
158:
159: /**
160: * Create and return a new {@link Subscription} associated with this
161: * User, for the specified host name.
162: *
163: * @param host Host name for which to create a subscription
164: *
165: * @exception IllegalArgumentException if the host name is not unique
166: * for this user
167: */
168: public Subscription createSubscription(String host) {
169:
170: synchronized (subscriptions) {
171: if (subscriptions.get(host) != null) {
172: throw new IllegalArgumentException("Duplicate host '"
173: + host + "' for user '" + username + "'");
174: }
175: MemorySubscription subscription = new MemorySubscription(
176: this , host);
177: synchronized (subscriptions) {
178: subscriptions.put(host, subscription);
179: }
180: return (subscription);
181: }
182:
183: }
184:
185: /**
186: * Find and return the {@link Subscription} associated with the specified
187: * host. If none is found, return <code>null</code>.
188: *
189: * @param host Host name to look up
190: */
191: public Subscription findSubscription(String host) {
192:
193: synchronized (subscriptions) {
194: return ((Subscription) subscriptions.get(host));
195: }
196:
197: }
198:
199: /**
200: * Remove the specified {@link Subscription} from being associated
201: * with this User.
202: *
203: * @param subscription Subscription to be removed
204: *
205: * @exception IllegalArgumentException if the specified subscription is not
206: * associated with this User
207: */
208: public void removeSubscription(Subscription subscription) {
209:
210: if (!(this == subscription.getUser())) {
211: throw new IllegalArgumentException(
212: "Subscription not associated with this user");
213: }
214: synchronized (subscriptions) {
215: subscriptions.remove(subscription.getHost());
216: }
217:
218: }
219:
220: /**
221: * Return a String representation of this object.
222: */
223: public String toString() {
224:
225: StringBuffer sb = new StringBuffer("<user username=\"");
226: sb.append(username);
227: sb.append("\"");
228: if (fromAddress != null) {
229: sb.append(" fromAddress=\"");
230: sb.append(fromAddress);
231: sb.append("\"");
232: }
233: if (fullName != null) {
234: sb.append(" fullName=\"");
235: sb.append(fullName);
236: sb.append("\"");
237: }
238: if (password != null) {
239: sb.append(" password=\"");
240: sb.append(password);
241: sb.append("\"");
242: }
243: if (replyToAddress != null) {
244: sb.append(" replyToAddress=\"");
245: sb.append(replyToAddress);
246: sb.append("\"");
247: }
248: sb.append(">");
249: return (sb.toString());
250:
251: }
252:
253: }
|