Author: dbkr
Date: 2006-08-15 20:38:49 +0000 (Tue, 15 Aug 2006)
New Revision: 10099

Added:
   trunk/apps/Freemail/src/freemail/config/
   trunk/apps/Freemail/src/freemail/config/ConfigClient.java
   trunk/apps/Freemail/src/freemail/config/Configurator.java
Modified:
   trunk/apps/Freemail/src/freemail/Freemail.java
   trunk/apps/Freemail/src/freemail/fcp/FCPContext.java
   trunk/apps/Freemail/src/freemail/imap/IMAPListener.java
   trunk/apps/Freemail/src/freemail/imap/IMAPMessage.java
   trunk/apps/Freemail/src/freemail/smtp/SMTPListener.java
   trunk/apps/Freemail/src/freemail/utils/PropsFile.java
Log:
Config file support.


Modified: trunk/apps/Freemail/src/freemail/Freemail.java
===================================================================
--- trunk/apps/Freemail/src/freemail/Freemail.java      2006-08-15 20:25:45 UTC 
(rev 10098)
+++ trunk/apps/Freemail/src/freemail/Freemail.java      2006-08-15 20:38:49 UTC 
(rev 10099)
@@ -7,8 +7,10 @@
 import freemail.fcp.FCPConnection;
 import freemail.imap.IMAPListener;
 import freemail.smtp.SMTPListener;
+import freemail.config.Configurator;
+import freemail.config.ConfigClient;

