KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > storage > OneFileSnipStorage


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
33
34 /**
35  * SnipStorage backend that uses files for persisting data. This storage
36  * has limitations in the snip name length and possibly characters as well
37  * since not all filesystems can store UTF-8 file names.
38  *
39  * This Storage uses one file for persistance.
40  *
41  * @author Stephan J. Schmidt
42  * @version $Id: OneFileSnipStorage.java 1606 2004-05-17 10:56:18Z leo $
43  */

44
45 public abstract class OneFileSnipStorage extends FileSnipStorage {
46
47   /**
48    * Load the snip from an InputStream and
49    * store the metadata and content in a Map.
50    *
51    * @param in Stream to read from
52    * @return
53    * @throws IOException
54    */

55   protected abstract Map JavaDoc loadSnip(InputStream in) throws IOException;
56
57   /**
58    * Return the file name of the snip file. This should
59    * be implemented to return eg. snip.xml
60    *
61    * @return
62    */

63   protected abstract String JavaDoc getFileName();
64
65   /**
66    * Store the snip to
67    * the output stream. Implementations
68    * can store whichever format they choose
69    * (plain text, XML, ...)
70    *
71    * @param snip
72    * @param out
73    */

74
75   protected abstract void storeSnip(Snip snip, OutputStream out);
76
77   /**
78    * Remove the metadata and file of snip from the storage.
79    * This implementation stores the file to a
80    * .removed backup file
81    *
82    * @param snip
83    */

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   /**
95    * Return the special checker to get the version number
96    * from a file.
97    *
98    * @return
99    */

100   protected VersionFileNameChecker getVersionFileNameChecker() {
101     return new VersionFileNameChecker() {
102       public int getVersion(String JavaDoc fileName) {
103         return Integer.parseInt(fileName.substring(fileName.lastIndexOf("-") + 1));
104       }
105
106       public boolean accept(File dir, String JavaDoc name) {
107         return name.startsWith(getFileName()) && (name.indexOf('-') != -1);
108       }
109     };
110   }
111
112   /**
113    * Load a version of a snip from the file system
114    * and the given directory. Version is stored in one file
115    *
116    * @param snip
117    * @param versionDir
118    * @return
119    */

120   protected Map JavaDoc 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 JavaDoc map = null;
132     try {
133       map = loadSnip(in);
134     } finally {
135       close(in);
136     }
137     return map;
138   }
139
140
141   /**
142    * Store version of snip to the file system and
143    * given directory. Version is stored as one file
144    *
145    * @param snip
146    * @param versionDir
147    */

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   /**
166    * Store a snip to a directory
167    *
168    * Create two output streams and write
169    * the metadata and content to those
170    *
171    * @param snip
172    * @param snipDir
173    */

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   /**
198    * Read all data from the snip file in the given directory to
199    * a map
200    *
201    * @param snipDir Directory with the snip file
202    * @return
203    * @throws IOException
204    */

205   protected synchronized Map JavaDoc 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 JavaDoc map = null;
213     try {
214       map = loadSnip(in);
215     } finally {
216       close(in);
217     }
218     return map;
219   }
220 }
221
Popular Tags