1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/org/sonews/Main.java Sun Aug 29 17:28:58 2010 +0200
1.3 @@ -0,0 +1,198 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews;
1.23 +
1.24 +import java.sql.Driver;
1.25 +import java.sql.DriverManager;
1.26 +import java.util.Enumeration;
1.27 +import java.util.Date;
1.28 +import java.util.logging.Level;
1.29 +import org.sonews.config.Config;
1.30 +import org.sonews.daemon.ChannelLineBuffers;
1.31 +import org.sonews.daemon.CommandSelector;
1.32 +import org.sonews.daemon.Connections;
1.33 +import org.sonews.daemon.NNTPDaemon;
1.34 +import org.sonews.feed.FeedManager;
1.35 +import org.sonews.mlgw.MailPoller;
1.36 +import org.sonews.storage.StorageBackendException;
1.37 +import org.sonews.storage.StorageManager;
1.38 +import org.sonews.storage.StorageProvider;
1.39 +import org.sonews.util.Log;
1.40 +import org.sonews.util.Purger;
1.41 +import org.sonews.util.io.Resource;
1.42 +
1.43 +/**
1.44 + * Startup class of the daemon.
1.45 + * @author Christian Lins
1.46 + * @since sonews/0.5.0
1.47 + */
1.48 +public final class Main
1.49 +{
1.50 +
1.51 + private Main()
1.52 + {
1.53 + }
1.54 +
1.55 + /** Version information of the sonews daemon */
1.56 + public static final String VERSION = "sonews/1.1.0";
1.57 + public static final Date STARTDATE = new Date();
1.58 +
1.59 + /**
1.60 + * The main entrypoint.
1.61 + * @param args
1.62 + * @throws Exception
1.63 + */
1.64 + public static void main(String[] args) throws Exception
1.65 + {
1.66 + System.out.println(VERSION);
1.67 + Thread.currentThread().setName("Mainthread");
1.68 +
1.69 + // Command line arguments
1.70 + boolean feed = false; // Enable feeding?
1.71 + boolean mlgw = false; // Enable Mailinglist gateway?
1.72 + int port = -1;
1.73 +
1.74 + for(int n = 0; n < args.length; n++)
1.75 + {
1.76 + if(args[n].equals("-c") || args[n].equals("-config"))
1.77 + {
1.78 + Config.inst().set(Config.LEVEL_CLI, Config.CONFIGFILE, args[++n]);
1.79 + System.out.println("Using config file " + args[n]);
1.80 + }
1.81 + else if(args[n].equals("-dumpjdbcdriver"))
1.82 + {
1.83 + System.out.println("Available JDBC drivers:");
1.84 + Enumeration<Driver> drvs = DriverManager.getDrivers();
1.85 + while(drvs.hasMoreElements())
1.86 + {
1.87 + System.out.println(drvs.nextElement());
1.88 + }
1.89 + return;
1.90 + }
1.91 + else if(args[n].equals("-feed"))
1.92 + {
1.93 + feed = true;
1.94 + }
1.95 + else if(args[n].equals("-h") || args[n].equals("-help"))
1.96 + {
1.97 + printArguments();
1.98 + return;
1.99 + }
1.100 + else if(args[n].equals("-mlgw"))
1.101 + {
1.102 + mlgw = true;
1.103 + }
1.104 + else if(args[n].equals("-p"))
1.105 + {
1.106 + port = Integer.parseInt(args[++n]);
1.107 + }
1.108 + else if(args[n].equals("-plugin"))
1.109 + {
1.110 + System.out.println("Warning: -plugin-storage is not implemented!");
1.111 + }
1.112 + else if(args[n].equals("-plugin-command"))
1.113 + {
1.114 + try
1.115 + {
1.116 + CommandSelector.addCommandHandler(args[++n]);
1.117 + }
1.118 + catch(Exception ex)
1.119 + {
1.120 + Log.get().warning("Could not load command plugin: " + args[n]);
1.121 + Log.get().log(Level.INFO, "Main.java", ex);
1.122 + }
1.123 + }
1.124 + else if(args[n].equals("-plugin-storage"))
1.125 + {
1.126 + System.out.println("Warning: -plugin-storage is not implemented!");
1.127 + }
1.128 + else if(args[n].equals("-v") || args[n].equals("-version"))
1.129 + {
1.130 + // Simply return as the version info is already printed above
1.131 + return;
1.132 + }
1.133 + }
1.134 +
1.135 + // Try to load the JDBCDatabase;
1.136 + // Do NOT USE BackendConfig or Log classes before this point because they require
1.137 + // a working JDBCDatabase connection.
1.138 + try
1.139 + {
1.140 + StorageProvider sprov =
1.141 + StorageManager.loadProvider("org.sonews.storage.impl.JDBCDatabaseProvider");
1.142 + StorageManager.enableProvider(sprov);
1.143 +
1.144 + // Make sure some elementary groups are existing
1.145 + if(!StorageManager.current().isGroupExisting("control"))
1.146 + {
1.147 + StorageManager.current().addGroup("control", 0);
1.148 + Log.get().info("Group 'control' created.");
1.149 + }
1.150 + }
1.151 + catch(StorageBackendException ex)
1.152 + {
1.153 + ex.printStackTrace();
1.154 + System.err.println("Database initialization failed with " + ex.toString());
1.155 + System.err.println("Make sure you have specified the correct database" +
1.156 + " settings in sonews.conf!");
1.157 + return;
1.158 + }
1.159 +
1.160 + ChannelLineBuffers.allocateDirect();
1.161 +
1.162 + // Add shutdown hook
1.163 + Runtime.getRuntime().addShutdownHook(new ShutdownHook());
1.164 +
1.165 + // Start the listening daemon
1.166 + if(port <= 0)
1.167 + {
1.168 + port = Config.inst().get(Config.PORT, 119);
1.169 + }
1.170 + final NNTPDaemon daemon = NNTPDaemon.createInstance(port);
1.171 + daemon.start();
1.172 +
1.173 + // Start Connections purger thread...
1.174 + Connections.getInstance().start();
1.175 +
1.176 + // Start mailinglist gateway...
1.177 + if(mlgw)
1.178 + {
1.179 + new MailPoller().start();
1.180 + }
1.181 +
1.182 + // Start feeds
1.183 + if(feed)
1.184 + {
1.185 + FeedManager.startFeeding();
1.186 + }
1.187 +
1.188 + Purger purger = new Purger();
1.189 + purger.start();
1.190 +
1.191 + // Wait for main thread to exit (setDaemon(false))
1.192 + daemon.join();
1.193 + }
1.194 +
1.195 + private static void printArguments()
1.196 + {
1.197 + String usage = Resource.getAsString("helpers/usage", true);
1.198 + System.out.println(usage);
1.199 + }
1.200 +
1.201 +}