-public class Freemail {
+public class Freemail implements ConfigClient {
        // version info
        public static final int VER_MAJOR = 0;
        public static final int VER_MINOR = 1;
@@ -19,7 +21,9 @@
        private static final String DATADIR = "data";
        private static final String GLOBALDATADIR = "globaldata";
        private static final String ACKDIR = "delayedacks";
+       private static final String CFGFILE = "globalconfig";
        private static File datadir;
+       private static File globaldatadir;
        private static File tempdir;
        private static FCPConnection fcpconn;

@@ -32,8 +36,7 @@
        }

        public static void main(String[] args) {
-               String fcphost = "localhost";
-               int fcpport = 9481;
+               String cfgfile = CFGFILE;

                String action = "";
                String account = null;
@@ -72,29 +75,23 @@
                                }
                                account = args[i - 1];
                                alias = args[i];
-                       } else if (args[i].equals("-h")) {
+                       } else if (args[i].equals("-c")) {
                                i++;
                                if (args.length - 1 < i) {
-                                       System.out.println("No hostname 
supplied, using default");
+                                       System.out.println("No config file 
supplied, using default");
                                        continue;
                                }
-                               fcphost = args[i];
-                       } else if (args[i].equals("-p")) {
-                               i++;
-                               if (args.length - 1 < i) {
-                                       System.out.println("No port supplied, 
using default");
-                                       continue;
-                               }
-                               try {
-                                       fcpport = Integer.parseInt(args[i]);
-                               } catch (NumberFormatException nfe) {
-                                       System.out.println("Bad port supplied, 
using default");
-                               }
+                               cfgfile = args[i];
                        }
                }

-               FCPContext fcpctx = new FCPContext(fcphost, fcpport);
+               Configurator cfg = new Configurator(new File(cfgfile));

+               FCPContext fcpctx = new FCPContext();
+               
+               cfg.register("fcp_host", fcpctx, "localhost");
+               cfg.register("fcp_port", fcpctx, "9481");
+               
                Freemail.fcpconn = new FCPConnection(fcpctx);
                Thread fcpthread  = new Thread(fcpconn);
                fcpthread.setDaemon(true);
@@ -132,13 +129,13 @@
                        return;
                }

-               File globaldatadir = new File(GLOBALDATADIR);
+               cfg.register("globaldatadir", new Freemail(), GLOBALDATADIR);
                if (!globaldatadir.exists()) {
                        globaldatadir.mkdir();
                }

                // start a SingleAccountWatcher for each account
-               Freemail.datadir = new File(DATADIR);
+               cfg.register("datadir", new Freemail(), Freemail.DATADIR);
                if (!Freemail.datadir.exists()) {
                        System.out.println("Starting Freemail for the first 
time.");
                        System.out.println("You will probably want to add an 
account by running Freemail with arguments --newaccount <username>");
@@ -147,7 +144,7 @@
                                System.exit(1);
                        }
                }
-               Freemail.tempdir = new File(Freemail.TEMPDIRNAME);
+               cfg.register("tempdir", new Freemail(), Freemail.TEMPDIRNAME);
                if (!Freemail.tempdir.exists()) {
                        if (!Freemail.tempdir.mkdir()) {
                                System.out.println("Couldn't create temporary 
directory. Please ensure that the user you are running Freemail as has write 
access to its working directory");
@@ -173,7 +170,7 @@
                senderthread.start();

                // start the SMTP Listener
-               SMTPListener smtpl = new SMTPListener(sender);
+               SMTPListener smtpl = new SMTPListener(sender, cfg);
                Thread smtpthread = new Thread(smtpl, "SMTP Listener");
                smtpthread.setDaemon(true);
                smtpthread.start();
@@ -188,7 +185,17 @@


                // start the IMAP listener
-               IMAPListener imapl = new IMAPListener();
+               IMAPListener imapl = new IMAPListener(cfg);
                imapl.run();
        }
+       
+       public void setConfigProp(String key, String val) {
+               if (key.equalsIgnoreCase("datadir")) {
+                       Freemail.datadir = new File(val);
+               } else if (key.equalsIgnoreCase("tempdir")) {
+                       Freemail.tempdir = new File(val);
+               } else if (key.equalsIgnoreCase("globaldatadir")) {
+                       Freemail.globaldatadir = new File(val);
+               }
+       }
 }

Added: trunk/apps/Freemail/src/freemail/config/ConfigClient.java
===================================================================
--- trunk/apps/Freemail/src/freemail/config/ConfigClient.java   2006-08-15 
20:25:45 UTC (rev 10098)
+++ trunk/apps/Freemail/src/freemail/config/ConfigClient.java   2006-08-15 
20:38:49 UTC (rev 10099)
@@ -0,0 +1,5 @@
+package freemail.config;
+
+public interface ConfigClient {
+       public void setConfigProp(String key, String val);
+}

Added: trunk/apps/Freemail/src/freemail/config/Configurator.java
===================================================================
--- trunk/apps/Freemail/src/freemail/config/Configurator.java   2006-08-15 
20:25:45 UTC (rev 10098)
+++ trunk/apps/Freemail/src/freemail/config/Configurator.java   2006-08-15 
20:38:49 UTC (rev 10099)
@@ -0,0 +1,53 @@
+package freemail.config;
+
+import java.io.File;
+import java.util.HashMap;
+
+import freemail.utils.PropsFile;
+
+/** "Probably the simplest config interface in the world"
+ *
+ */
+
+public class Configurator {
+       private final PropsFile props;
+       private final HashMap callbacks;
+
+       public Configurator(File f) {
+               this.props = new PropsFile(f);
+               this.props.setCommentPrefix("#");
+               String ls = System.getProperty("line.separator");
+               StringBuffer head = new StringBuffer();
+               head.append("# This is the configuration file for 
Freemail."+ls);
+               head.append("# "+ls);
+               head.append("# You are free to edit this file, but if 
Freemail"+ls);
+               head.append("# breaks as a result, you should delete this 
file"+ls);
+               head.append("# and Freemail will reset all values to their"+ls);
+               head.append("# defaults."+ls);
+               head.append("# The Freemail community cannot provide 
support"+ls);
+               head.append("# to people who have broken Freemail as a 
result"+ls);
+               head.append("# of editing this file.");
+               
+               this.props.setHeader(head.toString());
+               this.callbacks = new HashMap();
+       }
+       
+       public void register(String key, ConfigClient cb, String defaultval) {
+               this.callbacks.put(key, cb);
+               
+               String val = this.props.get(key);
+               if (val == null) {
+                       val = defaultval;
+                       props.put(key, val);
+               }
+               cb.setConfigProp(key, val);
+       }
+       
+       public void set(String key, String val) {
+               this.props.put(key, val);
+               
+               ConfigClient cb = (ConfigClient)this.callbacks.get(key);
+               if (cb == null) return;
+               cb.setConfigProp(key, val);
+       }
+}

Modified: trunk/apps/Freemail/src/freemail/fcp/FCPContext.java
===================================================================
--- trunk/apps/Freemail/src/freemail/fcp/FCPContext.java        2006-08-15 
20:25:45 UTC (rev 10098)
+++ trunk/apps/Freemail/src/freemail/fcp/FCPContext.java        2006-08-15 
20:38:49 UTC (rev 10099)
@@ -3,16 +3,25 @@
 import java.io.IOException;
 import java.net.Socket;

-public class FCPContext {
-       private final String hostname;
-       private final int port;
+import freemail.config.ConfigClient;
+
+public class FCPContext implements ConfigClient {
+       private String hostname;
+       private int port;

-       public FCPContext(String h, int p) {
-               this.hostname = h;
-               this.port = p;
-       }
-       
        public Socket getConn() throws IOException {
                return new Socket(this.hostname, this.port);
        }
+       
+       public void setConfigProp(String key, String val) {
+               if (key.equalsIgnoreCase("fcp_host")) {
+                       hostname = val;
+               } else if (key.equalsIgnoreCase("fcp_port")) {
+                       try {
+                               port = Integer.parseInt(val);
+                       } catch (NumberFormatException nfe) {
+                               // just leave it as it was
+                       }
+               }
+       }
 }

Modified: trunk/apps/Freemail/src/freemail/imap/IMAPListener.java
===================================================================
--- trunk/apps/Freemail/src/freemail/imap/IMAPListener.java     2006-08-15 
20:25:45 UTC (rev 10098)
+++ trunk/apps/Freemail/src/freemail/imap/IMAPListener.java     2006-08-15 
20:38:49 UTC (rev 10099)
@@ -1,11 +1,30 @@
 package freemail.imap;

 import java.net.ServerSocket;
+import java.net.InetAddress;
 import java.io.IOException;

-public class IMAPListener implements Runnable {
+import freemail.config.Configurator;
+import freemail.config.ConfigClient;
+
+public class IMAPListener implements Runnable,ConfigClient {
        private static final int LISTENPORT = 3143;
-
+       private String bindaddress;
+       private int bindport;
+       
+       public IMAPListener(Configurator cfg) {
+               cfg.register("imap_bind_address", this, "127.0.0.1");
+               cfg.register("imap_bind_port", this, 
Integer.toString(LISTENPORT));
+       }
+       
+       public void setConfigProp(String key, String val) {
+               if (key.equalsIgnoreCase("imap_bind_address")) {
+                       this.bindaddress = val;
+               } else if (key.equalsIgnoreCase("imap_bind_port")) {
+                       this.bindport = Integer.parseInt(val);
+               }
+       }
+       
        public void run() {
                try {
                        this.realrun();
@@ -15,7 +34,7 @@
        }

        public void realrun() throws IOException {
-               ServerSocket sock = new ServerSocket(LISTENPORT);
+               ServerSocket sock = new ServerSocket(this.bindport, 10, 
InetAddress.getByName(this.bindaddress));

                while (!sock.isClosed()) {
                        try {

Modified: trunk/apps/Freemail/src/freemail/imap/IMAPMessage.java
===================================================================
--- trunk/apps/Freemail/src/freemail/imap/IMAPMessage.java      2006-08-15 
20:25:45 UTC (rev 10098)
+++ trunk/apps/Freemail/src/freemail/imap/IMAPMessage.java      2006-08-15 
20:38:49 UTC (rev 10099)
@@ -2,7 +2,6 @@

 import java.util.Vector;
 import java.util.Stack;
-import java.util.Arrays;

 public class IMAPMessage {
        public final String tag;

Modified: trunk/apps/Freemail/src/freemail/smtp/SMTPListener.java
===================================================================
--- trunk/apps/Freemail/src/freemail/smtp/SMTPListener.java     2006-08-15 
20:25:45 UTC (rev 10098)
+++ trunk/apps/Freemail/src/freemail/smtp/SMTPListener.java     2006-08-15 
20:38:49 UTC (rev 10099)
@@ -1,16 +1,23 @@
 package freemail.smtp;

 import java.net.ServerSocket;
+import java.net.InetAddress;
 import java.io.IOException;

 import freemail.MessageSender;
+import freemail.config.ConfigClient;
+import freemail.config.Configurator;

-public class SMTPListener implements Runnable {
+public class SMTPListener implements Runnable,ConfigClient {
        private static final int LISTENPORT = 3025;
        private final MessageSender msgsender;
+       private String bindaddress;
+       private int bindport;

-       public SMTPListener(MessageSender sender) {
+       public SMTPListener(MessageSender sender, Configurator cfg) {
                this.msgsender = sender;
+               cfg.register("smtp_bind_address", this, "127.0.0.1");
+               cfg.register("smtp_bind_port", this, 
Integer.toString(LISTENPORT));
        }

        public void run() {
@@ -21,8 +28,16 @@
                }
        }

+       public void setConfigProp(String key, String val) {
+               if (key.equalsIgnoreCase("smtp_bind_address")) {
+                       this.bindaddress = val;
+               } else if (key.equalsIgnoreCase("smtp_bind_port")) {
+                       this.bindport = Integer.parseInt(val);
+               }
+       }
+       
        public void realrun() throws IOException {
-               ServerSocket sock = new ServerSocket(LISTENPORT);
+               ServerSocket sock = new ServerSocket(this.bindport, 10, 
InetAddress.getByName(this.bindaddress));

                while (!sock.isClosed()) {
                        try {

Modified: trunk/apps/Freemail/src/freemail/utils/PropsFile.java
===================================================================
--- trunk/apps/Freemail/src/freemail/utils/PropsFile.java       2006-08-15 
20:25:45 UTC (rev 10098)
+++ trunk/apps/Freemail/src/freemail/utils/PropsFile.java       2006-08-15 
20:38:49 UTC (rev 10099)
@@ -15,6 +15,8 @@
        private final File file;
        private HashMap data;
        private BufferedReader bufrdr;
+       private String commentPrefix;
+       private String header;

        /** Pass true into stopAtBlank to cause the reader to stop upon 
encountering
         * a blank line. It's the the caller's responsibility to get
@@ -30,12 +32,22 @@
                        } catch (IOException ioe) {
                        }
                }
+               this.commentPrefix = null;
+               this.header = null;
        }

        public PropsFile(File f) {
                this(f, false);
        }

+       public void setCommentPrefix(String cp) {
+               this.commentPrefix = cp;
+       }
+       
+       public void setHeader(String hdr) {
+               this.header = hdr;
+       }
+       
        private BufferedReader read(boolean stopAtBlank) throws IOException {
                this.data = new HashMap();

@@ -43,6 +55,9 @@

                String line = null;
                while ( (line = br.readLine()) != null) {
+                       if (this.commentPrefix != null && 
line.startsWith(this.commentPrefix)) {
+                               continue;
+                       }
                        if (stopAtBlank && line.length() == 0) {
                                return br;
                        }
@@ -70,6 +85,8 @@
        private void write() throws IOException {
                PrintWriter pw = new PrintWriter(new 
FileOutputStream(this.file));

+               if (this.header != null) pw.println(this.header);
+               
                Iterator i = this.data.entrySet().iterator();
                while (i.hasNext()) {
                        Map.Entry e = (Map.Entry) i.next();


Reply via email to