1 package org.ibex.mail.store;
2 import org.ibex.util.*;
6 // FIXME: appallingly inefficient
7 public class MessageStore {
9 private final String STORAGE_ROOT = System.getProperty("org.ibex.mail.MessageStore.ROOT", "/var/org.ibex.mail/");
10 public final MessageStore root = new MessageStore(STORAGE_ROOT);
13 private MessageStore(String path) throws IOException { new File(this.path = path).mkdirs(); }
14 public MessageStore slash(String name) { return new MessageStore(path + "/" + name); }
17 String[] names = new File(path).list();
18 int[] ret = new int[names.length];
19 for(int i=0, j=0; j<ret.length; i++, j++) {
21 ret[j] = Integer.parseInt(names[i].substring(0, names[i].length - 1));
22 } catch (NumberFormatException nfe) {
23 Log.warn(MessageStore.class, "NumberFormatException: " + names[i].substring(0, names[i].length - 1));
25 int[] newret = new int[ret.length - 1];
26 System.arrayCopy(ret, 0, newret, 0, newret.length);
33 /** returns a message identifier */
34 public synchronized int add(StoredMessage message) throws IOException {
37 for(int i=0; i<all.length; i++) max = Math.max(max, all[i]);
39 File f = new File(path + File.separatorChar + max + ".-");
40 FileOutputStream fo = new FileOutputStream(f);
43 f.renameTo(path + File.separatorChar + max + ".");
47 public StoredMessage get(int messagenum) throws IOException {
48 File f = new File(path + File.separatorChar + messagenum + ".");
49 if (!f.exists()) throw new FileNotFoundException(f);
50 return StoredMessage.undump(new FileInputStream(f));
53 // query types: stringmatch (headers, body), header element, deletion status, date range, message size
54 public StoredMessage[] query(int maxResults) {
55 throw new RuntimeException("MessageStore.query() not implemented yet");