1 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 ; 33 import java.io.FileInputStream ; 34 import java.io.FileOutputStream ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.OutputStream ; 38 import java.sql.Timestamp ; 39 import java.util.List ; 40 import java.util.Map ; 41 42 49 50 public abstract class FileUserStorage implements UserStorage { 51 58 protected abstract String getFileName(String login); 59 60 66 protected abstract void storeUser(User user, OutputStream out) throws IOException ; 67 68 76 protected abstract Map loadUser(String login, InputStream in) throws IOException ; 77 78 83 public File getWorkingDir() { 84 Application app = Application.get(); 85 return new File (new File (app.getConfiguration().getFileStore()), "users"); 86 } 87 88 94 public File getWorkingDir(String applicationOid) { 95 Application app = Application.get(); 96 return app.getConfiguration().getUserPath(applicationOid); 97 } 98 99 104 public void storageStore(User user) { 105 File userDir = getWorkingDir(); 106 107 if (!userDir.exists()) { 108 userDir.mkdirs(); 109 } 110 111 File userFile = new File (userDir, getFileName(user.getLogin())); 112 if (userFile.exists()) { 113 Logger.log("FileUserStorage: backing up " + userFile.getPath()); 114 File backup = new File (userFile.getPath() + ".bck"); 115 userFile.renameTo(backup); 116 } 117 118 try { 119 storeUser(user, new FileOutputStream (userFile)); 120 } catch (IOException e) { 121 Logger.log("FileUserStorage: Cannot store user "+user.getLogin(), e); 122 } 123 } 124 125 133 public User storageCreate(String login, String passwd, String email) { 134 File userDir = getWorkingDir(); 135 136 if (!userDir.exists()) { 137 userDir.mkdirs(); 138 } 139 File userFile = new File (userDir, getFileName(login)); 140 141 User user = new User(login, passwd, email); 142 String applicationOid = (String ) Application.get().getObject(Application.OID); 143 Timestamp cTime = new Timestamp (new java.util.Date ().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 (userFile)); 152 } catch (Exception e) { 153 Logger.log("FileUserStorage: Cannot create user "+login, e); 154 } 155 return user; 156 } 157 158 163 public void storageRemove(User user) { 164 removeUser(user, getWorkingDir()); 165 } 166 167 173 protected void removeUser(User user, File userDir) { 174 File userFile = new File (userDir, getFileName(user.getLogin())); 175 if (userFile.exists()) { 176 File backup = new File (userFile.getPath() + ".removed"); 177 userFile.renameTo(backup); 178 } 179 }; 180 181 187 public int storageUserCount() { 188 return storageAll().size(); 189 } 190 191 198 public User storageLoad(String login) { 199 File userFile = new File (getWorkingDir(), getFileName(login)); 200 201 Map map = null; 202 try { 203 map = loadUser(login, new FileInputStream (userFile)); 204 } catch (Exception 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 222 public List storageAll() { 223 return null; 225 } 226 227 protected User parseUser(Map map) { 228 return UserSerializer.getInstance().deserialize(map, new User()); 229 } 230 } 231 | Popular Tags |