src/org/sonews/Main.java
author cli
Wed, 04 May 2011 18:06:24 +0200
changeset 40 f8894df2d079
parent 37 74139325d305
child 42 7f84f4de2893
permissions -rwxr-xr-x
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;
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
cli@37
    48
	/** Version information of the sonews daemon */
cli@37
    49
	public static final String VERSION = "sonews/1.1.0";
cli@37
    50
	public static final Date STARTDATE = new Date();
chris@3
    51
cli@37
    52
	/**
cli@37
    53
	 * The main entrypoint.
cli@37
    54
	 * @param args
cli@37
    55
	 * @throws Exception
cli@37
    56
	 */
cli@37
    57
	public static void main(String[] args) throws Exception
cli@37
    58
	{
cli@37
    59
		System.out.println(VERSION);
cli@37
    60
		Thread.currentThread().setName("Mainthread");
chris@3
    61
cli@37
    62
		// Command line arguments
cli@37
    63
		boolean feed = false;  // Enable feeding?
cli@37
    64
		boolean mlgw = false;  // Enable Mailinglist gateway?
cli@37
    65
		int port = -1;
chris@3
    66
cli@37
    67
		for (int n = 0; n < args.length; n++) {
cli@37
    68
			if (args[n].equals("-c") || args[n].equals("-config")) {
cli@37
    69
				Config.inst().set(Config.LEVEL_CLI, Config.CONFIGFILE, args[++n]);
cli@37
    70
				System.out.println("Using config file " + args[n]);
cli@37
    71
			} else if (args[n].equals("-dumpjdbcdriver")) {
cli@37
    72
				System.out.println("Available JDBC drivers:");
cli@37
    73
				Enumeration<Driver> drvs = DriverManager.getDrivers();
cli@37
    74
				while (drvs.hasMoreElements()) {
cli@37
    75
					System.out.println(drvs.nextElement());
cli@37
    76
				}
cli@37
    77
				return;
cli@37
    78
			} else if (args[n].equals("-feed")) {
cli@37
    79
				feed = true;
cli@37
    80
			} else if (args[n].equals("-h") || args[n].equals("-help")) {
cli@37
    81
				printArguments();
cli@37
    82
				return;
cli@37
    83
			} else if (args[n].equals("-mlgw")) {
cli@37
    84
				mlgw = true;
cli@37
    85
			} else if (args[n].equals("-p")) {
cli@37
    86
				port = Integer.parseInt(args[++n]);
cli@37
    87
			} else if (args[n].equals("-plugin")) {
cli@37
    88
				System.out.println("Warning: -plugin-storage is not implemented!");
cli@37
    89
			} else if (args[n].equals("-plugin-command")) {
cli@37
    90
				try {
cli@37
    91
					CommandSelector.addCommandHandler(args[++n]);
cli@37
    92
				} catch (Exception ex) {
cli@37
    93
					Log.get().warning("Could not load command plugin: " + args[n]);
cli@37
    94
					Log.get().log(Level.INFO, "Main.java", ex);
cli@37
    95
				}
cli@37
    96
			} else if (args[n].equals("-plugin-storage")) {
cli@37
    97
				System.out.println("Warning: -plugin-storage is not implemented!");
cli@37
    98
			} else if (args[n].equals("-v") || args[n].equals("-version")) {
cli@37
    99
				// Simply return as the version info is already printed above
cli@37
   100
				return;
cli@37
   101
			}
cli@37
   102
		}
cli@37
   103
cli@37
   104
		// Try to load the JDBCDatabase;
cli@37
   105
		// Do NOT USE BackendConfig or Log classes before this point because they require
cli@37
   106
		// a working JDBCDatabase connection.
cli@37
   107
		try {
cli@37
   108
			StorageProvider sprov =
cli@37
   109
				StorageManager.loadProvider("org.sonews.storage.impl.JDBCDatabaseProvider");
cli@37
   110
			StorageManager.enableProvider(sprov);
cli@37
   111
cli@37
   112
			// Make sure some elementary groups are existing
cli@37
   113
			if (!StorageManager.current().isGroupExisting("control")) {
cli@37
   114
				StorageManager.current().addGroup("control", 0);
cli@37
   115
				Log.get().info("Group 'control' created.");
cli@37
   116
			}
cli@37
   117
		} catch (StorageBackendException ex) {
cli@37
   118
			ex.printStackTrace();
cli@37
   119
			System.err.println("Database initialization failed with " + ex.toString());
cli@37
   120
			System.err.println("Make sure you have specified the correct database"
cli@37
   121
				+ " settings in sonews.conf!");
cli@37
   122
			return;
cli@37
   123
		}
cli@37
   124
cli@37
   125
		ChannelLineBuffers.allocateDirect();
cli@37
   126
cli@37
   127
		// Add shutdown hook
cli@37
   128
		Runtime.getRuntime().addShutdownHook(new ShutdownHook());
cli@37
   129
cli@37
   130
		// Start the listening daemon
cli@37
   131
		if (port <= 0) {
cli@37
   132
			port = Config.inst().get(Config.PORT, 119);
cli@37
   133
		}
cli@37
   134
		final NNTPDaemon daemon = NNTPDaemon.createInstance(port);
cli@37
   135
		daemon.start();
cli@37
   136
cli@37
   137
		// Start Connections purger thread...
cli@37
   138
		Connections.getInstance().start();
cli@37
   139
cli@37
   140
		// Start mailinglist gateway...
cli@37
   141
		if (mlgw) {
cli@37
   142
			new MailPoller().start();
cli@37
   143
		}
cli@37
   144
cli@37
   145
		// Start feeds
cli@37
   146
		if (feed) {
cli@37
   147
			FeedManager.startFeeding();
cli@37
   148
		}
cli@37
   149
cli@37
   150
		Purger purger = new Purger();
cli@37
   151
		purger.start();
cli@37
   152
cli@37
   153
		// Wait for main thread to exit (setDaemon(false))
cli@37
   154
		daemon.join();
cli@37
   155
	}
cli@37
   156
cli@37
   157
	private static void printArguments()
cli@37
   158
	{
cli@37
   159
		String usage = Resource.getAsString("helpers/usage", true);
cli@37
   160
		System.out.println(usage);
cli@37
   161
	}
cli@37
   162
cli@37
   163
	private Main()
cli@37
   164
	{
cli@37
   165
	}
chris@3
   166
}