1 25 26 package org.snipsnap.snip.storage; 27 28 import org.radeox.util.logging.Logger; 29 import org.snipsnap.snip.Snip; 30 31 import java.io.*; 32 import java.util.Map ; 33 34 44 45 public abstract class OneFileSnipStorage extends FileSnipStorage { 46 47 55 protected abstract Map loadSnip(InputStream in) throws IOException; 56 57 63 protected abstract String getFileName(); 64 65 74 75 protected abstract void storeSnip(Snip snip, OutputStream out); 76 77 84 85 protected void storageRemoveFile(Snip snip, File snipDir) { 86 File file = new File(snipDir, getFileName()); 87 Logger.debug(file + ": exists? " + file.exists()); 88 if (file.exists()) { 89 File backup = new File(file.getPath() + ".removed"); 90 file.renameTo(backup); 91 } 92 } 93 94 100 protected VersionFileNameChecker getVersionFileNameChecker() { 101 return new VersionFileNameChecker() { 102 public int getVersion(String fileName) { 103 return Integer.parseInt(fileName.substring(fileName.lastIndexOf("-") + 1)); 104 } 105 106 public boolean accept(File dir, String name) { 107 return name.startsWith(getFileName()) && (name.indexOf('-') != -1); 108 } 109 }; 110 } 111 112 120 protected Map loadVersion(Snip snip, File versionDir, int version) throws IOException { 121 if (!versionDir.exists()) { 122 return null; 123 } 124 125 File versionFile = new File(versionDir, getFileName() + "-" + version); 126 if (!versionFile.exists()) { 127 return null; 128 } 129 130 FileInputStream in = new FileInputStream(versionFile); 131 Map map = null; 132 try { 133 map = loadSnip(in); 134 } finally { 135 close(in); 136 } 137 return map; 138 } 139 140 141 148 public void storeVersion(Snip snip, File versionDir) { 149 if (!versionDir.exists()) { 150 versionDir.mkdirs(); 151 } 152 153 File file = new File(versionDir, getFileName() + "-" + snip.getVersion()); 154 FileOutputStream out = null; 155 try { 156 out = new FileOutputStream(file); 157 storeSnip(snip, out); 158 } catch (IOException e) { 159 Logger.log("FileSnipStorage: unable to store version snip" + snip.getName(), e); 160 } finally { 161 close(out); 162 } 163 } 164 165 174 protected void storeSnip(Snip snip, File snipDir) { 175 if (!snipDir.exists()) { 176 snipDir.mkdirs(); 177 } 178 179 File file = new File(snipDir, getFileName()); 180 if (file.exists()) { 181 Logger.log("FileSnipStorage: backing up " + file.getPath()); 182 File backup = new File(file.getPath() + ".bck"); 183 file.renameTo(backup); 184 } 185 186 FileOutputStream out = null; 187 try { 188 out = new FileOutputStream(file); 189 storeSnip(snip, out); 190 } catch (IOException e) { 191 Logger.log("FileSnipStorage: unable to store snip metadata" + snip.getName(), e); 192 } finally { 193 close(out); 194 } 195 } 196 197 205 protected synchronized Map createSnipFromFile(File snipDir) throws IOException { 206 File metadataFile = new File(snipDir, getFileName()); 207 if (!metadataFile.exists()) { 208 return null; 209 } 210 211 FileInputStream in = new FileInputStream(metadataFile); 212 Map map = null; 213 try { 214 map = loadSnip(in); 215 } finally { 216 close(in); 217 } 218 return map; 219 } 220 } 221 | Popular Tags |