package votorola.a.election; // 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.io.*;
import java.util.*;
import votorola._.*;
import votorola.a.*;
import votorola.a.manline.*;
import votorola.a.register.*;
import votorola.a.register.VoterList; // in precedence over java.util.VoterList
import votorola.g.io.*;
import votorola.g.lang.*;
import votorola.g.logging.*;
import votorola.g.option.*;
/** Main class of the command line executable 'vocount' -
* a service utility to tally the results of an election.
*
* @see
* vocount(1)
*/
public @ThreadSafe final class VOCount
{
// Cf. a/register/VOList
/** Runs the tool from the command line.
*
* @param argv command line argument array
*/
public static void main( final String[] argv )
{
LoggerX.i(VOCount.class).info( "vocount is running with arguments " + Arrays.toString( argv ));
final Map optionMap = CommandLine.compileBaseOptions();
final int aFirstNonOption = GetoptX.parse( "vocount", argv, optionMap );
if( optionMap.get("help").hasOccured() )
{
System.out.print
(
"Usage: vocount ready SNAP-DIRECTORY\n" +
" or vocount mount|umount SNAP-DIRECTORY/readyCount-DATE\n" +
"Tally the results of an election.\n"
);
System.exit( 0 );
}
final int addressCount = argv.length - aFirstNonOption;
if( addressCount == 0 )
{
System.err.println( "vocount: missing action argument" );
System.err.println( GetoptX.createHelpPrompt( "vocount" ));
System.exit( 1 );
}
else if( addressCount > 2 )
{
System.err.println( "vocount: too many arguments" );
System.err.println( GetoptX.createHelpPrompt( "vocount" ));
System.exit( 1 );
}
try
{
final ElectoralSubserver.Run run =
new ElectoralSubserver( System.getProperty( "user.name" ))
.new Run( /*isSingleThreaded*/true );
run.singleServiceLock().lock(); // no need to unlock, single access
final String actionArg = argv[aFirstNonOption];
if( "mount".equals( actionArg ))
{
if( addressCount < 2 )
{
System.err.println( "vocount: missing argument SNAP-DIRECTORY/readyCount-DATE" );
System.err.println( GetoptX.createHelpPrompt( "vocount" ));
System.exit( 1 );
}
final ReadyDirectory readyDirectory =
new ReadyDirectory( argv[aFirstNonOption + 1] );
if( readyDirectory.isMounted() )
{
System.err.println( "vocount: already mounted: " + readyDirectory );
System.exit( 1 );
}
final votorola.a.register.ReadyDirectory registerReadyDirectory =
new votorola.a.register.ReadyDirectory(
readyDirectory.readyListLink().getCanonicalPath() );
if( !registerReadyDirectory.isMounted() )
{
System.err.println( "vocount: no voter list, register not mounted: " + registerReadyDirectory );
System.exit( 1 );
}
final Election election = run.init_ensureElection( readyDirectory.name() );
final VoterList voterList = VoterList.readObjectFromMountedListFile( registerReadyDirectory );
readyDirectory.mount( election, voterList );
}
else if( "ready".equals( actionArg ))
{
if( addressCount < 2 )
{
System.err.println( "vocount: missing SNAP-DIRECTORY argument" );
System.err.println( GetoptX.createHelpPrompt( "vocount" ));
System.exit( 1 );
}
final File snapDirectory = new File( argv[aFirstNonOption + 1] );
final String name = snapDirectory.getParentFile().getName();
final Election election = run.init_ensureElection( name );
final File registerReadyToReportLink
= election.subserverRun().register().readyToReportLink();
if( !registerReadyToReportLink.exists() )
{
System.err.println( "vocount: no voter list, missing symbolic link to ready register: " + registerReadyToReportLink );
System.exit( 1 );
}
final ReadyDirectory readyDirectory =
ReadyDirectory.createReadyDirectory( snapDirectory );
try
{
readyDirectory.ready( new votorola.a.register.ReadyDirectory(
registerReadyToReportLink.getCanonicalPath() ));
}
catch( Exception x )
{
if( !FileX.deleteRecursive( readyDirectory )) System.err.println( "vocount: unable to remove partially constructed ready directory (please delete it manually): " + readyDirectory );
throw x;
}
System.out.println( readyDirectory );
}
else if( "umount".equals( actionArg ))
{
if( addressCount < 2 )
{
System.err.println( "vocount: missing argument SNAP-DIRECTORY/readyCount-DATE" );
System.err.println( GetoptX.createHelpPrompt( "vocount" ));
System.exit( 1 );
}
final ReadyDirectory readyDirectory =
new ReadyDirectory( argv[aFirstNonOption + 1] );
if( !readyDirectory.unmount( run.init_ensureElection( readyDirectory.name() )))
{
System.err.println( "vocount: not mounted: " + readyDirectory );
System.exit( 1 );
}
}
else
{
System.err.println( "vocount: unrecognized action argument: " + actionArg );
System.err.println( GetoptX.createHelpPrompt( "vocount" ));
System.exit( 1 );
}
}
catch( RuntimeException x ) { throw x; }
catch( Exception x ) // ScriptException, IOException, NoSuchMethodException, ScriptException, SQLException
{
System.err.print( "vocount: fatal error" + Votorola.unmessagedDetails(x) + ": " );
// System.err.println( x );
x.printStackTrace( System.err );
System.exit( 1 );
}
}
}