src/org/sonews/Main.java
author cli
Tue, 07 Jun 2011 11:55:22 +0200
changeset 44 5d7d1adf387f
parent 42 7f84f4de2893
child 45 7e24949b87b0
permissions -rwxr-xr-x
Work on hsqldb support
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package org.sonews;
    19 
    20 import java.sql.Driver;
    21 import java.sql.DriverManager;
    22 import java.util.Enumeration;
    23 import java.util.Date;
    24 import java.util.logging.Level;
    25 import org.sonews.config.Config;
    26 import org.sonews.daemon.ChannelLineBuffers;
    27 import org.sonews.daemon.CommandSelector;
    28 import org.sonews.daemon.Connections;
    29 import org.sonews.daemon.NNTPDaemon;
    30 import org.sonews.feed.FeedManager;
    31 import org.sonews.mlgw.MailPoller;
    32 import org.sonews.storage.StorageBackendException;
    33 import org.sonews.storage.StorageManager;
    34 import org.sonews.storage.StorageProvider;
    35 import org.sonews.util.Log;
    36 import org.sonews.util.Purger;
    37 import org.sonews.util.io.Resource;
    38 
    39 /**
    40  * Startup class of the daemon.
    41  * @author Christian Lins
    42  * @since sonews/0.5.0
    43  */
    44 public final class Main {
    45 
    46 	/** Version information of the sonews daemon */
    47 	public static final String VERSION = "sonews/1.1.0";
    48 	public static final Date STARTDATE = new Date();
    49 
    50 	/**
    51 	 * The main entrypoint.
    52 	 * @param args
    53 	 * @throws Exception
    54 	 */
    55 	public static void main(String[] args) throws Exception {
    56 		System.out.println(VERSION);
    57 		Thread.currentThread().setName("Mainthread");
    58 
    59 		// Command line arguments
    60 		boolean feed = false;  // Enable feeding?
    61 		boolean mlgw = false;  // Enable Mailinglist gateway?
    62 		int port = -1;
    63 
    64 		for (int n = 0; n < args.length; n++) {
    65 			if (args[n].equals("-c") || args[n].equals("-config")) {
    66 				Config.inst().set(Config.LEVEL_CLI, Config.CONFIGFILE, args[++n]);
    67 				System.out.println("Using config file " + args[n]);
    68 			} else if (args[n].equals("-dumpjdbcdriver")) {
    69 				System.out.println("Available JDBC drivers:");
    70 				Enumeration<Driver> drvs = DriverManager.getDrivers();
    71 				while (drvs.hasMoreElements()) {
    72 					System.out.println(drvs.nextElement());
    73 				}
    74 				return;
    75 			} else if (args[n].equals("-feed")) {
    76 				feed = true;
    77 			} else if (args[n].equals("-h") || args[n].equals("-help")) {
    78 				printArguments();
    79 				return;
    80 			} else if (args[n].equals("-mlgw")) {
    81 				mlgw = true;
    82 			} else if (args[n].equals("-p")) {
    83 				port = Integer.parseInt(args[++n]);
    84 			} else if (args[n].equals("-plugin")) {
    85 				System.out.println("Warning: -plugin-storage is not implemented!");
    86 			} else if (args[n].equals("-plugin-command")) {
    87 				try {
    88 					CommandSelector.addCommandHandler(args[++n]);
    89 				} catch (Exception ex) {
    90 					Log.get().warning("Could not load command plugin: " + args[n]);
    91 					Log.get().log(Level.INFO, "Main.java", ex);
    92 				}
    93 			} else if (args[n].equals("-plugin-storage")) {
    94 				System.out.println("Warning: -plugin-storage is not implemented!");
    95 			} else if (args[n].equals("-v") || args[n].equals("-version")) {
    96 				// Simply return as the version info is already printed above
    97 				return;
    98 			}
    99 		}
   100 
   101 		// Try to load the JDBCDatabase;
   102 		// Do NOT USE BackendConfig or Log classes before this point because they require
   103 		// a working JDBCDatabase connection.
   104 		try {
   105 			StorageProvider sprov =
   106 					StorageManager.loadProvider("org.sonews.storage.impl.JDBCDatabaseProvider");
   107 			StorageManager.enableProvider(sprov);
   108 
   109 			// Make sure some elementary groups are existing
   110 			if (!StorageManager.current().isGroupExisting("control")) {
   111 				StorageManager.current().addGroup("control", 0);
   112 				Log.get().info("Group 'control' created.");
   113 			}
   114 		} catch (StorageBackendException ex) {
   115 			ex.printStackTrace();
   116 			System.err.println("Database initialization failed with " + ex.toString());
   117 			System.err.println("Make sure you have specified the correct database"
   118 					+ " settings in sonews.conf!");
   119 			return;
   120 		}
   121 
   122 		ChannelLineBuffers.allocateDirect();
   123 
   124 		// Add shutdown hook
   125 		Runtime.getRuntime().addShutdownHook(new ShutdownHook());
   126 
   127 		// Start the listening daemon
   128 		if (port <= 0) {
   129 			port = Config.inst().get(Config.PORT, 119);
   130 		}
   131 		final NNTPDaemon daemon = NNTPDaemon.createInstance(port);
   132 		daemon.start();
   133 
   134 		// Start Connections purger thread...
   135 		Connections.getInstance().start();
   136 
   137 		// Start mailinglist gateway...
   138 		if (mlgw) {
   139 			new MailPoller().start();
   140 		}
   141 
   142 		// Start feeds
   143 		if (feed) {
   144 			FeedManager.startFeeding();
   145 		}
   146 
   147 		Purger purger = new Purger();
   148 		purger.start();
   149 
   150 		// Wait for main thread to exit (setDaemon(false))
   151 		daemon.join();
   152 	}
   153 
   154 	private static void printArguments() {
   155 		String usage = Resource.getAsString("helpers/usage", true);
   156 		System.out.println(usage);
   157 	}
   158 
   159 	private Main() {
   160 	}
   161 }