001package votorola.a.response.line; // Copyright 2009, 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.
002
003import java.io.*;
004import java.net.*;
005import java.sql.*;
006import java.util.*;
007import javax.script.*;
008import votorola.a.*;
009import votorola.g.lang.*;
010import votorola.g.option.*;
011
012
013/** Base class of executables that compile results, such as lists or counts.
014  */
015public @ThreadSafe abstract class ResultsCompiler
016{
017
018
019    /** Compiles a map of options that are common to all results compilers.
020      */
021    protected static HashMap<String,Option> compileBaseOptions()
022    {
023        final HashMap<String,Option> optionMap = CommandLine.compileBaseOptions();
024        String name;
025
026      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
027        name = "etc";
028        optionMap.put( name, new Option( name, Option.OPTIONAL_ARGUMENT ));
029
030      // - - -
031        return optionMap;
032    }
033
034
035
036    /** The lazilly initialized vote-server.
037      */
038    protected VoteServer voteServer() throws IOException, ScriptException, URISyntaxException
039    {
040        if( _voteServer == null ) _voteServer = new VoteServer( System.getProperty( "user.name" ));
041
042        return _voteServer;
043    }
044
045
046        private VoteServer _voteServer;
047
048
049
050    /** The lazilly started and locked vote-server run.
051      */
052    protected VoteServer.Run vsRun() throws IOException, ScriptException, SQLException,
053      URISyntaxException
054    {
055        if( _vsRun == null )
056        {
057            _vsRun = voteServer().new Run( /*isSingleThreaded*/true );
058            _vsRun.singleServiceLock().lock(); // no need to unlock, single access
059        }
060        return _vsRun;
061    }
062
063
064        private VoteServer.Run _vsRun;
065
066
067
068   // ====================================================================================
069
070
071    /** An allowable action for a results compiler.
072      */
073    protected static enum Action
074    {
075
076        snap, ready, mount, report, umount, ureport, status;
077
078
079        /** The last of the actions valid for use as an end action, in an
080          * <code>--etc</code> option.
081          */
082        public static final Action END_ACTION_LAST = umount;
083
084
085        /** Returns true if an end action beyond the current action is specified in an
086          * <code>--etc</code> option.  Exits the system if the option is not well formed.
087          */
088        public boolean isEtc( final String commandName, final Map<String,Option> optionMap )
089        {
090            final Option etc = optionMap.get( "etc" );
091            if( !etc.hasOccured() ) return false;
092
093            final String endActionArg = etc.argumentValue();
094            final Action endAction;
095            if( endActionArg == null ) endAction = END_ACTION_LAST; // default
096            else
097            {
098                try
099                {
100                    endAction = valueOf( endActionArg );
101                }
102                catch( IllegalArgumentException x )
103                {
104                    System.err.println( commandName + ": unrecognized end-action in --etc: "
105                      + endActionArg );
106                    System.err.println( GetoptX.createHelpPrompt( commandName ));
107                    System.exit( 1 ); return false; // redundant return, to prevent compiler warnings
108                }
109
110                if( endAction.ordinal() > END_ACTION_LAST.ordinal() )
111                {
112                    System.err.println( commandName + ": invalid end-action in --etc: "
113                      + endActionArg );
114                    System.err.println( GetoptX.createHelpPrompt( commandName ));
115                    System.exit( 1 );
116                }
117            }
118
119            return endAction.ordinal() > ordinal();
120        }
121
122    }
123
124
125
126}