package votorola.a.manline; // 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.sql.*;
import java.util.*;
import javax.script.*;
import votorola._.*;
import votorola.a.*;
import votorola.a.locale.*;
import votorola.a.mail.*;
import votorola.a.voter.*;
import votorola.g.lang.*;
import votorola.g.logging.*;
import votorola.g.option.*;
/** Main class of the command line executable 'voter' -
* a command shell to issue a voter command to an electoral service.
*
* @see
* voter(1)
*/
public @ThreadSafe final class VOTer
{
private VOTer( final String voterEmail, final String serviceName, final String[] argvCommand )
throws java.io.IOException, ScriptException, SQLException
{
this.voterEmail = voterEmail;
this.serviceName = serviceName;
this.argvCommand = argvCommand;
}
/** Executes from the command line.
*
* @param argv command line argument array
*/
public static void main( String[] argv )
{
LoggerX.i(VOTer.class).info( "voter is running with arguments " + Arrays.toString( argv ));
// Parse arguments for VOTer. Split argv into argv for VOTer only,
// and argvCommand for the command responder.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
final String[] argvCommand;
String voterEmail = null; String serviceName = null; boolean tooManyEmailArguments = false;
{
int a;
for( a = 0; a < argv.length; ++a )
{
final String arg = argv[a];
if( arg.charAt(0) == '-' ) continue; // skip past VOTer options
if( arg.indexOf('@') > 0 ) // swallow VOTER-EMAIL
{
if( voterEmail == null )
{
voterEmail = arg;
continue;
}
else tooManyEmailArguments = true;
}
if( serviceName == null ) // swallow SERVICE-NAME
{
serviceName = arg;
continue;
}
break;
}
argvCommand = Arrays.copyOfRange( argv, a, argv.length ); // argvCommand[0] is the command itself, the arguments proper follow it
argv = Arrays.copyOfRange( argv, 0, a );
}
// Parse options for VOTer.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
final Map optionMap = CommandLine.compileBaseOptions();
final int aFirstNonOption = GetoptX.parse( "voter", argv, optionMap );
if( optionMap.get("help").hasOccured() )
{
System.out.print
(
"Usage: voter [VOTER-EMAIL] SERVICE-NAME COMMAND [OPTION...] [ARGUMENT...]\n" +
" e.g. voter SERVICE-NAME help\n" +
"Issue a voter command directly to an electoral service.\n"
);
System.exit( 0 );
}
// - - -
if( tooManyEmailArguments )
{
System.err.println( "voter: too many VOTER-EMAIL arguments" );
System.err.println( GetoptX.createHelpPrompt( "voter" ));
System.exit( 1 );
}
if( serviceName == null )
{
System.err.println( "voter: missing SERVICE-NAME argument" );
System.err.println( GetoptX.createHelpPrompt( "voter" ));
System.exit( 1 );
}
try
{
VOTer voTer = new VOTer( voterEmail, serviceName, argvCommand );
voTer.run();
}
catch( RuntimeException x ) { throw x; }
catch( Exception x ) // IOException, ScriptException, SQLException
{
System.err.print( "voter: fatal error" + Votorola.unmessagedDetails(x) + ": " );
// System.err.println( x );
x.printStackTrace( System.err );
System.exit( 1 );
}
}
//// P r i v a t e ///////////////////////////////////////////////////////////////////////
private final String[] argvCommand;
private ElectoralService ensureElectoralService( final String serviceName )
throws IOException, ScriptException, SQLException
{
try
{
return subserverRun.init_ensureElectoralService( serviceName, /*any class*/null );
}
catch( final ElectoralSubserver.NoSuchServiceException x )
{
final MailMetaService.ConfigurationContext cc;
try
{
cc = MailMetaService.ConfigurationContext.configure( subserverRun.subserver(),
MailResponder.compileConfigurationScript( subserverRun.subserver() ));
}
catch( ScriptException xS )
{
System.err.println( "voter, warning: cannot fall back to meta-service, bacause of: " + xS );
throw x;
}
if( !serviceName.equals( cc.getName() ))
{
System.err.println( "voter, warning: unrecognized SERVICE-NAME argument '" + serviceName + "', falling back to meta-service '" + cc.getName() + "'" );
}
final MailMetaService metaService = new MailMetaService( subserverRun, cc );
subserverRun.init_putElectoralService( metaService );
return metaService;
}
}
private void run() throws Exception // ScriptException, IOException, SQLException, and soft exceptions from dispatch()
{
if( argvCommand.length == 0 )
{
System.err.println( "voter: missing COMMAND argument" );
System.err.println( GetoptX.createHelpPrompt( "voter" ));
System.exit( 1 );
}
ElectoralService electoralService = null;
try
{
electoralService = ensureElectoralService( serviceName );
}
catch( ElectoralSubserver.NoSuchServiceException x )
{
System.err.println( "voter: unrecognized SERVICE-NAME argument '" + serviceName + "'" );
System.err.println( GetoptX.createHelpPrompt( "voter" ));
System.exit( 1 );
}
final ReplyBuilder replyB = new ReplyBuilder( Locale.getDefault() );
final BundleFormatter bunA = new BundleFormatter
( ResourceBundle.getBundle( "votorola.a.locale.A", replyB.locale() ));
int trustLevel = 0;
if( voterEmail != null )
{
trustLevel = subserverRun.register().getListNode( /*list ref*/null, voterEmail )
.trustLevel();
}
final CommandResponder.Session commandSession =
new CommandResponder.Session( voterEmail, trustLevel, bunA, replyB );
try
{
Exception x = electoralService.dispatch( argvCommand, commandSession );
if( x != null ) throw x;
}
catch( CommandResponder.AnonymousIssueException x )
{
System.err.println( "voter: missing VOTER-EMAIL (" + x.getMessage() + ")" );
System.err.println( GetoptX.createHelpPrompt( "voter" ));
System.exit( 1 );
}
System.out.print( replyB.chomplnn().toString() );
System.out.flush();
}
private String serviceName;
private final ElectoralSubserver.Run subserverRun =
new ElectoralSubserver( System.getProperty( "user.name" ))
.new Run( /*isSingleThreaded*/true );
{
subserverRun.singleServiceLock().lock(); // no need to unlock, single access
}
private String voterEmail;
}