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