org/sonews/daemon/Main.java
author chris <chris@marvin>
Wed Jul 01 10:48:22 2009 +0200 (2009-07-01)
changeset 2 1090e2141798
parent 1 6fceb66e1ad7
permissions -rw-r--r--
sonews/0.5.1 fixes merged
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.daemon;
    20 
    21 import java.sql.Driver;
    22 import java.sql.DriverManager;
    23 import java.sql.SQLException;
    24 import java.util.Enumeration;
    25 import java.util.Date;
    26 import org.sonews.feed.FeedManager;
    27 import org.sonews.mlgw.MailPoller;
    28 import org.sonews.daemon.storage.Database;
    29 import org.sonews.util.Log;
    30 import org.sonews.util.io.Resource;
    31 
    32 /**
    33  * Startup class of the daemon.
    34  * @author Christian Lins
    35  * @since sonews/0.5.0
    36  */
    37 public final class Main
    38 {
    39   
    40   private Main()
    41   {
    42   }
    43 
    44   /** Version information of the sonews daemon */
    45   public static final String VERSION = "sonews/0.6.0beta1";
    46   public static final Date   STARTDATE = new Date();
    47   
    48   /**
    49    * The main entrypoint.
    50    * @param args
    51    * @throws Exception
    52    */
    53   public static void main(String[] args) throws Exception
    54   {
    55     System.out.println(VERSION);
    56     Thread.currentThread().setName("Mainthread");
    57 
    58     // Command line arguments
    59     boolean feed    = false;  // Enable feeding?
    60     boolean mlgw    = false;  // Enable Mailinglist gateway?
    61     int     port    = -1;
    62     
    63     for(int n = 0; n < args.length; n++)
    64     {
    65       if(args[n].equals("-c") || args[n].equals("-config"))
    66       {
    67         BootstrapConfig.FILE = args[++n];
    68         System.out.println("Using config file " + args[n]);
    69       }
    70       else if(args[n].equals("-dumpjdbcdriver"))
    71       {
    72         System.out.println("Available JDBC drivers:");
    73         Enumeration<Driver> drvs =  DriverManager.getDrivers();
    74         while(drvs.hasMoreElements())
    75         {
    76           System.out.println(drvs.nextElement());
    77         }
    78         return;
    79       }
    80       else if(args[n].equals("-feed"))
    81       {
    82         feed = true;
    83       }
    84       else if(args[n].equals("-h") || args[n].equals("-help"))
    85       {
    86         printArguments();
    87         return;
    88       }
    89       else if(args[n].equals("-mlgw"))
    90       {
    91         mlgw = true;
    92       }
    93       else if(args[n].equals("-p"))
    94       {
    95         port = Integer.parseInt(args[++n]);
    96       }
    97     }
    98     
    99     // Try to load the Database;
   100     // Do NOT USE Config or Log classes before this point because they require
   101     // a working Database connection.
   102     try
   103     {
   104       Database.getInstance();
   105       
   106       // Make sure some elementary groups are existing
   107       if(!Database.getInstance().isGroupExisting("control"))
   108       {
   109         Database.getInstance().addGroup("control", 0);
   110         Log.msg("Group 'control' created.", true);
   111       }
   112     }
   113     catch(SQLException ex)
   114     {
   115       ex.printStackTrace();
   116       System.err.println("Database initialization failed with " + ex.toString());
   117       System.err.println("Make sure you have specified the correct database" +
   118         " settings in sonews.conf!");
   119       return;
   120     }
   121     
   122     ChannelLineBuffers.allocateDirect();
   123     
   124     // Add shutdown hook
   125     Runtime.getRuntime().addShutdownHook(new ShutdownHook());
   126     
   127     // Start the listening daemon
   128     if(port <= 0)
   129     {
   130       port = Config.getInstance().get(Config.PORT, 119);
   131     }
   132     final NNTPDaemon daemon = NNTPDaemon.createInstance(port);
   133     daemon.start();
   134     
   135     // Start Connections purger thread...
   136     Connections.getInstance().start();
   137     
   138     // Start mailinglist gateway...
   139     if(mlgw)
   140     {
   141       new MailPoller().start();
   142     }
   143     
   144     // Start feeds
   145     if(feed)
   146     {
   147       FeedManager.startFeeding();
   148     }
   149     
   150     // Wait for main thread to exit (setDaemon(false))
   151     daemon.join();
   152   }
   153   
   154   private static void printArguments()
   155   {
   156     String usage = Resource.getAsString("helpers/usage", true);
   157     System.out.println(usage);
   158   }
   159 
   160 }