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.interceptor.Aspects; 31 import org.snipsnap.snip.Links; 32 import org.snipsnap.snip.Snip; 33 import org.snipsnap.snip.SnipFactory; 34 import org.snipsnap.snip.attachment.Attachments; 35 import org.snipsnap.snip.label.Labels; 36 import org.snipsnap.user.Permissions; 37 import org.snipsnap.util.ApplicationAwareMap; 38 import org.snipsnap.versioning.VersionInfo; 39 import org.snipsnap.versioning.VersionStorage; 40 41 import java.io.File ; 42 import java.io.IOException ; 43 import java.io.OutputStream ; 44 import java.io.InputStream ; 45 import java.sql.Timestamp ; 46 import java.util.*; 47 48 59 60 public abstract class FileSnipStorage implements CacheableStorage, VersionStorage, SnipStorage { 61 public static final String NOT_SUPPORTED_EXCEPTION_MSG = 62 "Method not supported, do not call FileSnipStorage directly"; 63 64 protected ApplicationAwareMap cache; 65 66 private SnipSerializer serializer = SnipSerializer.getInstance(); 67 68 public void setCache(ApplicationAwareMap cache) { 70 this.cache = cache; 71 } 72 73 78 public File getWorkingDir() { 79 Application app = Application.get(); 80 return new File (app.getConfiguration().getFileStore(), "snips"); 81 } 82 83 89 public File getWorkingDir(String applicationOid) { 90 Application app = Application.get(); 91 return app.getConfiguration().getFilePath(applicationOid); 92 } 93 94 public void storageRemove(Snip snip) { 95 File snipDir = new File (getWorkingDir(), snip.getName()); 96 storageRemoveFile(snip, snipDir); 97 } 98 99 106 protected abstract void storageRemoveFile(Snip snip, File snipDir); 107 108 protected abstract Map createSnipFromFile(File snipDir) throws IOException ; 109 110 118 public Snip storageLoad(String name) { 119 Snip snip = null; 120 File snipDir = new File (getWorkingDir(), name); 121 try { 122 snip = parseSnip(createSnipFromFile(snipDir)); 123 } catch (IOException e) { 124 Logger.log("unable to load snip", e); 125 } 126 return snip; 127 } 128 129 131 138 protected abstract void storeVersion(Snip snip, File versionDir); 139 140 141 149 public void storeVersion(Snip snip) { 150 File snipDir = new File (getWorkingDir(), snip.getName()); 151 File versionDir = new File (snipDir, "version"); 152 storeVersion(snip, versionDir); 153 } 154 155 163 public List getVersionHistory(Snip snip) { 164 File snipDir = new File (getWorkingDir(), snip.getName()); 165 File versionDir = new File (snipDir, "version"); 166 List versions = getVersionHistory(snip, versionDir); 167 return versions; 168 } 169 170 177 protected abstract VersionFileNameChecker getVersionFileNameChecker(); 178 179 187 188 protected List getVersionHistory(Snip snip, File versionDir) { 189 VersionFileNameChecker checker = getVersionFileNameChecker(); 190 191 String [] files = versionDir.list(checker); 192 193 List versions = new ArrayList(); 195 try { 196 for (int i = 0; i < files.length; i++) { 197 String fileName = files[i]; 198 int version = checker.getVersion(fileName); 199 Map map = loadVersion(snip, versionDir, version); 200 VersionInfo info = new VersionInfo(); 201 info.setVersion(version); 202 info.setViewCount(Integer.parseInt((String ) map.get(SnipSerializer.SNIP_VIEWCOUNT))); 203 info.setMUser((String ) map.get(SnipSerializer.SNIP_MUSER)); 204 info.setMTime(new Timestamp (Long.parseLong((String ) map.get(SnipSerializer.SNIP_MTIME)))); 205 info.setSize(((String ) map.get(SnipSerializer.SNIP_CONTENT)).length()); 206 versions.add(info); 207 } 208 } catch (Exception e) { 209 Logger.log("TwoFileSnipStorage: unable read version history of snip" + snip.getName(), e); 210 } 211 Collections.sort(versions, new Comparator() { 212 public int compare(Object o1, Object o2) { 213 return ((VersionInfo) o1).getVersion() > ((VersionInfo) o2).getVersion() ? -1 : 1; 214 } 215 }); 216 217 return versions; 218 } 219 220 228 protected abstract Map loadVersion(Snip snip, File versionDir, int version) throws IOException ; 229 230 237 public Snip loadVersion(Snip snip, int version) { 238 File snipDir = new File (getWorkingDir(), snip.getName()); 239 File versionDir = new File (snipDir, "version"); 240 String name = snip.getName(); 241 try { 242 Snip newSnip = SnipFactory.createSnip(name, ""); 243 return serializer.deserialize(loadVersion(snip, versionDir, version), newSnip); 244 } catch (IOException e) { 245 Logger.log("FileSnipStorage: Unable to load version snip " + snip.getName() + " " + version); 246 } 247 return null; 248 } 249 250 257 protected abstract void storeSnip(Snip snip, File snipDir); 258 259 266 public void storageStore(Snip snip) { 267 File snipDir = new File (getWorkingDir(), snip.getName()); 268 storeSnip(snip, snipDir); 269 } 270 271 public Snip storageCreate(String name, String content) { 272 Application app = Application.get(); 273 String applicationOid = (String ) app.getObject(Application.OID); 274 String login = app.getUser().getLogin(); 275 276 Snip snip = SnipFactory.createSnip(name, content); 277 Timestamp cTime = new Timestamp (new java.util.Date ().getTime()); 278 Timestamp mTime = (Timestamp ) cTime.clone(); 279 snip.setCTime(cTime); 280 snip.setMTime(mTime); 281 snip.setCUser(login); 282 snip.setMUser(login); 283 snip.setOUser(login); 284 snip.setPermissions(new Permissions()); 285 snip.setBackLinks(new Links()); 286 snip.setSnipLinks(new Links()); 287 snip.setLabels(new Labels()); 288 snip.setAttachments(new Attachments()); 289 snip.setApplication(applicationOid); 290 storageStore(snip); 291 return (Snip) Aspects.newInstance(snip, Snip.class); 292 } 293 294 private Snip parseSnip(Map snipMap) { 295 String applicationOid = (String ) snipMap.get(SnipSerializer.SNIP_APPLICATION); 297 String name = (String ) snipMap.get(SnipSerializer.SNIP_NAME); 298 if (cache.getMap(applicationOid).containsKey(name.toUpperCase())) { 299 return (Snip) cache.getMap(applicationOid).get(name.toUpperCase()); 300 } 301 302 Snip newSnip = SnipFactory.createSnip(name, (String ) snipMap.get(SnipSerializer.SNIP_CONTENT)); 303 Snip snip = serializer.deserialize(snipMap, newSnip); 304 305 snip = (Snip) Aspects.newInstance(snip, Snip.class); 308 cache.getMap(applicationOid).put(name.toUpperCase(), snip); 309 return snip; 310 } 311 312 public List storageAll() { 314 String applicationOid = (String ) Application.get().getObject(Application.OID); 315 return storageAll(applicationOid); 316 } 317 318 public int storageCount() { 319 Application app = Application.get(); 320 File fileStore = new File (app.getConfiguration().getFileStore()); 321 return traverseFileStore(fileStore, new ArrayList()).size(); 322 } 323 324 public List storageAll(String applicationOid) { 325 return traverseFileStore(getWorkingDir(applicationOid), new ArrayList()); 326 } 327 328 private List traverseFileStore(File root, List list) { 329 try { 330 Map map = createSnipFromFile(root); 331 if (null != map) { 332 list.add(parseSnip(map)); 333 } 334 } catch (IOException e) { 335 } 337 338 File [] files = root.listFiles(); 339 for (int entry = 0; files != null && entry < files.length; entry++) { 340 if (files[entry].isDirectory()) { 341 traverseFileStore(files[entry], list); 342 } 343 } 344 return list; 345 } 346 347 349 350 protected void close(OutputStream out) { 351 if (out != null) { 352 try { 353 out.close(); 354 } catch (IOException e) { 355 } 357 } 358 } 359 360 protected void close(InputStream in) { 361 if (in != null) { 362 try { 363 in.close(); 364 } catch (IOException e) { 365 } 367 } 368 } 369 370 374 public List storageByHotness(int size) { 375 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 376 } 377 378 public List storageByUser(final String login) { 379 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 380 } 381 382 public List storageByDateSince(final Timestamp date) { 383 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 384 } 385 386 public List storageByRecent(String applicationOid, int size) { 387 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG + " : storageByRecent(applicationOid,size)"); 388 } 389 390 public List storageByComments(final Snip parent) { 391 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 392 } 393 394 public List storageByParent(final Snip parent) { 395 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 396 } 397 398 public List storageByParentNameOrder(Snip parent, int count) { 399 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 400 } 401 402 public List storageByParentModifiedOrder(Snip parent, int count) { 403 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 404 } 405 406 public List storageByDateInName(String nameSpace, String start, String end) { 407 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 408 } 409 410 public Snip[] match(String pattern) { 411 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 412 } 413 414 public Snip[] match(String start, String end) { 415 throw new MethodNotSupportedException(NOT_SUPPORTED_EXCEPTION_MSG); 416 } 417 418 public class MethodNotSupportedException extends RuntimeException { 419 public MethodNotSupportedException(String s) { 420 super(s); 421 } 422 } 423 } 424 | Popular Tags |