org/sonews/Main.java
author cli
Thu Aug 20 18:41:21 2009 +0200 (2009-08-20)
changeset 15 f2293e8566f5
parent 3 2fdc9cc89502
child 21 4b2c8bedb094
permissions -rw-r--r--
Started refactoring the Log system to use java.util.logging classes.
     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;
    20 
    21 import java.sql.Driver;
    22 import java.sql.DriverManager;
    23 import java.util.Enumeration;
    24 import java.util.Date;
    25 import org.sonews.config.Config;
    26 import org.sonews.daemon.ChannelLineBuffers;
    27 import org.sonews.daemon.Connections;
    28 import org.sonews.daemon.NNTPDaemon;
    29 import org.sonews.feed.FeedManager;
    30 import org.sonews.mlgw.MailPoller;
    31 import org.sonews.storage.StorageBackendException;
    32 import org.sonews.storage.StorageManager;
    33 import org.sonews.storage.StorageProvider;
    34 import org.sonews.util.Log;
    35 import org.sonews.util.Purger;
    36 import org.sonews.util.io.Resource;
    37 
    38 /**
    39  * Startup class of the daemon.
    40  * @author Christian Lins
    41  * @since sonews/0.5.0
    42  */
    43 public final class Main
    44 {
    45   
    46   private Main()
    47   {
    48   }
    49 
    50   /** Version information of the sonews daemon */
    51   public static final String VERSION = "sonews/1.1.0";
    52   public static final Date   STARTDATE = new Date();
    53   
    54   /**
    55    * The main entrypoint.
    56    * @param args
    57    * @throws Exception
    58    */
    59   public static void main(String[] args) throws Exception
    60   {
    61     System.out.println(VERSION);
    62     Thread.currentThread().setName("Mainthread");
    63 
    64     // Command line arguments
    65     boolean feed    = false;  // Enable feeding?
    66     boolean mlgw    = false;  // Enable Mailinglist gateway?
    67     int     port    = -1;
    68     
    69     for(int n = 0; n < args.length; n++)
    70     {
    71       if(args[n].equals("-c") || args[n].equals("-config"))
    72       {
    73         Config.inst().set(Config.LEVEL_CLI, Config.CONFIGFILE, args[++n]);
    74         System.out.println("Using config file " + args[n]);
    75       }
    76       else if(args[n].equals("-dumpjdbcdriver"))
    77       {
    78         System.out.println("Available JDBC drivers:");
    79         Enumeration<Driver> drvs =  DriverManager.getDrivers();
    80         while(drvs.hasMoreElements())
    81         {
    82           System.out.println(drvs.nextElement());
    83         }
    84         return;
    85       }
    86       else if(args[n].equals("-feed"))
    87       {
    88         feed = true;
    89       }
    90       else if(args[n].equals("-h") || args[n].equals("-help"))
    91       {
    92         printArguments();
    93         return;
    94       }
    95       else if(args[n].equals("-mlgw"))
    96       {
    97         mlgw = true;
    98       }
    99       else if(args[n].equals("-p"))
   100       {
   101         port = Integer.parseInt(args[++n]);
   102       }
   103       else if(args[n].equals("-v") || args[n].equals("-version"))
   104       {
   105         // Simply return as the version info is already printed above
   106         return;
   107       }
   108     }
   109     
   110     // Try to load the JDBCDatabase;
   111     // Do NOT USE BackendConfig or Log classes before this point because they require
   112     // a working JDBCDatabase connection.
   113     try
   114     {
   115       StorageProvider sprov =
   116         StorageManager.loadProvider("org.sonews.storage.impl.JDBCDatabaseProvider");
   117       StorageManager.enableProvider(sprov);
   118       
   119       // Make sure some elementary groups are existing
   120       if(!StorageManager.current().isGroupExisting("control"))
   121       {
   122         StorageManager.current().addGroup("control", 0);
   123         Log.get().info("Group 'control' created.");
   124       }
   125     }
   126     catch(StorageBackendException ex)
   127     {
   128       ex.printStackTrace();
   129       System.err.println("Database initialization failed with " + ex.toString());
   130       System.err.println("Make sure you have specified the correct database" +
   131         " settings in sonews.conf!");
   132       return;
   133     }
   134     
   135     ChannelLineBuffers.allocateDirect();
   136     
   137     // Add shutdown hook
   138     Runtime.getRuntime().addShutdownHook(new ShutdownHook());
   139     
   140     // Start the listening daemon
   141     if(port <= 0)
   142     {
   143       port = Config.inst().get(Config.PORT, 119);
   144     }
   145     final NNTPDaemon daemon = NNTPDaemon.createInstance(port);
   146     daemon.start();
   147     
   148     // Start Connections purger thread...
   149     Connections.getInstance().start();
   150     
   151     // Start mailinglist gateway...
   152     if(mlgw)
   153     {
   154       new MailPoller().start();
   155     }
   156     
   157     // Start feeds
   158     if(feed)
   159     {
   160       FeedManager.startFeeding();
   161     }
   162 
   163     Purger purger = new Purger();
   164     purger.start();
   165     
   166     // Wait for main thread to exit (setDaemon(false))
   167     daemon.join();
   168   }
   169   
   170   private static void printArguments()
   171   {
   172     String usage = Resource.getAsString("helpers/usage", true);
   173     System.out.println(usage);
   174   }
   175 
   176 }