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