001: /*
002: * Enhydra Java Application Server
003: * The Initial Developer of the Original Code is Lutris Technologies Inc.
004: * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
005: * Inc.
006: * All Rights Reserved.
007: *
008: * The contents of this file are subject to the Enhydra Public License Version
009: * 1.0 (the "License"); you may not use this file except in compliance with the
010: * License. You may obtain a copy of the License at
011: * http://www.enhydra.org/software/license/epl.html
012: *
013: * Software distributed under the License is distributed on an "AS IS" basis,
014: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
015: * License for the specific language governing rights and limitations under the
016: * License.
017: *
018: *
019: */
020:
021: package golfShop.data.user;
022:
023: import java.io.Serializable;
024: import com.lutris.util.ConfigException;
025: import com.lutris.appserver.server.*;
026: import golfShop.data.user.*;
027: import golfShop.spec.user.UserDO;
028: import com.lutris.appserver.server.user.*;
029: import golfShop.spec.LoginException;
030:
031: /**
032: * The application specific user data object. This is the base object that
033: * does most of the work. All the policy decisions happen here.
034: * The abstract functions are for accessing the store of all users.
035: * There will be at least 3 sub-classes that implement these functions,
036: * one that just keeps the list in memory, one that reads and writes to a
037: * text file, and one that acesses an ldap database. These abstract functions
038: * are private, so that all 3 flavors appear the same to the outside world.
039: * This is a data object, not a business object. This object hides the
040: * storage medium (memory, file, ldap etc..). The class methods get users
041: * from the storage medium, and create new users. The object methods give
042: * you access to the user data, and update changes to the storage medium.
043: * Passwords are protected, but other than that no policy is enforeced.
044: * For example, cheching password length, making sure the correct password
045: * was enntered etc.. is the responsiblity of the user buisness object.
046: *
047: * @author Andrew John
048: * @version $Revision: 1.1 $
049: */
050: public class UserDOImpl implements UserDO, User, Serializable {
051:
052: public static final int AUTH_OK = 0;
053: public static final int AUTH_FAILED = 1;
054:
055: protected boolean isDirty = false;
056: protected String username = null;
057: private String password = null;
058: protected String address1 = null;
059: protected String address2 = null;
060: protected String city = null;
061: protected String state = null;
062: protected String zip = null;
063: protected String creditCard = null;
064: protected String email = null;
065:
066: private static UserStore storage = null;
067:
068: /**
069: * Create the correct user store.
070: */
071: public static void InitializeStorageOption(String opt, String fn)
072: throws ApplicationException {
073:
074: try {
075: Class c = null;
076: if (opt.equalsIgnoreCase("memory")) {
077: c = Class.forName("golfShop.data.user.MemoryUserStore");
078: storage = (UserStore) c.newInstance();
079: storage.initializeUserStore();
080: } else if (opt.equalsIgnoreCase("file")) {
081: c = Class.forName("golfShop.data.user.FileUserStore");
082: storage = (UserStore) c.newInstance();
083: storage.initializeUserStore(fn);
084: } else if (opt.equalsIgnoreCase("ldap")) {
085: c = Class.forName("golfShop.data.user.LdapUserStore");
086: } else
087: throw new ApplicationException(
088: "Invalid user store option " + opt);
089:
090: } catch (ClassNotFoundException cnfe) {
091: throw new ApplicationException(
092: "Unable to create user store" + " of type: " + opt
093: + "\n" + cnfe);
094: } catch (IllegalAccessException iae) {
095: throw new ApplicationException(
096: "Unable to create user store" + " of type: " + opt
097: + "\n" + iae);
098: } catch (InstantiationException ie) {
099: throw new ApplicationException(
100: "Unable to create user store" + " of type: " + opt
101: + "\n" + ie);
102: }
103:
104: }
105:
106: /**
107: * Constructor. This is protected so that the outside world cannot
108: * create users. To obtain an already existing user, call the class
109: * method lookupUser(). To create a new user, call the class method
110: * createUser().
111: *
112: * @param username A unique String representing the user's identity.
113: * @param password A String containing the user's password.
114: * @param userid An int containing the user's unique id number.
115: */
116: protected UserDOImpl(String username, String password,
117: String address1, String address2, String city,
118: String state, String zip, String creditCard, String email) {
119: this .username = username;
120: this .password = password;
121: this .address1 = address1;
122: this .address2 = address2;
123: this .city = city;
124: this .state = state;
125: this .zip = zip;
126: this .creditCard = creditCard;
127: this .email = email;
128: this .isDirty = false;
129: }
130:
131: public static UserDO lookupUser(String username) {
132: return storage.lookupUserFromUserStore(username);
133: }
134:
135: /**
136: * This is the public interface for creating user data objects.
137: */
138: public static UserDO createUser(String username, String password,
139: String address1, String address2, String city,
140: String state, String zip, String creditCard, String email) {
141: // Make sure they don't already exist.
142: if (storage.usernameInUserStore(username))
143: return null;
144:
145: // Create them.
146: UserDOImpl newGuy = new UserDOImpl(username, password,
147: address1, address2, city, state, zip, creditCard, email);
148: // Add the new user to the collection of all users.
149: storage.addUserToUserStore(newGuy);
150:
151: // Return the new user.
152: return newGuy;
153: }
154:
155: /**
156: * This is needed to fulfill the requirements of being a User object.
157: * It is not used in this demo.
158: * Changes a user's password.
159: * The old password must be supplied and it must match the
160: * password stored in the user database.
161: * This only changes the copy in memory, call commitChanges() to
162: * write the new password out to the storage medium.
163: */
164: public void changePassword(String old, String new1, String new2,
165: User initiatingUser) throws LoginException {
166: if ((new1 != new2) || (authenticate(old) != AUTH_OK))
167: throw new LoginException(AUTH_FAILED);
168: password = new1;
169: isDirty = true;
170: }
171:
172: /**
173: */
174: public String getName() {
175: return username;
176: }
177:
178: /**
179: * Process an authentication request. Verify
180: * that the user's account and password are valid.
181: *
182: * @param password The password entered by the user.
183: * @return True if the password matches, false otherwise.
184: */
185: public int authenticate(String password) {
186: if (this .password.equals(password))
187: return AUTH_OK;
188: else
189: return AUTH_FAILED;
190: }
191:
192: protected String getPassword() {
193: return this .password;
194: }
195:
196: /**
197: * Write any changes to the storage medium. If no changes have been
198: * made, no action will be taken.
199: */
200: public void commitChanges() {
201: if (!isDirty)
202: return;
203: storage.updateUserInUserStore(this );
204: isDirty = false;
205: }
206:
207: public String getAddress1() {
208: return address1;
209: }
210:
211: public void setAddress1(String address1) {
212: if (address1.compareTo(this .address1) != 0) {
213: this .address1 = address1;
214: isDirty = true;
215: }
216: }
217:
218: public String getAddress2() {
219: return address2;
220: }
221:
222: public void setAddress2(String address2) {
223: if (address2.compareTo(this .address2) != 0) {
224: this .address2 = address2;
225: isDirty = true;
226: }
227: }
228:
229: public String getCity() {
230: return city;
231: }
232:
233: public void setCity(String city) {
234: if (city.compareTo(this .city) != 0) {
235: this .city = city;
236: isDirty = true;
237: }
238: }
239:
240: public String getState() {
241: return state;
242: }
243:
244: public void setState(String state) {
245: if (state.compareTo(this .state) != 0) {
246: this .state = state;
247: isDirty = true;
248: }
249: }
250:
251: public String getZip() {
252: return zip;
253: }
254:
255: public void setZip(String zip) {
256: if (zip.compareTo(this .zip) != 0) {
257: this .zip = zip;
258: isDirty = true;
259: }
260: }
261:
262: public String getCreditCard() {
263: return creditCard;
264: }
265:
266: public void setCreditCard(String creditCard) {
267: if (creditCard.compareTo(this .creditCard) != 0) {
268: this .creditCard = creditCard;
269: isDirty = true;
270: }
271: }
272:
273: public String getEmail() {
274: return email;
275: }
276:
277: public void setEmail(String email) {
278: if (email.compareTo(this .email) != 0) {
279: this .email = email;
280: isDirty = true;
281: }
282: }
283:
284: /**
285: * Compare this object to another user object. They are
286: * equal if the user name is the same.
287: */
288: public boolean equals(Object obj) {
289: return (obj instanceof UserDO)
290: && (((UserDO) obj).getName().equals(getName()));
291: }
292:
293: /**
294: * Convert to string for debugging.
295: */
296: public String toString() {
297: return "isDirty = \"" + isDirty + "\", " + "username = \""
298: + username + "\", " + "password = \"" + password
299: + "\", " + "address1 = \"" + address1 + "\", "
300: + "address2 = \"" + address2 + "\", " + "city = \""
301: + city + "\", " + "state = \"" + state + "\", "
302: + "zip = \"" + zip + "\", " + "creditCard = \""
303: + creditCard + "\", " + "email = \"" + email + "\", ";
304: }
305: }
|