KickJava   Java API By Example, From Geeks To Geeks.

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


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.user.User;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.FilenameFilter JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Properties JavaDoc;
41
42 /**
43  * Storage backend for User data. Users
44  * are stored in properties files.
45  *
46  * @author Stephan J. Schmidt
47  * @version $Id: PropertyFileUserStorage.java 1257 2003-12-11 13:36:55Z leo $
48  */

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

59   protected String JavaDoc getFileName(String JavaDoc login) {
60     return login + ".properties";
61   }
62
63   /**
64    * Store the user to the given directory
65    *
66    * @param user User to store
67    * @param out OutputStream where the user is stored
68    */

69   protected void storeUser(User user, OutputStream JavaDoc out) throws IOException JavaDoc {
70     Properties JavaDoc props = new Properties JavaDoc();
71     props.putAll(UserSerializer.getInstance().createUserMap(user));
72     props.store(out, "");
73   }
74
75   /**
76    * Return a list of all users from
77    * the file system
78    *
79    * @return
80    */

81   public List JavaDoc storageAll() {
82     List JavaDoc users = new ArrayList JavaDoc();
83     File JavaDoc userDir = getWorkingDir();
84
85     String JavaDoc[] files = userDir.list(new FilenameFilter JavaDoc() {
86       public boolean accept(File JavaDoc dir, String JavaDoc name) {
87         return name.endsWith(".properties");
88       }
89     });
90
91 // System.out.println("User files="+Arrays.asList(files));
92
for (int i = 0; i < files.length; i++) {
93       String JavaDoc fileName = files[i];
94       String JavaDoc login = fileName.substring(0, fileName.lastIndexOf("."));
95       try {
96         User user = parseUser(loadUser(login, new FileInputStream JavaDoc(new File JavaDoc(userDir, fileName))));
97         users.add(user);
98       } catch (Exception JavaDoc e) {
99         Logger.log("PropertyFileUserStorage: cannot load user "+fileName, e);
100       }
101     }
102     return users;
103   }
104
105   /**
106    * Load the user data to a map from the given
107    * directory
108    *
109    * @param login Login of the user to load
110    * @param in Input stream from which to load the data
111    * @return
112    */

113   protected Map JavaDoc loadUser(String JavaDoc login, InputStream JavaDoc in) throws IOException JavaDoc {
114     Properties JavaDoc userData = new Properties JavaDoc();
115     userData.load(in);
116     return userData;
117   }
118 }
119
Popular Tags