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