1 20 21 package golfShop.data.user; 22 23 import java.io.*; 24 import java.util.*; 25 import com.lutris.util.Config; 26 import com.lutris.util.ConfigException; 27 import com.lutris.logging.Logger; 28 import com.lutris.logging.LogChannel; 29 import com.lutris.appserver.server.Enhydra; 30 import golfShop.data.user.UserDOImpl; 31 import golfShop.data.user.UserStore; 32 33 34 43 public class FileUserStore extends UserStore { 44 45 48 private Vector allUsers = new Vector(); 49 private String fileName=""; 50 53 protected void initializeUserStore() {} 54 55 protected void initializeUserStore(String fn) { 56 fileName=fn; 57 readAllUsersFromFile(fn); 58 } 59 60 61 64 protected boolean usernameInUserStore(String username) { 65 Enumeration e = allUsers.elements(); 66 while (e.hasMoreElements()) { 67 UserDOImpl u = (UserDOImpl) e.nextElement(); 68 if (username.equals(u.username)) 69 return true; 70 } 71 return false; 72 } 73 74 75 78 protected UserDOImpl lookupUserFromUserStore(String username) { 79 Enumeration e = allUsers.elements(); 80 while (e.hasMoreElements()) { 81 UserDOImpl u = (UserDOImpl) e.nextElement(); 82 if (username.equals(u.username)) 83 return u; 84 } 85 return null; 86 } 87 88 89 92 protected void addUserToUserStore(UserDOImpl user) { 93 allUsers.addElement(user); 94 writeAllUsersToFile(); 95 } 96 97 101 protected void updateUserInUserStore(UserDOImpl user) { 102 writeAllUsersToFile(); 103 } 104 105 107 private void writeAllUsersToFile() { 108 109 try { 110 FileOutputStream ostream = new FileOutputStream(fileName); 111 ObjectOutputStream p = new ObjectOutputStream(ostream); 112 p.writeObject(allUsers); 113 p.flush(); 114 ostream.close(); 115 } catch (Exception e) { 116 LogChannel chan = Enhydra.getLogChannel(); 117 if (chan != null) 118 chan.write(Logger.ERROR, 119 "Error writing user list to file " + 120 fileName + ".", e); 121 } 122 } 123 124 private void readAllUsersFromFile(String fn) { 125 126 try { 127 128 FileInputStream istream = new FileInputStream(fn); 129 130 ObjectInputStream p = new ObjectInputStream(istream); 131 132 allUsers = (Vector)p.readObject(); 133 istream.close(); 134 } catch (Exception e) { 135 LogChannel chan = Enhydra.getLogChannel(); 136 if (chan != null) 137 chan.write(Logger.WARNING, "Creating a new user list with initial accout username \"enhydra\", password \"lutris\""); 138 allUsers = new Vector(); 139 UserDOImpl u = new UserDOImpl("enhydra", "lutris", "", "", "", "", "", "" ,""); 140 allUsers.addElement(u); 141 142 } 143 } 144 145 } 146 | Popular Tags |