src/org/sonews/Main.java
changeset 37 74139325d305
parent 35 ed84c8bdd87b
child 42 7f84f4de2893
     1.1 --- a/src/org/sonews/Main.java	Sun Aug 29 17:43:58 2010 +0200
     1.2 +++ b/src/org/sonews/Main.java	Sun Aug 29 18:17:37 2010 +0200
     1.3 @@ -44,155 +44,123 @@
     1.4   */
     1.5  public final class Main
     1.6  {
     1.7 -  
     1.8 -  private Main()
     1.9 -  {
    1.10 -  }
    1.11  
    1.12 -  /** Version information of the sonews daemon */
    1.13 -  public static final String VERSION = "sonews/1.1.0";
    1.14 -  public static final Date   STARTDATE = new Date();
    1.15 -  
    1.16 -  /**
    1.17 -   * The main entrypoint.
    1.18 -   * @param args
    1.19 -   * @throws Exception
    1.20 -   */
    1.21 -  public static void main(String[] args) throws Exception
    1.22 -  {
    1.23 -    System.out.println(VERSION);
    1.24 -    Thread.currentThread().setName("Mainthread");
    1.25 +	/** Version information of the sonews daemon */
    1.26 +	public static final String VERSION = "sonews/1.1.0";
    1.27 +	public static final Date STARTDATE = new Date();
    1.28  
    1.29 -    // Command line arguments
    1.30 -    boolean feed    = false;  // Enable feeding?
    1.31 -    boolean mlgw    = false;  // Enable Mailinglist gateway?
    1.32 -    int     port    = -1;
    1.33 -    
    1.34 -    for(int n = 0; n < args.length; n++)
    1.35 -    {
    1.36 -      if(args[n].equals("-c") || args[n].equals("-config"))
    1.37 -      {
    1.38 -        Config.inst().set(Config.LEVEL_CLI, Config.CONFIGFILE, args[++n]);
    1.39 -        System.out.println("Using config file " + args[n]);
    1.40 -      }
    1.41 -      else if(args[n].equals("-dumpjdbcdriver"))
    1.42 -      {
    1.43 -        System.out.println("Available JDBC drivers:");
    1.44 -        Enumeration<Driver> drvs =  DriverManager.getDrivers();
    1.45 -        while(drvs.hasMoreElements())
    1.46 -        {
    1.47 -          System.out.println(drvs.nextElement());
    1.48 -        }
    1.49 -        return;
    1.50 -      }
    1.51 -      else if(args[n].equals("-feed"))
    1.52 -      {
    1.53 -        feed = true;
    1.54 -      }
    1.55 -      else if(args[n].equals("-h") || args[n].equals("-help"))
    1.56 -      {
    1.57 -        printArguments();
    1.58 -        return;
    1.59 -      }
    1.60 -      else if(args[n].equals("-mlgw"))
    1.61 -      {
    1.62 -        mlgw = true;
    1.63 -      }
    1.64 -      else if(args[n].equals("-p"))
    1.65 -      {
    1.66 -        port = Integer.parseInt(args[++n]);
    1.67 -      }
    1.68 -      else if(args[n].equals("-plugin"))
    1.69 -      {
    1.70 -        System.out.println("Warning: -plugin-storage is not implemented!");
    1.71 -      }
    1.72 -      else if(args[n].equals("-plugin-command"))
    1.73 -      {
    1.74 -        try
    1.75 -        {
    1.76 -          CommandSelector.addCommandHandler(args[++n]);
    1.77 -        }
    1.78 -        catch(Exception ex)
    1.79 -        {
    1.80 -          Log.get().warning("Could not load command plugin: " + args[n]);
    1.81 -          Log.get().log(Level.INFO, "Main.java", ex);
    1.82 -        }
    1.83 -      }
    1.84 -      else if(args[n].equals("-plugin-storage"))
    1.85 -      {
    1.86 -        System.out.println("Warning: -plugin-storage is not implemented!");
    1.87 -      }
    1.88 -      else if(args[n].equals("-v") || args[n].equals("-version"))
    1.89 -      {
    1.90 -        // Simply return as the version info is already printed above
    1.91 -        return;
    1.92 -      }
    1.93 -    }
    1.94 -    
    1.95 -    // Try to load the JDBCDatabase;
    1.96 -    // Do NOT USE BackendConfig or Log classes before this point because they require
    1.97 -    // a working JDBCDatabase connection.
    1.98 -    try
    1.99 -    {
   1.100 -      StorageProvider sprov =
   1.101 -        StorageManager.loadProvider("org.sonews.storage.impl.JDBCDatabaseProvider");
   1.102 -      StorageManager.enableProvider(sprov);
   1.103 -      
   1.104 -      // Make sure some elementary groups are existing
   1.105 -      if(!StorageManager.current().isGroupExisting("control"))
   1.106 -      {
   1.107 -        StorageManager.current().addGroup("control", 0);
   1.108 -        Log.get().info("Group 'control' created.");
   1.109 -      }
   1.110 -    }
   1.111 -    catch(StorageBackendException ex)
   1.112 -    {
   1.113 -      ex.printStackTrace();
   1.114 -      System.err.println("Database initialization failed with " + ex.toString());
   1.115 -      System.err.println("Make sure you have specified the correct database" +
   1.116 -        " settings in sonews.conf!");
   1.117 -      return;
   1.118 -    }
   1.119 -    
   1.120 -    ChannelLineBuffers.allocateDirect();
   1.121 -    
   1.122 -    // Add shutdown hook
   1.123 -    Runtime.getRuntime().addShutdownHook(new ShutdownHook());
   1.124 -    
   1.125 -    // Start the listening daemon
   1.126 -    if(port <= 0)
   1.127 -    {
   1.128 -      port = Config.inst().get(Config.PORT, 119);
   1.129 -    }
   1.130 -    final NNTPDaemon daemon = NNTPDaemon.createInstance(port);
   1.131 -    daemon.start();
   1.132 -    
   1.133 -    // Start Connections purger thread...
   1.134 -    Connections.getInstance().start();
   1.135 -    
   1.136 -    // Start mailinglist gateway...
   1.137 -    if(mlgw)
   1.138 -    {
   1.139 -      new MailPoller().start();
   1.140 -    }
   1.141 -    
   1.142 -    // Start feeds
   1.143 -    if(feed)
   1.144 -    {
   1.145 -      FeedManager.startFeeding();
   1.146 -    }
   1.147 +	/**
   1.148 +	 * The main entrypoint.
   1.149 +	 * @param args
   1.150 +	 * @throws Exception
   1.151 +	 */
   1.152 +	public static void main(String[] args) throws Exception
   1.153 +	{
   1.154 +		System.out.println(VERSION);
   1.155 +		Thread.currentThread().setName("Mainthread");
   1.156  
   1.157 -    Purger purger = new Purger();
   1.158 -    purger.start();
   1.159 -    
   1.160 -    // Wait for main thread to exit (setDaemon(false))
   1.161 -    daemon.join();
   1.162 -  }
   1.163 -  
   1.164 -  private static void printArguments()
   1.165 -  {
   1.166 -    String usage = Resource.getAsString("helpers/usage", true);
   1.167 -    System.out.println(usage);
   1.168 -  }
   1.169 +		// Command line arguments
   1.170 +		boolean feed = false;  // Enable feeding?
   1.171 +		boolean mlgw = false;  // Enable Mailinglist gateway?
   1.172 +		int port = -1;
   1.173  
   1.174 +		for (int n = 0; n < args.length; n++) {
   1.175 +			if (args[n].equals("-c") || args[n].equals("-config")) {
   1.176 +				Config.inst().set(Config.LEVEL_CLI, Config.CONFIGFILE, args[++n]);
   1.177 +				System.out.println("Using config file " + args[n]);
   1.178 +			} else if (args[n].equals("-dumpjdbcdriver")) {
   1.179 +				System.out.println("Available JDBC drivers:");
   1.180 +				Enumeration<Driver> drvs = DriverManager.getDrivers();
   1.181 +				while (drvs.hasMoreElements()) {
   1.182 +					System.out.println(drvs.nextElement());
   1.183 +				}
   1.184 +				return;
   1.185 +			} else if (args[n].equals("-feed")) {
   1.186 +				feed = true;
   1.187 +			} else if (args[n].equals("-h") || args[n].equals("-help")) {
   1.188 +				printArguments();
   1.189 +				return;
   1.190 +			} else if (args[n].equals("-mlgw")) {
   1.191 +				mlgw = true;
   1.192 +			} else if (args[n].equals("-p")) {
   1.193 +				port = Integer.parseInt(args[++n]);
   1.194 +			} else if (args[n].equals("-plugin")) {
   1.195 +				System.out.println("Warning: -plugin-storage is not implemented!");
   1.196 +			} else if (args[n].equals("-plugin-command")) {
   1.197 +				try {
   1.198 +					CommandSelector.addCommandHandler(args[++n]);
   1.199 +				} catch (Exception ex) {
   1.200 +					Log.get().warning("Could not load command plugin: " + args[n]);
   1.201 +					Log.get().log(Level.INFO, "Main.java", ex);
   1.202 +				}
   1.203 +			} else if (args[n].equals("-plugin-storage")) {
   1.204 +				System.out.println("Warning: -plugin-storage is not implemented!");
   1.205 +			} else if (args[n].equals("-v") || args[n].equals("-version")) {
   1.206 +				// Simply return as the version info is already printed above
   1.207 +				return;
   1.208 +			}
   1.209 +		}
   1.210 +
   1.211 +		// Try to load the JDBCDatabase;
   1.212 +		// Do NOT USE BackendConfig or Log classes before this point because they require
   1.213 +		// a working JDBCDatabase connection.
   1.214 +		try {
   1.215 +			StorageProvider sprov =
   1.216 +				StorageManager.loadProvider("org.sonews.storage.impl.JDBCDatabaseProvider");
   1.217 +			StorageManager.enableProvider(sprov);
   1.218 +
   1.219 +			// Make sure some elementary groups are existing
   1.220 +			if (!StorageManager.current().isGroupExisting("control")) {
   1.221 +				StorageManager.current().addGroup("control", 0);
   1.222 +				Log.get().info("Group 'control' created.");
   1.223 +			}
   1.224 +		} catch (StorageBackendException ex) {
   1.225 +			ex.printStackTrace();
   1.226 +			System.err.println("Database initialization failed with " + ex.toString());
   1.227 +			System.err.println("Make sure you have specified the correct database"
   1.228 +				+ " settings in sonews.conf!");
   1.229 +			return;
   1.230 +		}
   1.231 +
   1.232 +		ChannelLineBuffers.allocateDirect();
   1.233 +
   1.234 +		// Add shutdown hook
   1.235 +		Runtime.getRuntime().addShutdownHook(new ShutdownHook());
   1.236 +
   1.237 +		// Start the listening daemon
   1.238 +		if (port <= 0) {
   1.239 +			port = Config.inst().get(Config.PORT, 119);
   1.240 +		}
   1.241 +		final NNTPDaemon daemon = NNTPDaemon.createInstance(port);
   1.242 +		daemon.start();
   1.243 +
   1.244 +		// Start Connections purger thread...
   1.245 +		Connections.getInstance().start();
   1.246 +
   1.247 +		// Start mailinglist gateway...
   1.248 +		if (mlgw) {
   1.249 +			new MailPoller().start();
   1.250 +		}
   1.251 +
   1.252 +		// Start feeds
   1.253 +		if (feed) {
   1.254 +			FeedManager.startFeeding();
   1.255 +		}
   1.256 +
   1.257 +		Purger purger = new Purger();
   1.258 +		purger.start();
   1.259 +
   1.260 +		// Wait for main thread to exit (setDaemon(false))
   1.261 +		daemon.join();
   1.262 +	}
   1.263 +
   1.264 +	private static void printArguments()
   1.265 +	{
   1.266 +		String usage = Resource.getAsString("helpers/usage", true);
   1.267 +		System.out.println(usage);
   1.268 +	}
   1.269 +
   1.270 +	private Main()
   1.271 +	{
   1.272 +	}
   1.273  }