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