import javax.servlet.http.*;
/** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
-public class FileBasedMailbox extends Mailbox.Default implements MailboxTree {
+public class FileBasedMailbox extends Mailbox.Default implements MailTree {
- public static final long MAGIC_DATE = 0;
+ public static final long MAGIC_DATE = 0;
private static final char slash = File.separatorChar;
- private static final WeakHashMap<String,FileBasedMailbox> instances = new WeakHashMap<String,FileBasedMailbox>();
+
+ // FIXME: ideally this should be weak, but we end up getting duplicates of SqliteMailboxes
+ private static final HashMap<String,MailTree> instances = new HashMap<String,MailTree>();
+
public String toString() { return "[FileBasedMailbox " + path.getAbsolutePath() + "]"; }
- public MailboxTree slash(String name, boolean create) { return getFileBasedMailbox(path.getAbsolutePath()+slash+name, create); }
+ public MailTree slash(String name, boolean create) { return getFileBasedMailbox(path.getAbsolutePath()+slash+name, create); }
public void rmdir(String subdir) { throw new RuntimeException("FIXME not implemented"); }
- public void rename(String subdir, MailboxTree newParent, String newName) { throw new RuntimeException("FIXME not implemented"); }
+ public void rename(String subdir, MailTree newParent, String newName) { throw new RuntimeException("FIXME not implemented"); }
public Mailbox getMailbox() { return this; }
- public JS get(JS key) throws JSExn {
- return (JS)slash(JSU.toString(key), true);
- }
-
// FIXME: should be a File()
- public static synchronized MailboxTree getFileBasedMailbox(String path, boolean create) {
+ public static synchronized MailTree getFileBasedMailbox(String path, boolean create) {
+ if (path.endsWith(".sqlite")) path = path.substring(0, path.length()-".sqlite".length());
+ path = new File(path).getAbsolutePath().toString();
try {
- MailboxTree ret = instances.get(path);
+ MailTree ret = instances.get(path);
if (ret == null) {
- if (!create && !(new File(path).exists())) return null;
- ret = new FileBasedMailbox(new File(path));
- instances.put(path, (FileBasedMailbox)ret);
+ Log.error("n", "no match for " + path + " in " + instances.hashCode());
+ if (new File(path+".sqlite").exists()) ret = new SqliteMailbox(path+".sqlite");
+ else if (new File(path).exists()) ret = new FileBasedMailbox(new File(path));
+ else if (create) ret = new SqliteMailbox(path+".sqlite");
+ else return null;
+ instances.put(path, (MailTree)ret);
+ Log.error("n", "filling " + path + " with " + instances.get(path));
}
return ret;
} catch (Exception e) {
try {
if (files[i].indexOf('.') <= 0) continue;
files[i] = files[i].substring(0, files[i].indexOf('.'));
- int n = Integer.parseInt(files[i]);
- if (n>=uidNext) uidNext = n;
+ try {
+ int n = Integer.parseInt(files[i]);
+ if (n>=uidNext) uidNext = n;
+ } catch (NumberFormatException nfe) { continue; }
} catch(Exception e) { Log.error(this, e); }
}
}
for(int i=0; i<list.length; i++) {
File f = new File(path.getAbsolutePath() + slash + list[i]);
if (f.isDirectory() && f.getName().charAt(0) != '.') vec.addElement(list[i]);
+ if (!f.isDirectory() && f.getName().endsWith(".sqlite"))
+ vec.addElement(list[i].substring(0, list[i].length()-".sqlite".length()));
}
return (String[])vec.copyInto(new String[vec.size()]);
}
if ((flags & Mailbox.Flag.SEEN) == 0) f.setLastModified(MAGIC_DATE);
else if (f.lastModified()==MAGIC_DATE) f.setLastModified(System.currentTimeMillis());
// FIXME: other flags?
+ // FIXME: definitely need DELETED flag in order to do message moves!
}
public Headers head() { return done() ? null : new Headers(new Fountain.File(file())); }
public Message cur() { return Message.newMessage(new Fountain.File(file())); }