Author: dbkr
Date: 2008-04-10 22:11:01 +0000 (Thu, 10 Apr 2008)
New Revision: 19157

Modified:
   trunk/apps/Freemail/src/freemail/Freemail.java
   trunk/apps/Freemail/src/freemail/FreemailCli.java
   trunk/apps/Freemail/src/freemail/FreemailPlugin.java
Log:
Sort out horrendous code duplication in bootstrap classes.


Modified: trunk/apps/Freemail/src/freemail/Freemail.java
===================================================================
--- trunk/apps/Freemail/src/freemail/Freemail.java      2008-04-10 20:51:51 UTC 
(rev 19156)
+++ trunk/apps/Freemail/src/freemail/Freemail.java      2008-04-10 22:11:01 UTC 
(rev 19157)
@@ -22,25 +22,77 @@
 package freemail;

 import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
+
 import freemail.fcp.FCPConnection;
+import freemail.fcp.FCPContext;
+import freemail.imap.IMAPListener;
+import freemail.smtp.SMTPListener;
+import freemail.utils.Logger;
 import freemail.config.ConfigClient;
+import freemail.config.Configurator;

-public class Freemail implements ConfigClient {
+public abstract class Freemail implements ConfigClient {
        public static final int VER_MAJOR = 0;
        public static final int VER_MINOR = 1;
        public static final int BUILD_NO = 9;
        public static final String VERSION_TAG = "Pet Shop";

-       protected static final String TEMPDIRNAME = "temp";
+       private static final String TEMPDIRNAME = "temp";
        protected static final String DATADIR = "data";
-       protected static final String GLOBALDATADIR = "globaldata";
-       protected static final String ACKDIR = "delayedacks";
+       private static final String GLOBALDATADIR = "globaldata";
+       private static final String ACKDIR = "delayedacks";
        protected static final String CFGFILE = "globalconfig";
        private static File datadir;
        private static File globaldatadir;
        private static File tempdir;
        protected static FCPConnection fcpconn = null;

+       private Thread fcpThread;
+       private ArrayList /* of Thread */ singleAccountWatcherThreadList = new 
ArrayList();
+       private Thread messageSenderThread;
+       private Thread smtpThread;
+       private Thread ackInserterThread;
+       private Thread imapThread;
+       
+       private ArrayList singleAccountWatcherList = new ArrayList();
+       private MessageSender sender;
+       private SMTPListener smtpl;
+       private AckProcrastinator ackinserter;
+       private IMAPListener imapl;
+       
+       protected final Configurator configurator;
+       
+       protected Freemail(String cfgfile) {
+               configurator = new Configurator(new File(cfgfile));
+               
+               configurator.register("loglevel", new Logger(), "normal|error");
+               
+               configurator.register("datadir", this, Freemail.DATADIR);
+               if (!getDataDir().exists()) {
+                       if (!getDataDir().mkdir()) {
+                               Logger.error(this,"Freemail: Couldn't create 
data directory. Please ensure that the user you are running Freemail as has 
write access to its working directory");
+                               return;
+                       }
+               }
+               
+               configurator.register("globaldatadir", this, GLOBALDATADIR);
+               if (!getGlobalDataDir().exists()) {
+                       if (!getGlobalDataDir().mkdir()) {
+                               Logger.error(this,"Freemail: Couldn't create 
global data directory. Please ensure that the user you are running Freemail as 
has write access to its working directory");
+                       }
+               }
+               
+               configurator.register("tempdir", this, Freemail.TEMPDIRNAME);
+               if (!getTempDir().exists()) {
+                       if (!Freemail.getTempDir().mkdir()) {
+                               Logger.error(this,"Freemail: Couldn't create 
temporary directory. Please ensure that the user you are running Freemail as 
has write access to its working directory");
+                               return;
+                       }
+               }
+       }
+       
        public static File getTempDir() {
                return Freemail.tempdir;
        }
@@ -66,4 +118,122 @@
                        Freemail.globaldatadir = new File(val);
                }
        }
+       
+       protected void startFcp(boolean daemon) {
+               FCPContext fcpctx = new FCPContext();
+               configurator.register("fcp_host", fcpctx, "localhost");
+               configurator.register("fcp_port", fcpctx, "9481");
+               
+               Freemail.fcpconn = new FCPConnection(fcpctx);
+               fcpThread = new Thread(fcpconn, "Freemail FCP Connection");
+               fcpThread.setDaemon(true);
+               fcpThread.start();
+       }
+       
+       protected void startServers(boolean daemon) {
+               // start the SMTP Listener
+               smtpl = new SMTPListener(sender, configurator);
+               smtpThread = new Thread(smtpl, "Freemail SMTP Listener");
+               smtpThread.setDaemon(daemon);
+               smtpThread.start();
+               
+               // start the IMAP listener
+               imapl = new IMAPListener(configurator);
+               imapThread = new Thread(imapl, "Freemail IMAP Listener");
+               imapThread.setDaemon(daemon);
+               imapThread.start();
+       }
+       
+       protected void startWorkers(boolean daemon) {
+               System.out.println("This is Freemail version 
"+VER_MAJOR+"."+VER_MINOR+" build #"+BUILD_NO+" ("+VERSION_TAG+")");
+               System.out.println("Freemail is released under the terms of the 
GNU Lesser General Public License. Freemail is provided WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE. For details, see the LICENSE file included with this 
distribution.");
+               System.out.println("");
+               
+               // start a SingleAccountWatcher for each account
+               File[] files = getDataDir().listFiles();
+               for (int i = 0; i < files.length; i++) {
+                       if (files[i].getName().equals(".") || 
files[i].getName().equals(".."))
+                               continue;
+                       if (!files[i].isDirectory()) continue;
+
+                       String 
invalid=AccountManager.validateUsername(files[i].getName());
+                       if(!invalid.equals("")) {
+                               Logger.error(this,"Account name 
"+files[i].getName()+" contains invalid chars (\""+invalid+"\"), you may get 
problems accessing the account.");
+                       }
+                       
+                       SingleAccountWatcher saw = new 
SingleAccountWatcher(files[i]); 
+                       singleAccountWatcherList.add(saw);
+                       Thread t = new Thread(saw, "Freemail Account Watcher 
for "+files[i].getName());
+                       t.setDaemon(daemon);
+                       t.start();
+                       singleAccountWatcherThreadList.add(t);
+               }
+               
+               // and a sender thread
+               sender = new MessageSender(getDataDir());
+               messageSenderThread = new Thread(sender, "Freemail Message 
sender");
+               messageSenderThread.setDaemon(daemon);
+               messageSenderThread.start();
+               
+               // start the delayed ACK inserter
+               File ackdir = new File(getGlobalDataDir(), ACKDIR);
+               AckProcrastinator.setAckDir(ackdir);
+               ackinserter = new AckProcrastinator();
+               ackInserterThread = new Thread(ackinserter, "Freemail Delayed 
ACK Inserter");
+               ackInserterThread.setDaemon(daemon);
+               ackInserterThread.start();
+       }
+       
+       public void terminate() {
+               Iterator it = singleAccountWatcherList.iterator();
+               while(it.hasNext()) {
+                       ((SingleAccountWatcher)it.next()).kill();
+                       it.remove();
+               }
+
+               sender.kill();
+               ackinserter.kill();
+               smtpl.kill();
+               imapl.kill();
+               // now kill the FCP thread - that's what all the other threads 
will be waiting on
+               fcpconn.kill();
+               
+               // now clean up all the threads
+               boolean cleanedUp = false;
+               while (!cleanedUp) {
+                       try {
+                               it = singleAccountWatcherThreadList.iterator();
+                               while(it.hasNext()) {
+                                       ((Thread)it.next()).join();
+                                       it.remove();
+                               }
+                               
+                               if (messageSenderThread != null) {
+                                       messageSenderThread.join();
+                                       messageSenderThread = null;
+                               }
+                               if (ackInserterThread != null) {
+                                       ackInserterThread.join();
+                                       ackInserterThread = null;
+                               }
+                               if (smtpThread != null) {
+                                       smtpThread.join();
+                                       smtpl.joinClientThreads();
+                                       smtpThread = null;
+                               }
+                               if (imapThread != null) {
+                                       imapThread.join();
+                                       imapl.joinClientThreads();
+                                       imapThread = null;
+                               }
+                               if (fcpThread != null) {
+                                       fcpThread.join();
+                                       fcpThread = null;
+                               }
+                       } catch (InterruptedException ie) {
+                               
+                       }
+                       cleanedUp = true;
+               }
+       }
 }

Modified: trunk/apps/Freemail/src/freemail/FreemailCli.java
===================================================================
--- trunk/apps/Freemail/src/freemail/FreemailCli.java   2008-04-10 20:51:51 UTC 
(rev 19156)
+++ trunk/apps/Freemail/src/freemail/FreemailCli.java   2008-04-10 22:11:01 UTC 
(rev 19157)
@@ -21,33 +21,22 @@

 package freemail;

-import java.io.File;
 import java.io.IOException;
-//import java.util.ArrayList;
-//import java.util.Iterator;

 import freemail.Freemail;
-import freemail.fcp.FCPContext;
-import freemail.fcp.FCPConnection;
-import freemail.imap.IMAPListener;
-import freemail.smtp.SMTPListener;
-import freemail.utils.Logger;
-import freemail.config.Configurator;
-//import freemail.config.ConfigClient;

-public abstract class FreemailCli extends Freemail {
+public class FreemailCli extends Freemail {
+       public FreemailCli(String cfgfile) {
+               super(cfgfile);
+       }
+       
        public static void main(String[] args) {
-               String cfgfile = CFGFILE;
-               
                String action = "";
                String account = null;
                String newpasswd = null;
                String alias = null;
-               
-               System.out.println("This is Freemail version 
"+VER_MAJOR+"."+VER_MINOR+" build #"+BUILD_NO+" ("+VERSION_TAG+")");
-               System.out.println("Freemail is released under the terms of the 
GNU Lesser General Public License. Freemail is provided WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE. For details, see the LICENSE file included with this 
distribution.");
-               System.out.println("");
-               
+               String cfgfile = CFGFILE;
+
                for (int i = 0; i < args.length; i++) {
                        if (args[i].equals("--newaccount")) {
                                action = args[i];
@@ -89,20 +78,9 @@
                        }
                }

-               Configurator cfg = new Configurator(new File(cfgfile));
+               FreemailCli freemail = new FreemailCli(cfgfile);
+               freemail.startFcp(false);

-               cfg.register("loglevel", new Logger(), "normal|error");
-               
-               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);
-               fcpthread.start();
-               
                if (action.equals("--newaccount")) {
                        try {
                                AccountManager.Create(account);
@@ -144,68 +122,8 @@
                        }
                        return;
                }
-               
-               cfg.register("globaldatadir", new Freemail(), GLOBALDATADIR);
-               if (!getGlobalDataDir().exists()) {
-                       getGlobalDataDir().mkdir();
-               }
-               
-               cfg.register("datadir", new Freemail(), Freemail.DATADIR);
-               if (!getDataDir().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>");
-                       if (!getDataDir().mkdir()) {
-                               System.out.println("Couldn't create data 
directory. Please ensure that the user you are running Freemail as has write 
access to its working directory");
-                               System.exit(1);
-                       }
-               }
-               cfg.register("tempdir", new Freemail(), Freemail.TEMPDIRNAME);
-               if (!getTempDir().exists()) {
-                       if (!getTempDir().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");
-                               System.exit(1);
-                       }
-               }
-               
-               // start a SingleAccountWatcher for each account
-               File[] files = getDataDir().listFiles();
-               for (int i = 0; i < files.length; i++) {
-                       if (files[i].getName().equals(".") || 
files[i].getName().equals(".."))
-                               continue;
-                       if (!files[i].isDirectory()) continue;
-                       
-                       
if(!AccountManager.validateUsername(files[i].getName()).equals("")) {
-                               System.out.println("Account name 
"+files[i].getName()+" contains invalid chars, you may get problems accessing 
the account.");
-                       }
-                       
-                       Thread t = new Thread(new 
SingleAccountWatcher(files[i]), "Account Watcher for "+files[i].getName());
-                       t.setDaemon(true);
-                       t.start();
-               }
-               
-               // and a sender thread
-               MessageSender sender = new MessageSender(getDataDir());
-               Thread senderthread = new Thread(sender, "Message sender");
-               senderthread.setDaemon(true);
-               senderthread.start();
-               
-               // start the SMTP Listener
-               SMTPListener smtpl = new SMTPListener(sender, cfg);
-               Thread smtpthread = new Thread(smtpl, "SMTP Listener");
-               smtpthread.setDaemon(true);
-               smtpthread.start();
-               
-               // start the delayed ACK inserter
-               File ackdir = new File(getGlobalDataDir(), ACKDIR);
-               AckProcrastinator.setAckDir(ackdir);
-               AckProcrastinator ackinserter = new AckProcrastinator();
-               Thread ackinsthread = new Thread(ackinserter, "Delayed ACK 
Inserter");
-               ackinsthread.setDaemon(true);
-               ackinsthread.start();
-               
-               
-               // start the IMAP listener
-               IMAPListener imapl = new IMAPListener(cfg);
-               imapl.run();
+
+               freemail.startServers(false);
+               freemail.startWorkers(false);
        }
 }

Modified: trunk/apps/Freemail/src/freemail/FreemailPlugin.java
===================================================================
--- trunk/apps/Freemail/src/freemail/FreemailPlugin.java        2008-04-10 
20:51:51 UTC (rev 19156)
+++ trunk/apps/Freemail/src/freemail/FreemailPlugin.java        2008-04-10 
22:11:01 UTC (rev 19157)
@@ -24,18 +24,7 @@

 import java.io.File;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;

-
-import freemail.Freemail;
-import freemail.fcp.FCPContext;
-import freemail.fcp.FCPConnection;
-import freemail.imap.IMAPListener;
-import freemail.smtp.SMTPListener;
-import freemail.config.Configurator;
-import freemail.utils.Logger;
-
 import freenet.pluginmanager.FredPlugin;
 import freenet.pluginmanager.FredPluginHTTP;
 import freenet.pluginmanager.FredPluginThreadless;
@@ -48,168 +37,29 @@
 // returns rather than just continuing to run for the lifetime of the plugin.
 public class FreemailPlugin extends Freemail implements FredPlugin, 
FredPluginHTTP,
                                                         FredPluginThreadless {
-       private static PluginRespirator pr;
-       private ArrayList singleAccountWatcherList = new ArrayList();
-       private MessageSender sender;
-       private SMTPListener smtpl;
-       private AckProcrastinator ackinserter;
-       private IMAPListener imapl;
+       private PluginRespirator pluginResp;

-       private Thread fcpThread;
-       private ArrayList /* of Thread */ singleAccountWatcherThreadList = new 
ArrayList();
-       private Thread messageSenderThread;
-       private Thread smtpThread;
-       private Thread ackInserterThread;
-       private Thread imapThread;
+       public FreemailPlugin() {
+               super(CFGFILE);
+       }

        public void runPlugin(PluginRespirator pr) {
-               FreemailPlugin.pr = pr;
-               String cfgfile = CFGFILE;
-               Configurator cfg = new Configurator(new File(cfgfile));
-               FCPContext fcpctx = new FCPContext();
-
-               cfg.register("loglevel", new Logger(), "normal|error");
-
-               Logger.normal(this, "This is the Freemail plugin version 
"+VER_MAJOR+"."+VER_MINOR+" build #"+BUILD_NO+" ("+VERSION_TAG+")");
+               pluginResp = pr;

-               cfg.register("fcp_host", fcpctx, "localhost");
-               cfg.register("fcp_port", fcpctx, "9481");
-               
-               Freemail.fcpconn = new FCPConnection(fcpctx);
-               fcpThread = new Thread(fcpconn, "Freemail FCP Connection");
-               fcpThread.setDaemon(true);
-               fcpThread.start();
-               cfg.register("globaldatadir", new Freemail(), GLOBALDATADIR);
-               if (!getGlobalDataDir().exists()) {
-                       if(!getGlobalDataDir().mkdir()) {
-                               Logger.error(this,"Freemail plugin: Couldn't 
create global data directory. Please ensure that the user you are running 
Freemail as has write access to its working directory");
-                               return;
-                       }
-               }
-               cfg.register("datadir", new Freemail(), Freemail.DATADIR);
-               if (!getDataDir().exists()) {
-                       if (!getDataDir().mkdir()) {
-                               Logger.error(this,"Freemail plugin: Couldn't 
create data directory. Please ensure that the user you are running Freemail as 
has write access to its working directory");
-                               return;
-                       }
-               }
-               cfg.register("tempdir", new Freemail(), Freemail.TEMPDIRNAME);
-               if (!getTempDir().exists()) {
-                       if (!Freemail.getTempDir().mkdir()) {
-                               Logger.error(this,"Freemail plugin: Couldn't 
create temporary directory. Please ensure that the user you are running 
Freemail as has write access to its working directory");
-                               return;
-                       }
-               }
-               File[] files = getDataDir().listFiles();
-               for (int i = 0; i < files.length; i++) {
-                       if (files[i].getName().equals(".") || 
files[i].getName().equals(".."))
-                               continue;
-                       if (!files[i].isDirectory()) continue;
-
-                       String 
invalid=AccountManager.validateUsername(files[i].getName());
-                       if(!invalid.equals("")) {
-                               Logger.error(this,"Account name 
"+files[i].getName()+" contains invalid chars (\""+invalid+"\"), you may get 
problems accessing the account.");
-                       }
-                       
-                       SingleAccountWatcher saw = new 
SingleAccountWatcher(files[i]); 
-                       singleAccountWatcherList.add(saw);
-                       Thread t = new Thread(saw, "Freemail Account Watcher 
for "+files[i].getName());
-                       t.setDaemon(true);
-                       t.start();
-                       singleAccountWatcherThreadList.add(t);
-               }
-               
-               // and a sender thread
-               sender = new MessageSender(getDataDir());
-               messageSenderThread = new Thread(sender, "Freemail Message 
sender");
-               messageSenderThread.setDaemon(true);
-               messageSenderThread.start();
-               
-               // start the SMTP Listener
-               smtpl = new SMTPListener(sender, cfg);
-               smtpThread = new Thread(smtpl, "Freemail SMTP Listener");
-               smtpThread.setDaemon(true);
-               smtpThread.start();
-               
-               // start the delayed ACK inserter
-               File ackdir = new File(getGlobalDataDir(), ACKDIR);
-               AckProcrastinator.setAckDir(ackdir);
-               ackinserter = new AckProcrastinator();
-               ackInserterThread = new Thread(ackinserter, "Freemail Delayed 
ACK Inserter");
-               ackInserterThread.setDaemon(true);
-               ackInserterThread.start();
-               
-               // start the IMAP listener
-               imapl = new IMAPListener(cfg);
-               imapThread = new Thread(imapl, "Freemail IMAP Listener");
-               imapThread.setDaemon(true);
-               imapThread.start();
+               startFcp(true);
+               startServers(true);
+               startWorkers(true);
        }

-       public void terminate() {
-               Iterator it = singleAccountWatcherList.iterator();
-               while(it.hasNext()) {
-                       ((SingleAccountWatcher)it.next()).kill();
-                       it.remove();
-               }
-
-               sender.kill();
-               ackinserter.kill();
-               smtpl.kill();
-               imapl.kill();
-               // now kill the FCP thread - that's what all the other threads 
will be waiting on
-               fcpconn.kill();
-               
-               // now clean up all the threads
-               boolean cleanedUp = false;
-               while (!cleanedUp) {
-                       try {
-                               it = singleAccountWatcherThreadList.iterator();
-                               while(it.hasNext()) {
-                                       ((Thread)it.next()).join();
-                                       it.remove();
-                               }
-                               
-                               if (messageSenderThread != null) {
-                                       messageSenderThread.join();
-                                       messageSenderThread = null;
-                               }
-                               if (ackInserterThread != null) {
-                                       ackInserterThread.join();
-                                       ackInserterThread = null;
-                               }
-                               if (smtpThread != null) {
-                                       smtpThread.join();
-                                       smtpl.joinClientThreads();
-                                       smtpThread = null;
-                               }
-                               if (imapThread != null) {
-                                       imapThread.join();
-                                       imapl.joinClientThreads();
-                                       imapThread = null;
-                               }
-                               if (fcpThread != null) {
-                                       fcpThread.join();
-                                       fcpThread = null;
-                               }
-                       } catch (InterruptedException ie) {
-                               
-                       }
-                       cleanedUp = true;
-               }
-               
-               return;
-       }
-
        public String handleHTTPGet(HTTPRequest request) throws 
PluginHTTPException {
-               HTMLNode pageNode = pr.getPageMaker().getPageNode("Freemail 
plugin", false, null);
-               HTMLNode contentNode = 
pr.getPageMaker().getContentNode(pageNode);
+               HTMLNode pageNode = 
pluginResp.getPageMaker().getPageNode("Freemail plugin", false, null);
+               HTMLNode contentNode = 
pluginResp.getPageMaker().getContentNode(pageNode);

                HTMLNode addBox = contentNode.addChild("div", "class", 
"infobox");
                addBox.addChild("div", "class", "infobox-header", "Add 
account");

                HTMLNode boxContent = addBox.addChild("div", "class", 
"infobox-content");
-               HTMLNode form = pr.addFormChild(boxContent, "", 
"addAccountForm");
+               HTMLNode form = pluginResp.addFormChild(boxContent, "", 
"addAccountForm");

                HTMLNode table = form.addChild("table", "class", "plugintable");
                HTMLNode tableRowName = table.addChild("tr");
@@ -229,8 +79,8 @@
        }

        public String handleHTTPPost(HTTPRequest request) throws 
PluginHTTPException {
-               HTMLNode pageNode = pr.getPageMaker().getPageNode("Freemail 
plugin", false, null);
-               HTMLNode contentNode = 
pr.getPageMaker().getContentNode(pageNode);
+               HTMLNode pageNode = 
pluginResp.getPageMaker().getPageNode("Freemail plugin", false, null);
+               HTMLNode contentNode = 
pluginResp.getPageMaker().getContentNode(pageNode);

                String add = request.getPartAsString("add", 100);
                String name = request.getPartAsString("name", 100);
@@ -295,5 +145,4 @@
        public String handleHTTPPut(HTTPRequest request) throws 
PluginHTTPException {
                return null;
        }
-
 }


Reply via email to