1 25 26 package org.snipsnap.snip.storage; 27 28 import org.radeox.util.logging.Logger; 29 import org.snipsnap.snip.Snip; 30 import org.snipsnap.versioning.VersionInfo; 31 32 import java.io.*; 33 import java.util.*; 34 import java.sql.Timestamp ; 35 36 47 48 public abstract class TwoFileSnipStorage extends FileSnipStorage { 49 50 58 protected abstract Map loadMetadata(InputStream in) throws IOException; 59 60 67 68 protected abstract String loadContent(InputStream in) throws IOException; 69 70 76 protected abstract String getMetadataFileName(); 77 78 84 85 protected abstract String getContentFileName(); 86 87 96 97 protected abstract void storeContent(Snip snip, OutputStream out); 98 99 108 protected abstract void storeMetadata(Snip snip, OutputStream out); 109 110 116 protected VersionFileNameChecker getVersionFileNameChecker() { 117 return new VersionFileNameChecker() { 118 public int getVersion(String fileName) { 119 return Integer.parseInt(fileName.substring(fileName.lastIndexOf("-")+1)); 120 } 121 public boolean accept(File dir, String name) { 122 return name.startsWith(getContentFileName()) && (name.indexOf('-') != -1); 123 } 124 }; 125 } 126 127 134 protected void storageRemoveMetadata(Snip snip, File snipDir) { 135 File metadataFile = new File(snipDir, getMetadataFileName()); 136 Logger.debug(metadataFile + ": exists? " + metadataFile.exists()); 137 if (metadataFile.exists()) { 138 File backup = new File(metadataFile.getPath() + ".removed"); 139 metadataFile.renameTo(backup); 140 } 141 } 142 143 150 protected void storageRemoveContent(Snip snip, File snipDir) { 151 File contentFile = new File(snipDir, getContentFileName()); 152 Logger.debug(contentFile+": exists? "+contentFile.exists()); 153 if (contentFile.exists()) { 154 File backup = new File(contentFile.getPath() + ".removed"); 155 contentFile.renameTo(backup); 156 } 157 } 158 159 166 public void storageRemoveFile(Snip snip, File snipDir) { 167 storageRemoveMetadata(snip, snipDir); 168 storageRemoveContent(snip, snipDir); 169 } 170 171 179 protected Map loadVersion(Snip snip, File versionDir, int version) throws IOException { 180 if (!versionDir.exists()) { 181 return null; 182 } 183 184 File metadataFile = new File(versionDir, getMetadataFileName() + "-" + version); 185 if (!metadataFile.exists()) { 186 return null; 187 } 188 189 File contentFile = new File(versionDir, getContentFileName() + "-" + version); 190 if (!contentFile.exists()) { 191 return null; 192 } 193 194 FileInputStream metaIn = null; 195 FileInputStream contentIn = null; 196 Map map = null; 197 try { 198 metaIn = new FileInputStream(metadataFile); 199 contentIn = new FileInputStream(contentFile); 200 map = createSnipFromFile(metaIn, contentIn); 201 } finally { 202 contentIn.close(); 203 metaIn.close(); 204 } 205 return map; 206 } 207 208 215 protected void storeVersion(Snip snip, File versionDir) { 216 if (!versionDir.exists()) { 217 versionDir.mkdirs(); 218 } 219 220 File metadataFile = new File(versionDir, getMetadataFileName() + "-" + snip.getVersion()); 221 FileOutputStream out = null; 222 try { 223 out = new FileOutputStream(metadataFile); 224 storeMetadata(snip, out); 225 } catch (IOException e) { 226 Logger.log("TwoFileSnipStorage: unable to store version snip metadata" + snip.getName(), e); 227 } finally { 228 close(out); 229 } 230 231 File contentFile = new File(versionDir, getContentFileName() + "-" + snip.getVersion()); 232 try { 233 out = new FileOutputStream(contentFile); 234 storeContent(snip, out); 235 } catch (IOException e) { 236 Logger.log("TwoFileSnipStorage: unable to store version snip content" + snip.getName(), e); 237 } finally { 238 close(out); 239 } 240 } 241 242 251 protected void storeSnip(Snip snip, File snipDir) { 252 if (!snipDir.exists()) { 253 snipDir.mkdirs(); 254 } 255 256 File metadataFile = new File(snipDir, getMetadataFileName()); 257 if (metadataFile.exists()) { 258 Logger.log("TwoFileSnipStorage: backing up " + metadataFile.getPath()); 259 File backup = new File(metadataFile.getPath() + ".bck"); 260 metadataFile.renameTo(backup); 261 } 262 263 FileOutputStream out = null; 264 try { 265 out = new FileOutputStream(metadataFile); 266 storeMetadata(snip, out); 267 } catch (IOException e) { 268 Logger.log("TwoFileSnipStorage: unable to store snip metadata" + snip.getName(), e); 269 } finally { 270 close(out); 271 } 272 273 File contentFile = new File(snipDir, getContentFileName()); 274 if (contentFile.exists()) { 275 Logger.log("TwoFileSnipStorage: backing up " + contentFile.getPath()); 276 File backup = new File(contentFile.getPath() + ".bck"); 277 contentFile.renameTo(backup); 278 } 279 280 try { 281 out = new FileOutputStream(contentFile); 282 storeContent(snip, out); 283 out.close(); 284 } catch (IOException e) { 285 Logger.log("TwoFileSnipStorage: unable to store snip content" + snip.getName(), e); 286 } finally { 287 close(out); 288 } 289 } 290 291 298 protected synchronized Map createSnipFromFile(File snipDir) throws IOException { 299 File metadataFile = new File(snipDir, getMetadataFileName()); 300 if (!metadataFile.exists()) { 301 return null; 302 } 303 304 File contentFile = new File(snipDir, getContentFileName()); 305 if (!contentFile.exists()) { 306 return null; 307 } 308 309 FileInputStream metaIn = null; 310 FileInputStream contentIn = null; 311 Map map = null; 312 try { 313 metaIn = new FileInputStream(metadataFile); 314 contentIn = new FileInputStream(contentFile); 315 map = createSnipFromFile(metaIn, contentIn); 316 } finally { 317 contentIn.close(); 318 metaIn.close(); 319 } 320 return map; 321 } 322 323 331 private Map createSnipFromFile(InputStream metadataIn, InputStream contentIn) throws IOException { 332 Map metadata = loadMetadata(metadataIn); 333 metadata.put(SnipSerializer.SNIP_CONTENT, loadContent(contentIn)); 334 return metadata; 335 } 336 } 337 | Popular Tags |