1 package org.ibex.mail.target;
2 import org.ibex.util.*;
3 import org.ibex.mail.*;
9 // FIXME: appallingly inefficient
10 public class FileSystem {
12 private static final String STORAGE_ROOT =
13 System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
15 //public final FileBased root = new FileBased(STORAGE_ROOT + File.separatorChar);
16 public static Transcript transcript = null;
19 transcript = new Transcript(STORAGE_ROOT + File.separatorChar + "transcript");
20 } catch (Exception e) {
25 public FileSystem slash(String name) throws IOException {
26 throw new Error(this.getClass().getName() + " does not support the slash() method"); }
27 public int[] list() { throw new Error(this.getClass().getName() + " does not support the list() method"); }
28 public int add(Message message) throws IOException {
29 throw new Error(this.getClass().getName() + " does not support the add() method"); }
30 public Message.StoredMessage get(int messagenum) throws IOException {
31 throw new Error(this.getClass().getName() + " does not support the get() method"); }
32 public Message.StoredMessage[] query(int maxResults) {
33 throw new Error(this.getClass().getName() + " does not support the query() method"); }
35 /** a fast-write, slow-read place to stash all messages we touch -- in case of a major f*ckup */
36 public static class Transcript extends FileSystem {
38 public Transcript(String path) throws IOException { new File(this.path = path).mkdirs(); }
39 private static String lastTime = null;
40 private static int lastCounter = 0;
42 /** returns a message identifier */
43 public synchronized int add(Message message) throws IOException {
44 File today = new File(path + File.separatorChar + (new SimpleDateFormat("yy-MMM-dd").format(new Date())));
47 String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
48 synchronized (Transcript.class) {
49 if (lastTime != null && lastTime.equals(time)) {
50 time += "." + (++lastCounter);
56 File target = new File(today.getPath() + File.separatorChar + time + ".txt");
57 OutputStream os = new FileOutputStream(target);
64 public static class FileBased extends FileSystem {
66 private FileBased(String path) throws IOException { new File(this.path = path).mkdirs(); }
67 public FileSystem slash(String name) throws IOException { return new FileBased(path + "/" + name); }
70 String[] names = new File(path).list();
71 int[] ret = new int[names.length];
72 for(int i=0, j=0; j<ret.length; i++, j++) {
74 ret[j] = Integer.parseInt(names[i].substring(0, names[i].length() - 1));
75 } catch (NumberFormatException nfe) {
76 Log.warn(FileBased.class, "NumberFormatException: " + names[i].substring(0, names[i].length() - 1));
78 int[] newret = new int[ret.length - 1];
79 System.arraycopy(ret, 0, newret, 0, newret.length);
86 /** returns a message identifier */
87 public synchronized int add(Message message) throws IOException {
90 for(int i=0; i<all.length; i++) max = Math.max(max, all[i]);
92 File f = new File(path + File.separatorChar + max + ".-");
93 FileOutputStream fo = new FileOutputStream(f);
96 f.renameTo(new File(path + File.separatorChar + max + "."));
100 public Message.StoredMessage get(int messagenum) throws IOException {
101 File f = new File(path + File.separatorChar + messagenum + ".");
102 if (!f.exists()) throw new FileNotFoundException(f.toString());
104 Message.StoredMessage ret =
105 new Message.StoredMessage(new LineReader(new InputStreamReader(new FileInputStream(f))));
106 // FIXME: set answered/read/etc here
108 } catch (MailException.Malformed malf) {
109 Log.error(this, "This should never happen");
110 Log.error(this, malf);
115 // query types: stringmatch (headers, body), header element, deletion status, date range, message size
116 public Message.StoredMessage[] query(int maxResults) {
117 throw new RuntimeException("FileBased.query() not implemented yet");