1 package org.ibex.mail.target;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.*;
10 // FIXME: appallingly inefficient
11 public class FileSystem {
13 private static final String STORAGE_ROOT =
14 System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
15 public static FileBased root = null;
16 public static Transcript transcript = null;
19 root = new FileBased(STORAGE_ROOT + File.separatorChar);
20 transcript = new Transcript(STORAGE_ROOT + File.separatorChar + "transcript");
21 } catch (Exception e) {
26 public FileSystem slash(String name) throws IOException {
27 throw new Error(this.getClass().getName() + " does not support the slash() method"); }
28 public int[] list() { throw new Error(this.getClass().getName() + " does not support the list() method"); }
29 public int add(Message message) throws IOException {
30 throw new Error(this.getClass().getName() + " does not support the add() method"); }
31 public int delete(Message message) throws IOException {
32 throw new Error(this.getClass().getName() + " does not support the delete() method"); }
33 public Message get(int messagenum) throws IOException {
34 throw new Error(this.getClass().getName() + " does not support the get() method"); }
35 public Message[] query(int maxResults) {
36 throw new Error(this.getClass().getName() + " does not support the query() method"); }
38 /** a fast-write, slow-read place to stash all messages we touch -- in case of a major f*ckup */
39 public static class Transcript extends FileSystem {
41 public Transcript(String path) throws IOException { new File(this.path = path).mkdirs(); }
42 private static String lastTime = null;
43 private static int lastCounter = 0;
45 /** returns a message identifier */
46 public synchronized int add(Message message) throws IOException {
47 File today = new File(path + File.separatorChar + (new SimpleDateFormat("yy-MMM-dd").format(new Date())));
50 String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
51 synchronized (Transcript.class) {
52 if (lastTime != null && lastTime.equals(time)) {
53 time += "." + (++lastCounter);
59 File target = new File(today.getPath() + File.separatorChar + time + ".txt");
60 OutputStream os = new FileOutputStream(target);
67 public static class FileBased extends FileSystem {
69 private FileBased(String path) throws IOException { new File(this.path = path).mkdirs(); }
70 public FileSystem slash(String name) throws IOException { return new FileBased(path + "/" + name); }
73 String[] names = new File(path).list();
74 int[] ret = new int[names.length];
75 for(int i=0, j=0; j<ret.length; i++, j++) {
77 ret[j] = Integer.parseInt(names[i].substring(0, names[i].length() - 1));
78 } catch (NumberFormatException nfe) {
79 Log.warn(FileBased.class, "NumberFormatException: " + names[i].substring(0, names[i].length() - 1));
81 int[] newret = new int[ret.length - 1];
82 System.arraycopy(ret, 0, newret, 0, newret.length);
89 /** returns a message identifier */
90 public synchronized int add(Message message) throws IOException {
93 for(int i=0; i<all.length; i++) max = Math.max(max, all[i]);
95 File f = new File(path + File.separatorChar + max + ".-");
96 FileOutputStream fo = new FileOutputStream(f);
99 f.renameTo(new File(path + File.separatorChar + max + "."));
103 public Message get(int messagenum) throws IOException {
104 File f = new File(path + File.separatorChar + messagenum + ".");
105 if (!f.exists()) throw new FileNotFoundException(f.toString());
107 // FIXME: need to store envelope from/to
108 //Message ret = new Message(new LineReader(new InputStreamReader(new FileInputStream(f))));
109 // FIXME: set answered/read/etc here
113 } catch (MailException.Malformed malf) {
114 Log.error(this, "This should never happen");
115 Log.error(this, malf);
121 // query types: stringmatch (headers, body), header element, deletion status, date range, message size
122 public Message[] query(int maxResults) {
123 throw new RuntimeException("FileBased.query() not implemented yet");