org/sonews/Main.java
changeset 3 2fdc9cc89502
child 15 f2293e8566f5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/Main.java	Wed Jul 22 14:04:05 2009 +0200
     1.3 @@ -0,0 +1,176 @@
     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;
    1.23 +
    1.24 +import java.sql.Driver;
    1.25 +import java.sql.DriverManager;
    1.26 +import java.util.Enumeration;
    1.27 +import java.util.Date;
    1.28 +import org.sonews.config.Config;
    1.29 +import org.sonews.daemon.ChannelLineBuffers;
    1.30 +import org.sonews.daemon.Connections;
    1.31 +import org.sonews.daemon.NNTPDaemon;
    1.32 +import org.sonews.feed.FeedManager;
    1.33 +import org.sonews.mlgw.MailPoller;
    1.34 +import org.sonews.storage.StorageBackendException;
    1.35 +import org.sonews.storage.StorageManager;
    1.36 +import org.sonews.storage.StorageProvider;
    1.37 +import org.sonews.util.Log;
    1.38 +import org.sonews.util.Purger;
    1.39 +import org.sonews.util.io.Resource;
    1.40 +
    1.41 +/**
    1.42 + * Startup class of the daemon.
    1.43 + * @author Christian Lins
    1.44 + * @since sonews/0.5.0
    1.45 + */
    1.46 +public final class Main
    1.47 +{
    1.48 +  
    1.49 +  private Main()
    1.50 +  {
    1.51 +  }
    1.52 +
    1.53 +  /** Version information of the sonews daemon */
    1.54 +  public static final String VERSION = "sonews/1.0.0";
    1.55 +  public static final Date   STARTDATE = new Date();
    1.56 +  
    1.57 +  /**
    1.58 +   * The main entrypoint.
    1.59 +   * @param args
    1.60 +   * @throws Exception
    1.61 +   */
    1.62 +  public static void main(String[] args) throws Exception
    1.63 +  {
    1.64 +    System.out.println(VERSION);
    1.65 +    Thread.currentThread().setName("Mainthread");
    1.66 +
    1.67 +    // Command line arguments
    1.68 +    boolean feed    = false;  // Enable feeding?
    1.69 +    boolean mlgw    = false;  // Enable Mailinglist gateway?
    1.70 +    int     port    = -1;
    1.71 +    
    1.72 +    for(int n = 0; n < args.length; n++)
    1.73 +    {
    1.74 +      if(args[n].equals("-c") || args[n].equals("-config"))
    1.75 +      {
    1.76 +        Config.inst().set(Config.LEVEL_CLI, Config.CONFIGFILE, args[++n]);
    1.77 +        System.out.println("Using config file " + args[n]);
    1.78 +      }
    1.79 +      else if(args[n].equals("-dumpjdbcdriver"))
    1.80 +      {
    1.81 +        System.out.println("Available JDBC drivers:");
    1.82 +        Enumeration<Driver> drvs =  DriverManager.getDrivers();
    1.83 +        while(drvs.hasMoreElements())
    1.84 +        {
    1.85 +          System.out.println(drvs.nextElement());
    1.86 +        }
    1.87 +        return;
    1.88 +      }
    1.89 +      else if(args[n].equals("-feed"))
    1.90 +      {
    1.91 +        feed = true;
    1.92 +      }
    1.93 +      else if(args[n].equals("-h") || args[n].equals("-help"))
    1.94 +      {
    1.95 +        printArguments();
    1.96 +        return;
    1.97 +      }
    1.98 +      else if(args[n].equals("-mlgw"))
    1.99 +      {
   1.100 +        mlgw = true;
   1.101 +      }
   1.102 +      else if(args[n].equals("-p"))
   1.103 +      {
   1.104 +        port = Integer.parseInt(args[++n]);
   1.105 +      }
   1.106 +      else if(args[n].equals("-v") || args[n].equals("-version"))
   1.107 +      {
   1.108 +        // Simply return as the version info is already printed above
   1.109 +        return;
   1.110 +      }
   1.111 +    }
   1.112 +    
   1.113 +    // Try to load the JDBCDatabase;
   1.114 +    // Do NOT USE BackendConfig or Log classes before this point because they require
   1.115 +    // a working JDBCDatabase connection.
   1.116 +    try
   1.117 +    {
   1.118 +      StorageProvider sprov =
   1.119 +        StorageManager.loadProvider("org.sonews.storage.impl.JDBCDatabaseProvider");
   1.120 +      StorageManager.enableProvider(sprov);
   1.121 +      
   1.122 +      // Make sure some elementary groups are existing
   1.123 +      if(!StorageManager.current().isGroupExisting("control"))
   1.124 +      {
   1.125 +        StorageManager.current().addGroup("control", 0);
   1.126 +        Log.msg("Group 'control' created.", true);
   1.127 +      }
   1.128 +    }
   1.129 +    catch(StorageBackendException ex)
   1.130 +    {
   1.131 +      ex.printStackTrace();
   1.132 +      System.err.println("Database initialization failed with " + ex.toString());
   1.133 +      System.err.println("Make sure you have specified the correct database" +
   1.134 +        " settings in sonews.conf!");
   1.135 +      return;
   1.136 +    }
   1.137 +    
   1.138 +    ChannelLineBuffers.allocateDirect();
   1.139 +    
   1.140 +    // Add shutdown hook
   1.141 +    Runtime.getRuntime().addShutdownHook(new ShutdownHook());
   1.142 +    
   1.143 +    // Start the listening daemon
   1.144 +    if(port <= 0)
   1.145 +    {
   1.146 +      port = Config.inst().get(Config.PORT, 119);
   1.147 +    }
   1.148 +    final NNTPDaemon daemon = NNTPDaemon.createInstance(port);
   1.149 +    daemon.start();
   1.150 +    
   1.151 +    // Start Connections purger thread...
   1.152 +    Connections.getInstance().start();
   1.153 +    
   1.154 +    // Start mailinglist gateway...
   1.155 +    if(mlgw)
   1.156 +    {
   1.157 +      new MailPoller().start();
   1.158 +    }
   1.159 +    
   1.160 +    // Start feeds
   1.161 +    if(feed)
   1.162 +    {
   1.163 +      FeedManager.startFeeding();
   1.164 +    }
   1.165 +
   1.166 +    Purger purger = new Purger();
   1.167 +    purger.start();
   1.168 +    
   1.169 +    // Wait for main thread to exit (setDaemon(false))
   1.170 +    daemon.join();
   1.171 +  }
   1.172 +  
   1.173 +  private static void printArguments()
   1.174 +  {
   1.175 +    String usage = Resource.getAsString("helpers/usage", true);
   1.176 +    System.out.println(usage);
   1.177 +  }
   1.178 +
   1.179 +}