KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
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 /**
35  * This is one flavor of a UserStore. The storage medium in this case
36  * is a text file. To save the users, use serialization to write the whole
37  * vector of them to the file. To read users, un-serialize from the file.
38  * All other operations occur on the copy in memory.
39  *
40  * @author Andrew John
41  * @version $Revision: 1.1 $
42  */

43 public class FileUserStore extends UserStore {
44     
45     /*
46      * This is the storage medium. Just keep a set of users in memory.
47      */

48     private Vector allUsers = new Vector();
49     private String JavaDoc fileName="";
50     /**
51      * Initialize the set of users.
52      */

53       protected void initializeUserStore() {}
54      
55     protected void initializeUserStore(String JavaDoc fn) {
56         fileName=fn;
57         readAllUsersFromFile(fn);
58     }
59
60
61     /**
62      * Just search the copy of the objects in memory.
63      */

64     protected boolean usernameInUserStore(String JavaDoc 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     /**
76      * We already read all the users. Just lookup from memory.
77      */

78     protected UserDOImpl lookupUserFromUserStore(String JavaDoc 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     /**
90      * Add to memory, then write out the whole set of users to the file.
91      */

92     protected void addUserToUserStore(UserDOImpl user) {
93         allUsers.addElement(user);
94         writeAllUsersToFile();
95     }
96
97     /**
98      * Even though only the one user has changed, we must write out
99      * the whole vector of all users.
100      */

101     protected void updateUserInUserStore(UserDOImpl user) {
102         writeAllUsersToFile();
103     }
104
105     /////////////////////////////////////////////////////////////////
106

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 JavaDoc 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 JavaDoc 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 JavaDoc 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