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