KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > golfShop > data > user > UserDOImpl


1 /*
2  * Enhydra Java Application Server
3  * The Initial Developer of the Original Code is Lutris Technologies Inc.
4  * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
5  * Inc.
6  * All Rights Reserved.
7  *
8  * The contents of this file are subject to the Enhydra Public License Version
9  * 1.0 (the "License"); you may not use this file except in compliance with the
10  * License. You may obtain a copy of the License at
11  * http://www.enhydra.org/software/license/epl.html
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  * License for the specific language governing rights and limitations under the
16  * License.
17  *
18  *
19  */

20
21 package golfShop.data.user;
22
23 import java.io.Serializable JavaDoc;
24 import com.lutris.util.ConfigException;
25 import com.lutris.appserver.server.*;
26 import golfShop.data.user.*;
27 import golfShop.spec.user.UserDO;
28 import com.lutris.appserver.server.user.*;
29 import golfShop.spec.LoginException;
30 /**
31  * The application specific user data object. This is the base object that
32  * does most of the work. All the policy decisions happen here.
33  * The abstract functions are for accessing the store of all users.
34  * There will be at least 3 sub-classes that implement these functions,
35  * one that just keeps the list in memory, one that reads and writes to a
36  * text file, and one that acesses an ldap database. These abstract functions
37  * are private, so that all 3 flavors appear the same to the outside world.
38  * This is a data object, not a business object. This object hides the
39  * storage medium (memory, file, ldap etc..). The class methods get users
40  * from the storage medium, and create new users. The object methods give
41  * you access to the user data, and update changes to the storage medium.
42  * Passwords are protected, but other than that no policy is enforeced.
43  * For example, cheching password length, making sure the correct password
44  * was enntered etc.. is the responsiblity of the user buisness object.
45  *
46  * @author Andrew John
47  * @version $Revision: 1.1 $
48  */

