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