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