org/sonews/daemon/Main.java
changeset 1 6fceb66e1ad7
child 2 1090e2141798
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/daemon/Main.java	Fri Jun 26 16:48:50 2009 +0200
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.daemon;
    1.23 +
    1.24 +import java.sql.Driver;
    1.25 +import java.sql.DriverManager;
    1.26 +import java.sql.SQLException;
    1.27 +import java.util.Enumeration;
    1.28 +import java.util.Date;
    1.29 +import org.sonews.feed.FeedManager;
    1.30 +import org.sonews.mlgw.MailPoller;
    1.31 +import org.sonews.daemon.storage.Database;
    1.32 +import org.sonews.util.Log;
    1.33 +import org.sonews.util.io.Resource;
    1.34 +
    1.35 +/**
    1.36 + * Startup class of the daemon.
    1.37 + * @author Christian Lins
    1.38 + * @since sonews/0.5.0
    1.39 + */
    1.40 +public final class Main
    1.41 +{
    1.42 +  
    1.43 +  private Main()
    1.44 +  {
    1.45 +  }
    1.46 +
    1.47 +  /** Version information of the sonews daemon */
    1.48 +  public static final String VERSION = "sonews/0.5.0";
    1.49 +  public static final Date   STARTDATE = new Date();
    1.50 +  
    1.51 +  /**
    1.52 +   * The main entrypoint.
    1.53 +   * @param args
    1.54 +   * @throws Exception
    1.55 +   */
    1.56 +  public static void main(String[] args) throws Exception
    1.57 +  {
    1.58 +    System.out.println(VERSION);
    1.59 +    Thread.currentThread().setName("Mainthread");
    1.60 +
    1.61 +    // Command line arguments
    1.62 +    boolean feed    = false;  // Enable feeding?
    1.63 +    boolean mlgw    = false;  // Enable Mailinglist gateway?
    1.64 +    int     port    = -1;
    1.65 +    
    1.66 +    for(int n = 0; n < args.length; n++)
    1.67 +    {
    1.68 +      if(args[n].equals("-c") || args[n].equals("-config"))
    1.69 +      {
    1.70 +        BootstrapConfig.FILE = args[++n];
    1.71 +        System.out.println("Using config file " + args[n]);
    1.72 +      }
    1.73 +      else if(args[n].equals("-dumpjdbcdriver"))
    1.74 +      {
    1.75 +        System.out.println("Available JDBC drivers:");
    1.76 +        Enumeration<Driver> drvs =  DriverManager.getDrivers();
    1.77 +        while(drvs.hasMoreElements())
    1.78 +        {
    1.79 +          System.out.println(drvs.nextElement());
    1.80 +        }
    1.81 +        return;
    1.82 +      }
    1.83 +      else if(args[n].equals("-feed"))
    1.84 +      {
    1.85 +        feed = true;
    1.86 +      }
    1.87 +      else if(args[n].equals("-h") || args[n].equals("-help"))
    1.88 +      {
    1.89 +        printArguments();
    1.90 +        return;
    1.91 +      }
    1.92 +      else if(args[n].equals("-mlgw"))
    1.93 +      {
    1.94 +        mlgw = true;
    1.95 +      }
    1.96 +      else if(args[n].equals("-p"))
    1.97 +      {
    1.98 +        port = Integer.parseInt(args[++n]);
    1.99 +      }
   1.100 +    }
   1.101 +    
   1.102 +    // Try to load the Database;
   1.103 +    // Do NOT USE Config or Log classes before this point because they require
   1.104 +    // a working Database connection.
   1.105 +    try
   1.106 +    {
   1.107 +      Database.getInstance();
   1.108 +      
   1.109 +      // Make sure some elementary groups are existing
   1.110 +      if(!Database.getInstance().isGroupExisting("control"))
   1.111 +      {
   1.112 +        Database.getInstance().addGroup("control", 0);
   1.113 +        Log.msg("Group 'control' created.", true);
   1.114 +      }
   1.115 +    }
   1.116 +    catch(SQLException ex)
   1.117 +    {
   1.118 +      ex.printStackTrace();
   1.119 +      System.err.println("Database initialization failed with " + ex.toString());
   1.120 +      System.err.println("Make sure you have specified the correct database" +
   1.121 +        " settings in sonews.conf!");
   1.122 +      return;
   1.123 +    }
   1.124 +    
   1.125 +    ChannelLineBuffers.allocateDirect();
   1.126 +    
   1.127 +    // Add shutdown hook
   1.128 +    Runtime.getRuntime().addShutdownHook(new ShutdownHook());
   1.129 +    
   1.130 +    // Start the listening daemon
   1.131 +    if(port <= 0)
   1.132 +    {
   1.133 +      port = Config.getInstance().get(Config.PORT, 119);
   1.134 +    }
   1.135 +    final NNTPDaemon daemon = NNTPDaemon.createInstance(port);
   1.136 +    daemon.start();
   1.137 +    
   1.138 +    // Start Connections purger thread...
   1.139 +    Connections.getInstance().start();
   1.140 +    
   1.141 +    // Start mailinglist gateway...
   1.142 +    if(mlgw)
   1.143 +    {
   1.144 +      new MailPoller().start();
   1.145 +    }
   1.146 +    
   1.147 +    // Start feeds
   1.148 +    if(feed)
   1.149 +    {
   1.150 +      FeedManager.startFeeding();
   1.151 +    }
   1.152 +    
   1.153 +    // Wait for main thread to exit (setDaemon(false))
   1.154 +    daemon.join();
   1.155 +  }
   1.156 +  
   1.157 +  private static void printArguments()
   1.158 +  {
   1.159 +    String usage = Resource.getAsString("helpers/usage", true);
   1.160 +    System.out.println(usage);
   1.161 +  }
   1.162 +
   1.163 +}