49 public class UserDOImpl implements UserDO,User, Serializable JavaDoc {
50
51 public static final int AUTH_OK = 0;
52 public static final int AUTH_FAILED = 1;
53      
54     protected boolean isDirty = false;
55     protected String JavaDoc username = null;
56     private String JavaDoc password = null;
57     protected String JavaDoc address1 = null;
58     protected String JavaDoc address2 = null;
59     protected String JavaDoc city = null;
60     protected String JavaDoc state = null;
61     protected String JavaDoc zip = null;
62     protected String JavaDoc creditCard = null;
63     protected String JavaDoc email= null;
64
65     private static UserStore storage = null;
66
67     /**
68      * Create the correct user store.
69      */

70     public static void InitializeStorageOption(String JavaDoc opt,String JavaDoc fn) throws ApplicationException{
71   
72   
73         try {
74             Class JavaDoc c = null;
75             if (opt.equalsIgnoreCase("memory")){
76               c = Class.forName("golfShop.data.user.MemoryUserStore");
77               storage = (UserStore) c.newInstance();
78               storage.initializeUserStore();
79            }
80             else if (opt.equalsIgnoreCase("file"))
81            {
82             c = Class.forName("golfShop.data.user.FileUserStore");
83             storage = (UserStore) c.newInstance();
84             storage.initializeUserStore(fn);
85            }
86             else if (opt.equalsIgnoreCase("ldap"))
87            {
88                 c = Class.forName("golfShop.data.user.LdapUserStore");
89            }
90             else
91                 throw new ApplicationException("Invalid user store option " +
92                     opt);
93            
94         
95         } catch (ClassNotFoundException JavaDoc cnfe) {
96             throw new ApplicationException("Unable to create user store" +
97                 " of type: " + opt + "\n" + cnfe);
98         } catch (IllegalAccessException JavaDoc iae) {
99             throw new ApplicationException("Unable to create user store" +
100                 " of type: " + opt + "\n" + iae);
101         } catch (InstantiationException JavaDoc ie) {
102             throw new ApplicationException("Unable to create user store" +
103                 " of type: " + opt + "\n" + ie);
104         }
105          
106     }
107
108     /**
109      * Constructor. This is protected so that the outside world cannot
110      * create users. To obtain an already existing user, call the class
111      * method lookupUser(). To create a new user, call the class method
112      * createUser().
113      *
114      * @param username A unique String representing the user's identity.
115      * @param password A String containing the user's password.
116      * @param userid An int containing the user's unique id number.
117      */

118     protected UserDOImpl(String JavaDoc username, String JavaDoc password, String JavaDoc address1,
119                         String JavaDoc address2, String JavaDoc city, String JavaDoc state,
120                         String JavaDoc zip, String JavaDoc creditCard, String JavaDoc email) {
121         this.username = username;
122         this.password = password;
123         this.address1 = address1;
124         this.address2 = address2;
125         this.city = city;
126         this.state = state;
127         this.zip = zip;
128         this.creditCard = creditCard;
129         this.email = email;
130         this.isDirty = false;
131     }
132    
133
134     public static UserDO lookupUser(String JavaDoc username) {
135         return storage.lookupUserFromUserStore(username);
136     }
137         
138
139     /**
140      * This is the public interface for creating user data objects.
141      */

142     public static UserDO createUser(String JavaDoc username, String JavaDoc password,
143                         String JavaDoc address1, String JavaDoc address2, String JavaDoc city,
144                         String JavaDoc state, String JavaDoc zip, String JavaDoc creditCard,
145                         String JavaDoc email) {
146         // Make sure they don't already exist.
147
if (storage.usernameInUserStore(username))
148             return null;
149
150         // Create them.
151
UserDOImpl newGuy = new UserDOImpl (username, password, address1, address2,
152                                      city, state, zip, creditCard, email);
153         // Add the new user to the collection of all users.
154
storage.addUserToUserStore(newGuy);
155
156         // Return the new user.
157
return newGuy;
158     }
159
160
161     /**
162      * This is needed to fulfill the requirements of being a User object.
163      * It is not used in this demo.
164      * Changes a user's password.
165      * The old password must be supplied and it must match the
166      * password stored in the user database.
167      * This only changes the copy in memory, call commitChanges() to
168      * write the new password out to the storage medium.
169      */

170     public void changePassword(String JavaDoc old, String JavaDoc new1, String JavaDoc new2,
171                             User initiatingUser) throws LoginException {
172         if ((new1 != new2) || (authenticate(old) != AUTH_OK))
173             throw new LoginException(AUTH_FAILED);
174         password = new1;
175         isDirty = true;
176     }
177
178     /**
179      */

180     public String JavaDoc getName() {
181         return username;
182     }
183
184    
185     /**
186      * Process an authentication request. Verify
187      * that the user's account and password are valid.
188      *
189      * @param password The password entered by the user.
190      * @return True if the password matches, false otherwise.
191      */

192     public int authenticate(String JavaDoc password) {
193         if (this.password.equals(password))
194                   return AUTH_OK;
195         else
196                   return AUTH_FAILED;
197     }
198
199     protected String JavaDoc getPassword() {
200         return this.password;
201     }
202  
203
204     /**
205      * Write any changes to the storage medium. If no changes have been
206      * made, no action will be taken.
207      */

208     public void commitChanges() {
209         if (!isDirty)
210             return;
211         storage.updateUserInUserStore(this);
212         isDirty = false;
213     }
214
215
216     public String JavaDoc getAddress1() {
217         return address1;
218     }
219
220     public void setAddress1(String JavaDoc address1) {
221         if (address1.compareTo(this.address1) != 0) {
222             this.address1 = address1;
223             isDirty = true;
224         }
225     }
226
227     public String JavaDoc getAddress2() {
228         return address2;
229     }
230
231     public void setAddress2(String JavaDoc address2) {
232         if (address2.compareTo(this.address2) != 0) {
233             this.address2 = address2;
234             isDirty = true;
235         }
236     }
237
238     public String JavaDoc getCity() {
239         return city;
240     }
241
242     public void setCity(String JavaDoc city) {
243         if (city.compareTo(this.city) != 0) {
244             this.city = city;
245             isDirty = true;
246         }
247     }
248
249     public String JavaDoc getState() {
250         return state;
251     }
252
253     public void setState(String JavaDoc state) {
254         if (state.compareTo(this.state) != 0) {
255             this.state = state;
256             isDirty = true;
257         }
258     }
259
260     public String JavaDoc getZip() {
261         return zip;
262     }
263
264     public void setZip(String JavaDoc zip) {
265         if (zip.compareTo(this.zip) != 0) {
266             this.zip = zip;
267             isDirty = true;
268         }
269     }
270
271     public String JavaDoc getCreditCard() {
272         return creditCard;
273     }
274
275     public void setCreditCard(String JavaDoc creditCard) {
276         if (creditCard.compareTo(this.creditCard) != 0) {
277             this.creditCard = creditCard;
278             isDirty = true;
279         }
280     }
281
282     public String JavaDoc getEmail() {
283         return email;
284     }
285
286     public void setEmail(String JavaDoc email) {
287         if (email.compareTo(this.email) != 0) {
288             this.email = email;
289             isDirty = true;
290         }
291     }
292
293     /**
294      * Compare this object to another user object. They are
295      * equal if the user name is the same.
296      */

297     public boolean equals(Object JavaDoc obj) {
298         return (obj instanceof UserDO)
299             && (((UserDO)obj).getName().equals(getName()));
300     }
301
302     /**
303      * Convert to string for debugging.
304      */

305     public String JavaDoc toString() {
306         return "isDirty = \"" + isDirty + "\", "
307             + "username = \"" + username + "\", "
308             + "password = \"" + password + "\", "
309             + "address1 = \"" + address1 + "\", "
310             + "address2 = \"" + address2 + "\", "
311             + "city = \"" + city + "\", "
312             + "state = \"" + state + "\", "
313             + "zip = \"" + zip + "\", "
314             + "creditCard = \"" + creditCard + "\", "
315             + "email = \"" + email + "\", ";
316     }
317 }
318  
319
Popular Tags