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.*;
024: import java.util.*;
025: import com.lutris.util.Config;
026: import com.lutris.util.ConfigException;
027: import com.lutris.logging.Logger;
028: import com.lutris.logging.LogChannel;
029: import com.lutris.appserver.server.Enhydra;
030: import golfShop.data.user.UserDOImpl;
031: import golfShop.data.user.UserStore;
032:
033: /**
034: * This is one flavor of a UserStore. The storage medium in this case
035: * is a text file. To save the users, use serialization to write the whole
036: * vector of them to the file. To read users, un-serialize from the file.
037: * All other operations occur on the copy in memory.
038: *
039: * @author Andrew John
040: * @version $Revision: 1.1 $
041: */
042: public class FileUserStore extends UserStore {
043:
044: /*
045: * This is the storage medium. Just keep a set of users in memory.
046: */
047: private Vector allUsers = new Vector();
048: private String fileName = "";
049:
050: /**
051: * Initialize the set of users.
052: */
053: protected void initializeUserStore() {
054: }
055:
056: protected void initializeUserStore(String fn) {
057: fileName = fn;
058: readAllUsersFromFile(fn);
059: }
060:
061: /**
062: * Just search the copy of the objects in memory.
063: */
064: protected boolean usernameInUserStore(String username) {
065: Enumeration e = allUsers.elements();
066: while (e.hasMoreElements()) {
067: UserDOImpl u = (UserDOImpl) e.nextElement();
068: if (username.equals(u.username))
069: return true;
070: }
071: return false;
072: }
073:
074: /**
075: * We already read all the users. Just lookup from memory.
076: */
077: protected UserDOImpl lookupUserFromUserStore(String username) {
078: Enumeration e = allUsers.elements();
079: while (e.hasMoreElements()) {
080: UserDOImpl u = (UserDOImpl) e.nextElement();
081: if (username.equals(u.username))
082: return u;
083: }
084: return null;
085: }
086:
087: /**
088: * Add to memory, then write out the whole set of users to the file.
089: */
090: protected void addUserToUserStore(UserDOImpl user) {
091: allUsers.addElement(user);
092: writeAllUsersToFile();
093: }
094:
095: /**
096: * Even though only the one user has changed, we must write out
097: * the whole vector of all users.
098: */
099: protected void updateUserInUserStore(UserDOImpl user) {
100: writeAllUsersToFile();
101: }
102:
103: /////////////////////////////////////////////////////////////////
104:
105: private void writeAllUsersToFile() {
106:
107: try {
108: FileOutputStream ostream = new FileOutputStream(fileName);
109: ObjectOutputStream p = new ObjectOutputStream(ostream);
110: p.writeObject(allUsers);
111: p.flush();
112: ostream.close();
113: } catch (Exception e) {
114: LogChannel chan = Enhydra.getLogChannel();
115: if (chan != null)
116: chan.write(Logger.ERROR,
117: "Error writing user list to file " + fileName
118: + ".", e);
119: }
120: }
121:
122: private void readAllUsersFromFile(String fn) {
123:
124: try {
125:
126: FileInputStream istream = new FileInputStream(fn);
127:
128: ObjectInputStream p = new ObjectInputStream(istream);
129:
130: allUsers = (Vector) p.readObject();
131: istream.close();
132: } catch (Exception e) {
133: LogChannel chan = Enhydra.getLogChannel();
134: if (chan != null)
135: chan
136: .write(
137: Logger.WARNING,
138: "Creating a new user list with initial accout username \"enhydra\", password \"lutris\"");
139: allUsers = new Vector();
140: UserDOImpl u = new UserDOImpl("enhydra", "lutris", "", "",
141: "", "", "", "", "");
142: allUsers.addElement(u);
143:
144: }
145: }
146:
147: }
|