View Javadoc
1   /*
2    * $Id$
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  
26  import java.util.HashMap;
27  
28  import org.apache.struts.apps.mailreader.dao.Subscription;
29  import org.apache.struts.apps.mailreader.dao.User;
30  import org.apache.struts.apps.mailreader.dao.UserDatabase;
31  
32  
33  /**
34   * <p>Concrete implementation of {@link AbstractUser}.</p>
35   *
36   * @author Craig R. McClanahan
37   * @version $Rev$ $Date$
38   * @since Struts 1.1
39   */
40  
41  public abstract class AbstractUser implements User {
42  
43  
44      // ----------------------------------------------------------- Constructors
45  
46  
47      /**
48       * <p>Construct a new User associated with the specified
49       * {@link UserDatabase}.
50       *
51       * @param database The user database with which we are associated
52       * @param username The username of this user
53       */
54      public AbstractUser(UserDatabase database, String username) {
55  
56          super();
57          this.database = database;
58          this.username = username;
59  
60      }
61  
62  
63      // ----------------------------------------------------- Instance Variables
64  
65  
66      /**
67       * The {@link UserDatabase} with which we are associated.
68       */
69      private UserDatabase database = null;
70  
71  
72      /**
73       * The {@link Subscription}s for this User, keyed by hostname.
74       */
75      private HashMap<String, Subscription> subscriptions = new HashMap<>();
76  
77  
78      /**
79       * The username for this user.
80       */
81      private String username = null;
82  
83  
84      // ------------------------------------------------------------- Properties
85  
86  
87      /**
88       * The {@link UserDatabase} with which we are associated.
89       */
90      public UserDatabase getDatabase() {
91          return (this.database);
92      }
93  
94  
95      /**
96       * The email address from which messages are sent.
97       */
98      private String fromAddress = null;
99  
100     public String getFromAddress() {
101         return (this.fromAddress);
102     }
103 
104     public void setFromAddress(String fromAddress) {
105         this.fromAddress = fromAddress;
106     }
107 
108 
109     /**
110      * The full name of this user, included in from addresses.
111      */
112     private String fullName = null;
113 
114     public String getFullName() {
115         return (this.fullName);
116     }
117 
118     public void setFullName(String fullName) {
119         this.fullName = fullName;
120     }
121 
122 
123     /**
124      * The password (in clear text).
125      */
126     private String password = null;
127 
128     public String getPassword() {
129         return (this.password);
130     }
131 
132     public void setPassword(String password) {
133         this.password = password;
134     }
135 
136 
137     /**
138      * The EMAIL address to which replies should be sent.
139      */
140     private String replyToAddress = null;
141 
142     public String getReplyToAddress() {
143         return (this.replyToAddress);
144     }
145 
146     public void setReplyToAddress(String replyToAddress) {
147         this.replyToAddress = replyToAddress;
148     }
149 
150 
151     /**
152      * Find and return all {@link Subscription}s associated with this user.
153      * If there are none, a zero-length array is returned.
154      */
155     public Subscription[] getSubscriptions() {
156 
157         synchronized (subscriptions) {
158             return subscriptions.values().toArray(new Subscription[0]);
159         }
160 
161     }
162 
163 
164     /**
165      * The username (must be unique).
166      */
167     public String getUsername() {
168         return (this.username);
169     }
170 
171 
172     // --------------------------------------------------------- Public Methods
173 
174 
175     /**
176      * Create and return a new {@link Subscription} associated with this
177      * User, for the specified host name.
178      *
179      * @param host Host name for which to create a subscription
180      *
181      * @exception IllegalArgumentException if the host name is not unique
182      *  for this user
183      */
184     public Subscription createSubscription(String host) {
185 
186         synchronized (subscriptions) {
187             if (subscriptions.get(host) != null) {
188                 throw new IllegalArgumentException("Duplicate host '" + host
189                                                    + "' for user '" +
190                                                    username + "'");
191             }
192             Subscription subscription =
193                 new AbstractSubscription(this, host);
194             synchronized (subscriptions) {
195                 subscriptions.put(host, subscription);
196             }
197             return (subscription);
198         }
199 
200     }
201 
202 
203     /**
204      * Find and return the {@link Subscription} associated with the specified
205      * host.  If none is found, return <code>null</code>.
206      *
207      * @param host Host name to look up
208      */
209     public Subscription findSubscription(String host) {
210 
211         synchronized (subscriptions) {
212             return (subscriptions.get(host));
213         }
214 
215     }
216 
217 
218     /**
219      * Remove the specified {@link Subscription} from being associated
220      * with this User.
221      *
222      * @param subscription Subscription to be removed
223      *
224      * @exception IllegalArgumentException if the specified subscription is not
225      *  associated with this User
226      */
227     public void removeSubscription(Subscription subscription) {
228 
229         if (!(this == subscription.getUser())) {
230             throw new IllegalArgumentException
231                 ("Subscription not associated with this user");
232         }
233         synchronized (subscriptions) {
234             subscriptions.remove(subscription.getHost());
235         }
236 
237     }
238 
239 
240 }