package votorola.a.mail; // Copyright 2007-2008, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
import java.util.*;
import java.util.concurrent.atomic.*;
import votorola._.*;
import votorola.a.manline.*;
import votorola.g.hold.*;
import votorola.g.lang.*;
import votorola.g.util.concurrent.*;
import votorola.g.logging.*;
import votorola.g.option.*;
/** Main class of the command line executable 'vomailrd' -
* a service daemon to await and respond to email messages from voters.
* This is actually the command line launcher,
* the daemon itself is {@linkplain MailResponder MailResponder}.
* The single instance of VOMailRD is available via VOMailRD.i().
*
* @see
* vomailrd(1)
*/
public @ThreadSafe final class VOMailRD
{
private VOMailRD() {}
/** The single instance of VOMailRD.
*/
static VOMailRD i() { return instance; }
/** Executes from the command line.
*
* @param argv command line argument array
*/
public static void main( String[] argv )
{
Thread.setDefaultUncaughtExceptionHandler( fatalExceptionHandler ); // early
LoggerX.i(VOMailRD.class).info( "vomailrd is running with arguments " + Arrays.toString( argv ));
final Map optionMap = CommandLine.compileBaseOptions();
{
final int aFirstNonOption = GetoptX.parse( "vomailrd", argv, optionMap );
if( aFirstNonOption < argv.length )
{
System.err.println( GetoptX.createUnexpectedArgWarning
( "vomailrd", argv[aFirstNonOption] ));
System.err.println( GetoptX.createHelpPrompt( "vomailrd" ));
System.exit( 1 );
}
}
if( optionMap.get("help").hasOccured() )
{
System.out.print
(
"Usage: vomailrd\n" +
"Await and respond to email messages from voters.\n"
);
System.exit( 0 );
}
instance.start();
}
void start()
{
try
{
if( wasStartCommencedA.getAndSet( true )) { throw new IllegalStateException(); }
LoggerX.i(VOMailRD.class).info( "starting mail responder daemon" );
new MailResponder(); // its thread will keep the VM running (if nothing else does) until Runtime.exit() is requested
}
catch( RuntimeException x ) { throw x; } // though it goes to fatalExceptionHandler regardless
catch( Exception x )
{
fatalExceptionHandler.uncaughtException( Thread.currentThread(), x ); // and exit
}
}
// ------------------------------------------------------------------------------------
/** Spool unwound just before this run ends, at VM shut-down.
*/
Spool spool() { return spool; }
private final Spool spool = new SpoolT();
{
Thread shutdownThread = new Thread()
{
public void run()
{
// LoggerX.i(VOMailRD.class).info( "stopping mail responder daemon" );
///// logging service unreliable, at this stage
stopLatch.countDown();
spool.unwind( new CatcherP() );
// throw new Error( "test error in shutdown thread" );
}
};
shutdownThread.setUncaughtExceptionHandler( new ThreadX.UncaughtExceptionPrinter() ); // instead of this run's setDefaultUncaughtExceptionHandler, which depends on the logger
Runtime.getRuntime().addShutdownHook( shutdownThread );
// spool.add( new Hold()
// {
// public @ThreadSafe void release() { throw new RuntimeException( "test exception during shutdown" ); }
// });
}
/** A latch that zeroes when this run is stopping.
*/
CountDownLatchX stopLatch() { return stopLatch; }
private CountDownLatchX stopLatch = new CountDownLatchX( 1 );
/** Returns true if this run is starting, or has started.
*/
boolean wasStartCommenced() { return wasStartCommencedA.get(); }
private AtomicBoolean wasStartCommencedA = new AtomicBoolean();
//// P r i v a t e ///////////////////////////////////////////////////////////////////////
private static final Thread.UncaughtExceptionHandler fatalExceptionHandler =
new ThreadX.UncaughtExceptionLogger( LoggerX.SEVERE )
{
public void uncaughtException( Thread thread, final Throwable t )
{
super.uncaughtException( thread, t ); // log the full trace
System.err.println( "Fatal error" + Votorola.unmessagedDetails(t) + ": " + t ); // give the user a brief message
System.exit( 1 ); // rather than risk running with a dead thread
}
};
//////////////////////////////////////////////////////////////////////////////////////////
// Last, so static fields above initialize, and are available during instantiation below
private static final VOMailRD instance = new VOMailRD();
}