// FIXME: appallingly inefficient
public class MessageStore {
- private final String STORAGE_ROOT = System.getProperty("org.ibex.mail.MessageStore.ROOT", "/var/org.ibex.mail/");
- public final MessageStore root = new MessageStore(STORAGE_ROOT);
-
- private String path;
- private MessageStore(String path) throws IOException { new File(this.path = path).mkdirs(); }
- public MessageStore slash(String name) { return new MessageStore(path + "/" + name); }
-
- public int[] list() {
- String[] names = new File(path).list();
- int[] ret = new int[names.length];
- for(int i=0, j=0; j<ret.length; i++, j++) {
- try {
- ret[j] = Integer.parseInt(names[i].substring(0, names[i].length - 1));
- } catch (NumberFormatException nfe) {
- Log.warn(MessageStore.class, "NumberFormatException: " + names[i].substring(0, names[i].length - 1));
- j--;
- int[] newret = new int[ret.length - 1];
- System.arrayCopy(ret, 0, newret, 0, newret.length);
- ret = newret;
+ private final String STORAGE_ROOT = System.getProperty("ibex.mail.root",
+ File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
+
+ //public final FileBased root = new FileBased(STORAGE_ROOT + File.separatorChar);
+ public final FileBased transcript = new FileBased(STORAGE_ROOT + File.separatorChar + "transcript");
+
+ public MessageStore slash(String name) {
+ throw new Error(this.getClass().getName() + " does not support the slash() method"); }
+ public int[] list() { throw new Error(this.getClass().getName() + " does not support the list() method"); }
+ public int add(StoredMessage message) throws IOException {
+ throw new Error(this.getClass().getName() + " does not support the add() method"); }
+ public StoredMessage get(int messagenum) throws IOException {
+ throw new Error(this.getClass().getName() + " does not support the get() method"); }
+ public StoredMessage[] query(int maxResults) {
+ throw new Error(this.getClass().getName() + " does not support the query() method"); }
+
+ /** a fast-write, slow-read place to stash all messages we touch -- in case of a major f*ckup */
+ public static class Transcript {
+ private String path;
+ public Transcript(String path) throws IOException { new File(this.path = path).mkdirs(); }
+ private static String lastTime = null;
+ private static int lastCounter = 0;
+
+ /** returns a message identifier */
+ public synchronized int add(StoredMessage message) throws IOException {
+ File today = new File(path + File.separatorChar + (new SimpleDateFormat("yyyyy.MMMMM.dd").format(new Date())));
+ today.mkdirs();
+
+ String time = new SimpleDateFormat("").format(new Date("hh.mm.ss"));
+ synchronized (Transcript.class) {
+ if (lastTime != null && lastTime.equals(time)) {
+ time += "." + (++lastCounter);
+ } else {
+ lastTime = time;
+ }
}
+
+ File target = new File(today.getPath() + File.separatorChar() + time + ".txt");
+ String msg = message.dumpStoredForm();
+ OutputStream os = new FileOutputStream(target);
+ os.write(msg.getBytes("UTF-8")); // FIXME: right?
+ os.close();
+ return -1; // FIXME
}
- return ret;
}
- /** returns a message identifier */
- public synchronized int add(StoredMessage message) throws IOException {
- int[] all = list();
- int max = 0;
- for(int i=0; i<all.length; i++) max = Math.max(max, all[i]);
- int target = max++;
- File f = new File(path + File.separatorChar + max + ".-");
- FileOutputStream fo = new FileOutputStream(f);
- message.dump(fo);
- fo.close();
- f.renameTo(path + File.separatorChar + max + ".");
- return target;
- }
+ public static FileBased extends MessageStore {
+ private String path;
+ private FileBased(String path) throws IOException { new File(this.path = path).mkdirs(); }
+ public FileBased slash(String name) { return new FileBased(path + "/" + name); }
- public StoredMessage get(int messagenum) throws IOException {
- File f = new File(path + File.separatorChar + messagenum + ".");
- if (!f.exists()) throw new FileNotFoundException(f);
- return StoredMessage.undump(new FileInputStream(f));
- }
+ public int[] list() {
+ String[] names = new File(path).list();
+ int[] ret = new int[names.length];
+ for(int i=0, j=0; j<ret.length; i++, j++) {
+ try {
+ ret[j] = Integer.parseInt(names[i].substring(0, names[i].length - 1));
+ } catch (NumberFormatException nfe) {
+ Log.warn(FileBased.class, "NumberFormatException: " + names[i].substring(0, names[i].length - 1));
+ j--;
+ int[] newret = new int[ret.length - 1];
+ System.arrayCopy(ret, 0, newret, 0, newret.length);
+ ret = newret;
+ }
+ }
+ return ret;
+ }
+
+ /** returns a message identifier */
+ public synchronized int add(StoredMessage message) throws IOException {
+ int[] all = list();
+ int max = 0;
+ for(int i=0; i<all.length; i++) max = Math.max(max, all[i]);
+ int target = max++;
+ File f = new File(path + File.separatorChar + max + ".-");
+ FileOutputStream fo = new FileOutputStream(f);
+ message.dump(fo);
+ fo.close();
+ f.renameTo(path + File.separatorChar + max + ".");
+ return target;
+ }
+
+ public StoredMessage get(int messagenum) throws IOException {
+ File f = new File(path + File.separatorChar + messagenum + ".");
+ if (!f.exists()) throw new FileNotFoundException(f);
+ return StoredMessage.undump(new FileInputStream(f));
+ }
+
+ // query types: stringmatch (headers, body), header element, deletion status, date range, message size
+ public StoredMessage[] query(int maxResults) {
+ throw new RuntimeException("FileBased.query() not implemented yet");
+ }
- // query types: stringmatch (headers, body), header element, deletion status, date range, message size
- public StoredMessage[] query(int maxResults) {
- throw new RuntimeException("MessageStore.query() not implemented yet");
}
}