17aa7c83aa1cff93982c7762b5840979ee69ce53
[org.ibex.mail.git] / src / org / ibex / mail / target / Transcript.java
1 package org.ibex.mail.target;
2 import org.ibex.mail.*;
3 import org.ibex.util.*;
4 import org.ibex.io.*;
5 import java.io.*;
6 import java.net.*;
7 import java.util.*;
8 import java.text.*;
9
10 /** a fast-write, slow-read place to stash all messages we touch -- in case of a major f*ckup */
11 public class Transcript extends Target {
12
13     public static final Transcript transcript = new Transcript(Mailbox.STORAGE_ROOT + File.separatorChar + "transcript");
14
15     private String path;
16     public Transcript(String path) { new File(this.path = path).mkdirs(); }
17     private static String lastTime = null;
18     private static int lastCounter = 0;
19
20     public synchronized void accept(Message message) {
21         try {
22             File today = new File(path + File.separatorChar + (new SimpleDateFormat("yy-MMM-dd").format(new Date())));
23             today.mkdirs();
24                 
25             String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
26             synchronized (Transcript.class) {
27                 if (lastTime != null && lastTime.equals(time)) {
28                     time += "." + (++lastCounter);
29                 } else {
30                     lastTime = time;
31                     lastCounter = 0;
32                 }
33             }
34                 
35             File target = new File(today.getPath() + File.separatorChar + time + ".txt");
36             OutputStream os = new FileOutputStream(target);
37             try {
38                 message.dump(new Stream(os));
39                 os.flush();
40             } finally { os.close(); }
41         } catch (IOException e) { throw new MailException.IOException(e); }
42     }
43 }
44