X-Git-Url: http://git.megacz.com/?p=org.ibex.mail.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2FFileBasedMailbox.java;h=c965b9a3a05119c05294534ee5a152066a1c8c92;hp=e13e22dea9a4632b482d54af885473af023eed8f;hb=dba19dd54d92f5431946932068486f86d0cd4ee1;hpb=2a1d65e90e0dbae298a366fdf865b0025d7b15ed diff --git a/src/org/ibex/mail/FileBasedMailbox.java b/src/org/ibex/mail/FileBasedMailbox.java index e13e22d..c965b9a 100644 --- a/src/org/ibex/mail/FileBasedMailbox.java +++ b/src/org/ibex/mail/FileBasedMailbox.java @@ -6,7 +6,9 @@ package org.ibex.mail; import org.prevayler.*; import org.ibex.mail.*; import org.ibex.util.*; +import org.ibex.js.*; import org.ibex.io.*; +import org.ibex.io.Fountain; import java.io.*; import java.nio.*; import java.nio.channels.*; @@ -17,26 +19,36 @@ import javax.servlet.*; import javax.servlet.http.*; /** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */ -public class FileBasedMailbox extends Mailbox.Default { +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 instances = new WeakHashMap(); + + // FIXME: ideally this should be weak, but we end up getting duplicates of SqliteMailboxes + private static final HashMap instances = new HashMap(); + public String toString() { return "[FileBasedMailbox " + path.getAbsolutePath() + "]"; } - public Mailbox 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, MailTree newParent, String newName) { throw new RuntimeException("FIXME not implemented"); } + public Mailbox getMailbox() { return this; } // FIXME: should be a File() - public static synchronized Mailbox 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 { - Mailbox ret = instances.get(path); + MailTree ret = instances.get(path); if (ret == null) { - if (!create && !(new File(path).exists())) return null; - if (new File(new File(path)+"/subscribers").exists()) { - ret = new MailingList(new File(path), new FileBasedMailbox(new File(path))); - } else { - ret = new FileBasedMailbox(new File(path)); - } - instances.put(path, 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) { @@ -68,6 +80,7 @@ public class FileBasedMailbox extends Mailbox.Default { // acquire lock File lockfile = new File(this.path.getAbsolutePath() + slash + ".lock"); lock = new RandomAccessFile(lockfile, "rw").getChannel().tryLock(); + // FIXME!!! /* if (lock == null) { Log.warn(this, "warning: blocking waiting for a lock on " + path); @@ -84,8 +97,10 @@ public class FileBasedMailbox extends Mailbox.Default { 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); } } } @@ -118,6 +133,8 @@ public class FileBasedMailbox extends Mailbox.Default { for(int i=0; i