1 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 ; 32 import java.io.FileInputStream ; 33 import java.io.FilenameFilter ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.OutputStream ; 37 import java.util.ArrayList ; 38 import java.util.List ; 39 import java.util.Map ; 40 import java.util.Properties ; 41 42 49 50 public class PropertyFileUserStorage extends FileUserStorage { 51 52 59 protected String getFileName(String login) { 60 return login + ".properties"; 61 } 62 63 69 protected void storeUser(User user, OutputStream out) throws IOException { 70 Properties props = new Properties (); 71 props.putAll(UserSerializer.getInstance().createUserMap(user)); 72 props.store(out, ""); 73 } 74 75 81 public List storageAll() { 82 List users = new ArrayList (); 83 File userDir = getWorkingDir(); 84 85 String [] files = userDir.list(new FilenameFilter () { 86 public boolean accept(File dir, String name) { 87 return name.endsWith(".properties"); 88 } 89 }); 90 91 for (int i = 0; i < files.length; i++) { 93 String fileName = files[i]; 94 String login = fileName.substring(0, fileName.lastIndexOf(".")); 95 try { 96 User user = parseUser(loadUser(login, new FileInputStream (new File (userDir, fileName)))); 97 users.add(user); 98 } catch (Exception e) { 99 Logger.log("PropertyFileUserStorage: cannot load user "+fileName, e); 100 } 101 } 102 return users; 103 } 104 105 113 protected Map loadUser(String login, InputStream in) throws IOException { 114 Properties userData = new Properties (); 115 userData.load(in); 116 return userData; 117 } 118 } 119 | Popular Tags |