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