package org.ibex.mail.store;
import org.ibex.util.*;
+import org.ibex.mail.*;
import java.io.*;
import java.net.*;
+import java.util.*;
+import java.text.*;
// FIXME: appallingly inefficient
public class MessageStore {
- private final String STORAGE_ROOT = System.getProperty("ibex.mail.root",
- File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
+ private static final String STORAGE_ROOT = System.getProperty("ibex.mail.root",
+ File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
//public final FileBased root = new FileBased(STORAGE_ROOT + File.separatorChar);
- public final FileBased transcript = new FileBased(STORAGE_ROOT + File.separatorChar + "transcript");
+ public static FileBased transcript = null;
+ static {
+ try {
+ transcript = new FileBased(STORAGE_ROOT + File.separatorChar + "transcript");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
- public MessageStore slash(String name) {
+ public MessageStore slash(String name) throws IOException {
throw new Error(this.getClass().getName() + " does not support the slash() method"); }
public int[] list() { throw new Error(this.getClass().getName() + " does not support the list() method"); }
- public int add(StoredMessage message) throws IOException {
+ public int add(Message.StoredMessage message) throws IOException {
throw new Error(this.getClass().getName() + " does not support the add() method"); }
- public StoredMessage get(int messagenum) throws IOException {
+ public Message.StoredMessage get(int messagenum) throws IOException {
throw new Error(this.getClass().getName() + " does not support the get() method"); }
- public StoredMessage[] query(int maxResults) {
+ public Message.StoredMessage[] query(int maxResults) {
throw new Error(this.getClass().getName() + " does not support the query() method"); }
/** a fast-write, slow-read place to stash all messages we touch -- in case of a major f*ckup */
private static int lastCounter = 0;
/** returns a message identifier */
- public synchronized int add(StoredMessage message) throws IOException {
+ public synchronized int add(Message.StoredMessage message) throws IOException {
File today = new File(path + File.separatorChar + (new SimpleDateFormat("yyyyy.MMMMM.dd").format(new Date())));
today.mkdirs();
}
}
- File target = new File(today.getPath() + File.separatorChar() + time + ".txt");
+ File target = new File(today.getPath() + File.separatorChar + time + ".txt");
String msg = message.dumpStoredForm();
OutputStream os = new FileOutputStream(target);
os.write(msg.getBytes("UTF-8")); // FIXME: right?
}
}
- public static FileBased extends MessageStore {
+ public static class FileBased extends MessageStore {
private String path;
private FileBased(String path) throws IOException { new File(this.path = path).mkdirs(); }
- public FileBased slash(String name) { return new FileBased(path + "/" + name); }
+ public FileBased slash(String name) throws IOException { return new FileBased(path + "/" + name); }
public int[] list() {
String[] names = new File(path).list();
int[] ret = new int[names.length];
for(int i=0, j=0; j<ret.length; i++, j++) {
try {
- ret[j] = Integer.parseInt(names[i].substring(0, names[i].length - 1));
+ ret[j] = Integer.parseInt(names[i].substring(0, names[i].length() - 1));
} catch (NumberFormatException nfe) {
- Log.warn(FileBased.class, "NumberFormatException: " + names[i].substring(0, names[i].length - 1));
+ Log.warn(FileBased.class, "NumberFormatException: " + names[i].substring(0, names[i].length() - 1));
j--;
int[] newret = new int[ret.length - 1];
- System.arrayCopy(ret, 0, newret, 0, newret.length);
+ System.arraycopy(ret, 0, newret, 0, newret.length);
ret = newret;
}
}
}
/** returns a message identifier */
- public synchronized int add(StoredMessage message) throws IOException {
+ public synchronized int add(Message.StoredMessage message) throws IOException {
int[] all = list();
int max = 0;
for(int i=0; i<all.length; i++) max = Math.max(max, all[i]);
FileOutputStream fo = new FileOutputStream(f);
message.dump(fo);
fo.close();
- f.renameTo(path + File.separatorChar + max + ".");
+ f.renameTo(new File(path + File.separatorChar + max + "."));
return target;
}
- public StoredMessage get(int messagenum) throws IOException {
+ public Message.StoredMessage get(int messagenum) throws IOException {
File f = new File(path + File.separatorChar + messagenum + ".");
- if (!f.exists()) throw new FileNotFoundException(f);
- return StoredMessage.undump(new FileInputStream(f));
+ if (!f.exists()) throw new FileNotFoundException(f.toString());
+ return Message.StoredMessage.undump(new FileInputStream(f));
}
// query types: stringmatch (headers, body), header element, deletion status, date range, message size
- public StoredMessage[] query(int maxResults) {
+ public Message.StoredMessage[] query(int maxResults) {
throw new RuntimeException("FileBased.query() not implemented yet");
}