View Javadoc

1   /*
2    * Copyright 1999-2002,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.struts.webapp.example.memory;
19  
20  
21  import java.util.HashMap;
22  import org.apache.struts.webapp.example.Subscription;
23  import org.apache.struts.webapp.example.User;
24  import org.apache.struts.webapp.example.UserDatabase;
25  
26  
27  /***
28   * <p>Concrete implementation of {@link User} for an in-memory
29   * database backed by an XML data file.</p>
30   *
31   * @author Craig R. McClanahan
32   * @version $Rev: 421493 $ $Date: 2006-07-12 20:52:31 -0700 (Wed, 12 Jul 2006) $
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 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 subscriptions = new HashMap();
71  
72  
73      /***
74       * The username for this user.
75       */
76      private String 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 fromAddress = null;
94  
95      public String getFromAddress() {
96          return (this.fromAddress);
97      }
98  
99      public void setFromAddress(String fromAddress) {
100         this.fromAddress = fromAddress;
101     }
102 
103 
104     /***
105      * The full name of this user, included in from addresses.
106      */
107     private String fullName = null;
108 
109     public String getFullName() {
110         return (this.fullName);
111     }
112 
113     public void setFullName(String fullName) {
114         this.fullName = fullName;
115     }
116 
117 
118     /***
119      * The password (in clear text).
120      */
121     private String password = null;
122 
123     public String getPassword() {
124         return (this.password);
125     }
126 
127     public void setPassword(String password) {
128         this.password = password;
129     }
130 
131 
132     /***
133      * The EMAIL address to which replies should be sent.
134      */
135     private String replyToAddress = null;
136 
137     public String getReplyToAddress() {
138         return (this.replyToAddress);
139     }
140 
141     public void setReplyToAddress(String 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 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 host) {
181 
182         synchronized (subscriptions) {
183             if (subscriptions.get(host) != null) {
184                 throw new IllegalArgumentException("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 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
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 toString() {
240 
241         StringBuffer sb = new StringBuffer("<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 }