chris@3: /*
chris@3: * SONEWS News Server
chris@3: * see AUTHORS for the list of contributors
chris@3: *
chris@3: * This program is free software: you can redistribute it and/or modify
chris@3: * it under the terms of the GNU General Public License as published by
chris@3: * the Free Software Foundation, either version 3 of the License, or
chris@3: * (at your option) any later version.
chris@3: *
chris@3: * This program is distributed in the hope that it will be useful,
chris@3: * but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
chris@3: * GNU General Public License for more details.
chris@3: *
chris@3: * You should have received a copy of the GNU General Public License
chris@3: * along with this program. If not, see .
chris@3: */
chris@3: package org.sonews;
chris@3:
chris@3: import java.sql.Driver;
chris@3: import java.sql.DriverManager;
chris@3: import java.util.Enumeration;
chris@3: import java.util.Date;
cli@21: import java.util.logging.Level;
chris@3: import org.sonews.config.Config;
chris@3: import org.sonews.daemon.ChannelLineBuffers;
cli@21: import org.sonews.daemon.CommandSelector;
chris@3: import org.sonews.daemon.Connections;
chris@3: import org.sonews.daemon.NNTPDaemon;
chris@3: import org.sonews.feed.FeedManager;
chris@3: import org.sonews.mlgw.MailPoller;
chris@3: import org.sonews.storage.StorageBackendException;
chris@3: import org.sonews.storage.StorageManager;
chris@3: import org.sonews.storage.StorageProvider;
chris@3: import org.sonews.util.Log;
chris@3: import org.sonews.util.Purger;
chris@3: import org.sonews.util.io.Resource;
chris@3:
chris@3: /**
chris@3: * Startup class of the daemon.
chris@3: * @author Christian Lins
chris@3: * @since sonews/0.5.0
chris@3: */
cli@42: public final class Main {
chris@3:
cli@37: /** Version information of the sonews daemon */
cli@37: public static final String VERSION = "sonews/1.1.0";
cli@37: public static final Date STARTDATE = new Date();
chris@3:
cli@37: /**
cli@37: * The main entrypoint.
cli@37: * @param args
cli@37: * @throws Exception
cli@37: */
cli@42: public static void main(String[] args) throws Exception {
cli@37: System.out.println(VERSION);
cli@37: Thread.currentThread().setName("Mainthread");
chris@3:
cli@37: // Command line arguments
cli@37: boolean feed = false; // Enable feeding?
cli@37: boolean mlgw = false; // Enable Mailinglist gateway?
cli@37: int port = -1;
chris@3:
cli@37: for (int n = 0; n < args.length; n++) {
cli@37: if (args[n].equals("-c") || args[n].equals("-config")) {
cli@37: Config.inst().set(Config.LEVEL_CLI, Config.CONFIGFILE, args[++n]);
cli@37: System.out.println("Using config file " + args[n]);
cli@37: } else if (args[n].equals("-dumpjdbcdriver")) {
cli@37: System.out.println("Available JDBC drivers:");
cli@37: Enumeration drvs = DriverManager.getDrivers();
cli@37: while (drvs.hasMoreElements()) {
cli@37: System.out.println(drvs.nextElement());
cli@37: }
cli@37: return;
cli@37: } else if (args[n].equals("-feed")) {
cli@37: feed = true;
cli@37: } else if (args[n].equals("-h") || args[n].equals("-help")) {
cli@37: printArguments();
cli@37: return;
cli@37: } else if (args[n].equals("-mlgw")) {
cli@37: mlgw = true;
cli@37: } else if (args[n].equals("-p")) {
cli@37: port = Integer.parseInt(args[++n]);
cli@37: } else if (args[n].equals("-plugin")) {
cli@37: System.out.println("Warning: -plugin-storage is not implemented!");
cli@37: } else if (args[n].equals("-plugin-command")) {
cli@37: try {
cli@37: CommandSelector.addCommandHandler(args[++n]);
cli@37: } catch (Exception ex) {
cli@37: Log.get().warning("Could not load command plugin: " + args[n]);
cli@37: Log.get().log(Level.INFO, "Main.java", ex);
cli@37: }
cli@37: } else if (args[n].equals("-plugin-storage")) {
cli@37: System.out.println("Warning: -plugin-storage is not implemented!");
cli@37: } else if (args[n].equals("-v") || args[n].equals("-version")) {
cli@37: // Simply return as the version info is already printed above
cli@37: return;
cli@37: }
cli@37: }
cli@37:
cli@37: // Try to load the JDBCDatabase;
cli@37: // Do NOT USE BackendConfig or Log classes before this point because they require
cli@37: // a working JDBCDatabase connection.
cli@37: try {
cli@45: String provName = Config.inst().get(Config.LEVEL_FILE,
cli@45: Config.STORAGE_PROVIDER, "org.sonews.storage.impl.JDBCDatabaseProvider");
cli@45: StorageProvider sprov = StorageManager.loadProvider(provName);
cli@37: StorageManager.enableProvider(sprov);
cli@37:
cli@37: // Make sure some elementary groups are existing
cli@37: if (!StorageManager.current().isGroupExisting("control")) {
cli@37: StorageManager.current().addGroup("control", 0);
cli@37: Log.get().info("Group 'control' created.");
cli@37: }
cli@37: } catch (StorageBackendException ex) {
cli@37: ex.printStackTrace();
cli@37: System.err.println("Database initialization failed with " + ex.toString());
cli@37: System.err.println("Make sure you have specified the correct database"
cli@42: + " settings in sonews.conf!");
cli@37: return;
cli@37: }
cli@37:
cli@37: ChannelLineBuffers.allocateDirect();
cli@37:
cli@37: // Add shutdown hook
cli@37: Runtime.getRuntime().addShutdownHook(new ShutdownHook());
cli@37:
cli@37: // Start the listening daemon
cli@37: if (port <= 0) {
cli@37: port = Config.inst().get(Config.PORT, 119);
cli@37: }
cli@37: final NNTPDaemon daemon = NNTPDaemon.createInstance(port);
cli@37: daemon.start();
cli@37:
cli@37: // Start Connections purger thread...
cli@37: Connections.getInstance().start();
cli@37:
cli@37: // Start mailinglist gateway...
cli@37: if (mlgw) {
cli@37: new MailPoller().start();
cli@37: }
cli@37:
cli@37: // Start feeds
cli@37: if (feed) {
cli@37: FeedManager.startFeeding();
cli@37: }
cli@37:
cli@37: Purger purger = new Purger();
cli@37: purger.start();
cli@37:
cli@37: // Wait for main thread to exit (setDaemon(false))
cli@37: daemon.join();
cli@37: }
cli@37:
cli@42: private static void printArguments() {
cli@37: String usage = Resource.getAsString("helpers/usage", true);
cli@37: System.out.println(usage);
cli@37: }
cli@37:
cli@42: private Main() {
cli@37: }
chris@3: }