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: 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: */ chris@3: public final class Main chris@3: { chris@3: chris@3: private Main() chris@3: { chris@3: } chris@3: chris@3: /** Version information of the sonews daemon */ cli@15: public static final String VERSION = "sonews/1.1.0"; chris@3: public static final Date STARTDATE = new Date(); chris@3: chris@3: /** chris@3: * The main entrypoint. chris@3: * @param args chris@3: * @throws Exception chris@3: */ chris@3: public static void main(String[] args) throws Exception chris@3: { chris@3: System.out.println(VERSION); chris@3: Thread.currentThread().setName("Mainthread"); chris@3: chris@3: // Command line arguments chris@3: boolean feed = false; // Enable feeding? chris@3: boolean mlgw = false; // Enable Mailinglist gateway? chris@3: int port = -1; chris@3: chris@3: for(int n = 0; n < args.length; n++) chris@3: { chris@3: if(args[n].equals("-c") || args[n].equals("-config")) chris@3: { chris@3: Config.inst().set(Config.LEVEL_CLI, Config.CONFIGFILE, args[++n]); chris@3: System.out.println("Using config file " + args[n]); chris@3: } chris@3: else if(args[n].equals("-dumpjdbcdriver")) chris@3: { chris@3: System.out.println("Available JDBC drivers:"); chris@3: Enumeration drvs = DriverManager.getDrivers(); chris@3: while(drvs.hasMoreElements()) chris@3: { chris@3: System.out.println(drvs.nextElement()); chris@3: } chris@3: return; chris@3: } chris@3: else if(args[n].equals("-feed")) chris@3: { chris@3: feed = true; chris@3: } chris@3: else if(args[n].equals("-h") || args[n].equals("-help")) chris@3: { chris@3: printArguments(); chris@3: return; chris@3: } chris@3: else if(args[n].equals("-mlgw")) chris@3: { chris@3: mlgw = true; chris@3: } chris@3: else if(args[n].equals("-p")) chris@3: { chris@3: port = Integer.parseInt(args[++n]); chris@3: } cli@21: else if(args[n].equals("-plugin")) cli@21: { cli@21: System.out.println("Warning: -plugin-storage is not implemented!"); cli@21: } cli@21: else if(args[n].equals("-plugin-command")) cli@21: { cli@21: try cli@21: { cli@21: CommandSelector.addCommandHandler(args[++n]); cli@21: } cli@21: catch(Exception ex) cli@21: { cli@21: Log.get().warning("Could not load command plugin: " + args[n]); cli@21: Log.get().log(Level.INFO, "Main.java", ex); cli@21: } cli@21: } cli@21: else if(args[n].equals("-plugin-storage")) cli@21: { cli@21: System.out.println("Warning: -plugin-storage is not implemented!"); cli@21: } chris@3: else if(args[n].equals("-v") || args[n].equals("-version")) chris@3: { chris@3: // Simply return as the version info is already printed above chris@3: return; chris@3: } chris@3: } chris@3: chris@3: // Try to load the JDBCDatabase; chris@3: // Do NOT USE BackendConfig or Log classes before this point because they require chris@3: // a working JDBCDatabase connection. chris@3: try chris@3: { chris@3: StorageProvider sprov = chris@3: StorageManager.loadProvider("org.sonews.storage.impl.JDBCDatabaseProvider"); chris@3: StorageManager.enableProvider(sprov); chris@3: chris@3: // Make sure some elementary groups are existing chris@3: if(!StorageManager.current().isGroupExisting("control")) chris@3: { chris@3: StorageManager.current().addGroup("control", 0); cli@15: Log.get().info("Group 'control' created."); chris@3: } chris@3: } chris@3: catch(StorageBackendException ex) chris@3: { chris@3: ex.printStackTrace(); chris@3: System.err.println("Database initialization failed with " + ex.toString()); chris@3: System.err.println("Make sure you have specified the correct database" + chris@3: " settings in sonews.conf!"); chris@3: return; chris@3: } chris@3: chris@3: ChannelLineBuffers.allocateDirect(); chris@3: chris@3: // Add shutdown hook chris@3: Runtime.getRuntime().addShutdownHook(new ShutdownHook()); chris@3: chris@3: // Start the listening daemon chris@3: if(port <= 0) chris@3: { chris@3: port = Config.inst().get(Config.PORT, 119); chris@3: } chris@3: final NNTPDaemon daemon = NNTPDaemon.createInstance(port); chris@3: daemon.start(); chris@3: chris@3: // Start Connections purger thread... chris@3: Connections.getInstance().start(); chris@3: chris@3: // Start mailinglist gateway... chris@3: if(mlgw) chris@3: { chris@3: new MailPoller().start(); chris@3: } chris@3: chris@3: // Start feeds chris@3: if(feed) chris@3: { chris@3: FeedManager.startFeeding(); chris@3: } chris@3: chris@3: Purger purger = new Purger(); chris@3: purger.start(); chris@3: chris@3: // Wait for main thread to exit (setDaemon(false)) chris@3: daemon.join(); chris@3: } chris@3: chris@3: private static void printArguments() chris@3: { chris@3: String usage = Resource.getAsString("helpers/usage", true); chris@3: System.out.println(usage); chris@3: } chris@3: chris@3: }