it compiles
[org.ibex.mail.git] / src / org / ibex / mail / target / FileSystem.java
1 package org.ibex.mail.target;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.*;
5 import java.io.*;
6 import java.net.*;
7 import java.util.*;
8 import java.text.*;
9
10 // FIXME: appallingly inefficient
11 public class FileSystem {
12
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;
17     static {
18         try {
19             root = new FileBased(STORAGE_ROOT + File.separatorChar);
20             transcript = new Transcript(STORAGE_ROOT + File.separatorChar + "transcript");
21         } catch (Exception e) {
22             e.printStackTrace();
23         }
24     }
25
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"); }
37
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 {
40         private String path;
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;
44
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())));
48             today.mkdirs();
49             
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);
54                 } else {
55                     lastTime = time;
56                 }
57             }
58             
59             File target = new File(today.getPath() + File.separatorChar + time + ".txt");
60             OutputStream os = new FileOutputStream(target);
61             message.dump(os);
62             os.close();
63             return -1;
64         }
65     }
66
67     public static class FileBased extends FileSystem {
68         private String path;
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); }
71
72         public int[] list() {
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++) {
76                 try {
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));
80                     j--;
81                     int[] newret = new int[ret.length - 1];
82                     System.arraycopy(ret, 0, newret, 0, newret.length);
83                     ret = newret;
84                 }
85             }
86             return ret;
87         }
88
89         /** returns a message identifier */
90         public synchronized int add(Message message) throws IOException {
91             int[] all = list();
92             int max = 0;
93             for(int i=0; i<all.length; i++) max = Math.max(max, all[i]);
94             int target = max++;
95             File f = new File(path + File.separatorChar + max + ".-");
96             FileOutputStream fo = new FileOutputStream(f);
97             message.dump(fo);
98             fo.close();
99             f.renameTo(new File(path + File.separatorChar + max + "."));
100             return target;
101         }
102
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());
106             //try {
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
110                 //return ret;
111                 return null;
112                 /*
113             } catch (MailException.Malformed malf) {
114                 Log.error(this, "This should never happen");
115                 Log.error(this, malf);
116                 return null;
117             }
118                 */
119         }
120
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");
124         }
125
126     }
127
128 }