KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > storage > FileUserStorage


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip.storage;
27
28 import org.radeox.util.logging.Logger;
29 import org.snipsnap.app.Application;
30 import org.snipsnap.user.User;
31
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.OutputStream JavaDoc;
38 import java.sql.Timestamp JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41
42 /**
43  * Storage backend for User data. Users
44  * are stored in files.
45  *
46  * @author Stephan J. Schmidt
47  * @version $Id: FileUserStorage.java 1257 2003-12-11 13:36:55Z leo $
48  */

49
50 public abstract class FileUserStorage implements UserStorage {
51   /**
52    * Return name of the file to store
53    * user into
54    *
55    * @param login User to get file name for
56    * @return
57    */

58   protected abstract String JavaDoc getFileName(String JavaDoc login);
59
60   /**
61    * Store the user to the given directory
62    *
63    * @param user User to store
64    * @param out Output stream where the user is stored
65    */

66   protected abstract void storeUser(User user, OutputStream JavaDoc out) throws IOException JavaDoc;
67
68   /**
69    * Load the user data to a map from the given
70    * directory
71    *
72    * @param login Login of the user to load
73    * @param in Input stream from which the user is loaded
74    * @return
75    */

76   protected abstract Map JavaDoc loadUser(String JavaDoc login, InputStream JavaDoc in) throws IOException JavaDoc;
77
78   /**
79    * Return the base directory where all users are stored
80    *
81    * @return
82    */

83   public File JavaDoc getWorkingDir() {
84     Application app = Application.get();
85     return new File JavaDoc(new File JavaDoc(app.getConfiguration().getFileStore()), "users");
86   }
87
88   /**
89    * Return the base directory where all users are stored
90    *
91    * @param applicationOid
92    * @return
93    */

94   public File JavaDoc getWorkingDir(String JavaDoc applicationOid) {
95     Application app = Application.get();
96     return app.getConfiguration().getUserPath(applicationOid);
97   }
98
99   /**
100    * Store an user in to the backend
101    *
102    * @param user User to store
103    */

104   public void storageStore(User user) {
105     File JavaDoc userDir = getWorkingDir();
106
107     if (!userDir.exists()) {
108       userDir.mkdirs();
109     }
110
111     File JavaDoc userFile = new File JavaDoc(userDir, getFileName(user.getLogin()));
112     if (userFile.exists()) {
113       Logger.log("FileUserStorage: backing up " + userFile.getPath());
114       File JavaDoc backup = new File JavaDoc(userFile.getPath() + ".bck");
115       userFile.renameTo(backup);
116     }
117
118     try {
119       storeUser(user, new FileOutputStream JavaDoc(userFile));
120     } catch (IOException JavaDoc e) {
121       Logger.log("FileUserStorage: Cannot store user "+user.getLogin(), e);
122     }
123   }
124
125   /**
126    * Create a new user in the backend
127    *
128    * @param login Login name of the user
129    * @param passwd Credential of the user
130    * @param email Email adress of the user
131    * @return
132    */

133   public User storageCreate(String JavaDoc login, String JavaDoc passwd, String JavaDoc email) {
134     File JavaDoc userDir = getWorkingDir();
135
136     if (!userDir.exists()) {
137       userDir.mkdirs();
138     }
139     File JavaDoc userFile = new File JavaDoc(userDir, getFileName(login));
140
141     User user = new User(login, passwd, email);
142     String JavaDoc applicationOid = (String JavaDoc) Application.get().getObject(Application.OID);
143     Timestamp JavaDoc cTime = new Timestamp JavaDoc(new java.util.Date JavaDoc().getTime());
144     user.setCTime(cTime);
145     user.setMTime(cTime);
146     user.setLastLogin(cTime);
147     user.setLastAccess(cTime);
148     user.setLastLogout(cTime);
149     user.setApplication(applicationOid);
150     try {
151       storeUser(user, new FileOutputStream JavaDoc(userFile));
152     } catch (Exception JavaDoc e) {
153       Logger.log("FileUserStorage: Cannot create user "+login, e);
154     }
155     return user;
156   }
157
158   /**
159    * Remove an user from the backend
160    *
161    * @param user User to remove
162    */

163   public void storageRemove(User user) {
164     removeUser(user, getWorkingDir());
165   }
166
167   /**
168    * Remove user files from directory
169    *
170    * @param user User to remove
171    * @param userDir Directory which contains the user data
172    */

173   protected void removeUser(User user, File JavaDoc userDir) {
174     File JavaDoc userFile = new File JavaDoc(userDir, getFileName(user.getLogin()));
175     if (userFile.exists()) {
176       File JavaDoc backup = new File JavaDoc(userFile.getPath() + ".removed");
177       userFile.renameTo(backup);
178     }
179   };
180
181   /**
182    * Return the number of users in the backend which
183    * are stored in the file system
184    *
185    * @return
186    */

187   public int storageUserCount() {
188     return storageAll().size();
189   }
190
191   /**
192    * Load a user from the backend. Users are
193    * stored in the file system.
194    *
195    * @param login Login of the user to load
196    * @return
197    */

198  public User storageLoad(String JavaDoc login) {
199     File JavaDoc userFile = new File JavaDoc(getWorkingDir(), getFileName(login));
200
201     Map JavaDoc map = null;
202     try {
203       map = loadUser(login, new FileInputStream JavaDoc(userFile));
204       // create user from map
205
} catch (Exception JavaDoc e) {
206       Logger.log("FileUserStorage: Cannot load user "+login, e);
207     }
208
209     if (null == map) {
210       return null;
211     } else {
212       return parseUser(map);
213     }
214   }
215
216   /**
217    * Return a list of all users from
218    * the file system
219    *
220    * @return
221    */

222   public List JavaDoc storageAll() {
223     // load all users from directory
224
return null;
225   }
226
227   protected User parseUser(Map JavaDoc map) {
228     return UserSerializer.getInstance().deserialize(map, new User());
229   }
230 }
231
Popular Tags