fix too many open files problem
authoradam <adam@megacz.com>
Tue, 3 Aug 2004 07:18:56 +0000 (07:18 +0000)
committeradam <adam@megacz.com>
Tue, 3 Aug 2004 07:18:56 +0000 (07:18 +0000)
darcs-hash:20040803071856-5007d-a90fca423104bfdf4d38966e514465e7e884a122.gz

src/org/ibex/mail/Main.java
src/org/ibex/mail/target/FileBasedMailbox.java
src/org/ibex/mail/target/Transcript.java

index 6a44fc6..cdca5a0 100644 (file)
@@ -13,11 +13,11 @@ public class Main implements Listener {
 
     public void accept(Connection conn) {
         Log.error(this, "connection port is " + conn.getLocalPort());
-        if (conn.getLocalPort() == 25) {         new SMTP.Server().handleRequest(conn);
-        } else if (conn.getLocalPort() == 143) { new IMAP.Listener().handleRequest(conn);
-        } else if (conn.getLocalPort() == 119) { new NNTP.Listener().handleRequest(conn);
+        if (conn.getLocalPort() == 25) {
+            new SMTP.Server().handleRequest(conn);
+        } else if (conn.getLocalPort() == 143) {
+            new IMAP.Listener().handleRequest(conn);
         }
-        conn.close();
     }
 
 }
index 54347cf..2602990 100644 (file)
@@ -77,31 +77,24 @@ public class FileBasedMailbox extends Mailbox.Default implements Serializable {
                 tmp.renameTo(uidNext);
                 return 1;
             }
-            FileInputStream fis = null;
-            int ret = -1;
-            try {
-                fis = new FileInputStream(uidNext);
-                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
-                ret = Integer.parseInt(br.readLine());
-                if (inc) {
-                    File tmp = new File(uidNext + "-");
-                    FileOutputStream fos = null;
-                    try {
-                        fos = new FileOutputStream(tmp);
-                        PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
-                        pw.println(ret+1);
-                        pw.flush();
-                    } finally { if (fos != null) fos.close(); }
-                    tmp.renameTo(uidNext);
-                }
-            } finally { if (fis != null) fis.close(); }
+            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(uidNext)));
+            int ret = Integer.parseInt(br.readLine());
+            br.close();
+            if (inc) {
+                File tmp = new File(uidNext + "-");
+                PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp)));
+                pw.println(ret+1);
+                pw.flush();
+                pw.close();
+                tmp.renameTo(uidNext);
+            }
             return ret;
         } catch (IOException e) { throw new MailException.IOException(e); }
     }        
 
     public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); }
     public synchronized void add(Message message, int flags) {
-        Log.info(path, message.summary());
+        Log.info(this, "adding message to ["+toString()+"]:\n" + message.summary());
         try {
             int num = new File(path).list(filter).length;
            String name = path + slash + uidNext(true) + "." +
@@ -111,7 +104,7 @@ public class FileBasedMailbox extends Mailbox.Default implements Serializable {
                 ((flags & Mailbox.Flag.ANSWERED) == Mailbox.Flag.ANSWERED ? "a" : "") +
                 ((flags & Mailbox.Flag.FLAGGED) == Mailbox.Flag.FLAGGED ? "f" : "") +
                 ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN ? "s" : "");
-            //Log.info(this, "    with chosen filename " + name);
+            Log.info(this, "    with chosen filename " + name);
             File target = new File(name);
             File f = new File(target.getCanonicalPath() + "-");
             FileOutputStream fo = new FileOutputStream(f);
@@ -121,7 +114,7 @@ public class FileBasedMailbox extends Mailbox.Default implements Serializable {
             message.dump(stream);
             fo.close();
             f.renameTo(target);
-            //Log.info(this, "    done writing.");
+            Log.info(this, "    done writing.");
         } catch (IOException e) { throw new MailException.IOException(e); }
     }
 
@@ -136,11 +129,25 @@ public class FileBasedMailbox extends Mailbox.Default implements Serializable {
            if (cur >= names.length) return null;
             try {
                 File file = new File(path + File.separatorChar + names[cur]);
-                FileInputStream fis = null;
-                try {
-                    fis = new FileInputStream(file);
-                    return new Message(new Stream(fis), new Message.Envelope(null, null, new Date(file.lastModified())));
-                } finally { if (fis != null) fis.close(); }
+                FileInputStream fis = new FileInputStream(file);
+                Stream stream = new Stream(fis);
+                Address envelopeFrom = null;
+                Address envelopeTo = null;
+                /*
+                for(String s = stream.readln(); s != null; s = stream.readln()) {
+                    if (s.startsWith("X-org.ibex.mail.headers.envelope.From: "))
+                        envelopeFrom = Address.parse(s.substring(38).trim());
+                    else if (s.startsWith("X-org.ibex.mail.headers.envelope.To: "))
+                        envelopeTo = Address.parse(s.substring(36).trim());
+                    else {
+                        stream.unread(s + "\r\n");
+                        break;
+                    }
+                }
+                */
+                Message ret = new Message(stream, new Message.Envelope(null, null, new Date(file.lastModified())));
+                fis.close();
+                return ret;
             } catch (IOException e) { throw new MailException.IOException(e);
             } catch (Message.Malformed e) { throw new MailException(e.getMessage()); }
         }
index 5d105bf..17aa7c8 100644 (file)
@@ -34,9 +34,10 @@ public class Transcript extends Target {
                 
             File target = new File(today.getPath() + File.separatorChar + time + ".txt");
             OutputStream os = new FileOutputStream(target);
-            message.dump(new Stream(os));
-            os.flush();
-            os.close();
+            try {
+                message.dump(new Stream(os));
+                os.flush();
+            } finally { os.close(); }
         } catch (IOException e) { throw new MailException.IOException(e); }
     }
 }