FileDocCategorySizeDatePackage
Command.javaAPI DocAndroid 1.5 API10827Wed May 06 22:41:16 BST 2009com.vladium.emma

Command

public abstract class Command extends Object
author
Vlad Roubtsov, (C) 2003

Fields Summary
protected final String
m_usageToolName
protected final String[]
m_args
protected File
m_propertyFile
protected Properties
m_propertyOverrides
protected boolean
m_exit
protected PrintWriter
m_out
protected static final String
COMMA_DELIMITERS
protected static final String
PATH_DELIMITERS
protected static final String[]
USAGE_OPT_NAMES
protected static final int
STDOUT_WIDTH
protected static final int
RC_OK
protected static final int
RC_USAGE
protected static final int
RC_UNEXPECTED
Constructors Summary
protected Command(String usageToolName, String[] args)

        m_usageToolName = usageToolName;
        m_args = args != null ? (String []) args.clone () : IConstants.EMPTY_STRING_ARRAY;  
    
Methods Summary
public static com.vladium.emma.Commandcreate(java.lang.String name, java.lang.String usageName, java.lang.String[] args)

        final Command tool;
        
        // TODO: dynamic load here?
        
        if ("run".equals (name))
            tool = new runCommand (usageName, args);
        else if ("instr".equals (name))
            tool = new instrCommand (usageName, args);
        else if ("report".equals (name))
            tool = new reportCommand (usageName, args);
        else if ("merge".equals (name))
            tool = new mergeCommand (usageName, args);
        else
            throw new IllegalArgumentException ("unknown command: [" + name + "]");    
            
        tool.initialize ();
        
        return tool;
    
protected final voidexit(boolean showBuildID, java.lang.String msg, java.lang.Throwable t, int rc)

        if (showBuildID)
        {
            m_out.println (IAppConstants.APP_USAGE_BUILD_ID);
        }
        
        if (msg != null)
        {
            m_out.print (toolNameToCommandName (m_usageToolName) + ": "); m_out.println (msg);
        }

        if (rc != RC_OK)
        {
            // error exit:
            
            //if ((showBuildID) || (msg != null)) m_out.println ();
            
            if (m_exit)
            {
                if (t != null) t.printStackTrace (m_out);
                System.exit (rc);
            }
            else
            {
                if (t instanceof EMMARuntimeException)
                    throw (EMMARuntimeException) t;
                else if (t != null)
                    throw msg != null ? new EMMARuntimeException (msg, t) : new EMMARuntimeException ("unexpected failure: ", t);
            }
        }
        else
        {
            // normal exit: 't' is ignored
            
            if (m_exit)
            {
                System.exit (0);
            }
        }
    
protected static java.lang.String[]getListOptValue(IOptsParser.IOpt opt, java.lang.String delimiters, boolean processAtFiles)

        return Strings.mergeAT (opt.getValues (), delimiters, processAtFiles);
    
protected final com.vladium.util.args.IOptsParsergetOptParser(java.lang.ClassLoader loader)

        return IOptsParser.Factory.create (usageResName (getToolName ()), loader,
            usageMsgPrefix (m_usageToolName), USAGE_OPT_NAMES);
    
protected static booleangetOptionalBooleanOptValue(IOptsParser.IOpt opt)

        if (opt.getValueCount () == 0)
            return true;
        else
        {
            final String v = opt.getFirstValue ().toLowerCase ();
         
            return Property.toBoolean (v);
        }
    
protected final java.lang.StringgetToolName()

        // TODO: embed build number etc
        final String clsName = getClass ().getName ();
        
        return clsName.substring (0, clsName.length () - 7);
    
protected voidinitialize()

        m_exit = false;
        
        if (m_out != null) try { m_out.flush (); } catch (Throwable ignore) {}
        m_out = new PrintWriter (System.out, true);
    
protected final voidprocessCmdPropertyOverrides(IOptsParser.IOpts parsedopts)

        final IOptsParser.IOpt [] popts = parsedopts.getOpts (EMMAProperties.GENERIC_PROPERTY_OVERRIDE_PREFIX);
        if ((popts != null) && (popts.length != 0))
        {
            final Properties cmdOverrides = new XProperties ();
            
            for (int o = 0; o < popts.length; ++ o)
            {
                final IOptsParser.IOpt opt = popts [o];
                final String on = opt.getName ().substring (opt.getPatternPrefix ().length ());
                
                // TODO: support mergeable prefixed opts?
                
                cmdOverrides.setProperty (on, opt.getFirstValue ());
            }
            
            // command line user overrides are have highest precedence:
            m_propertyOverrides = Property.combine (cmdOverrides, m_propertyOverrides);
        }
    
protected final booleanprocessFilePropertyOverrides()

        if (m_propertyFile != null)
        {
            final Properties fileOverrides;
            
            try
            {
                fileOverrides = Property.getPropertiesFromFile (m_propertyFile);
            }
            catch (IOException ioe)
            {
                exit (true, "property override file [" + m_propertyFile.getAbsolutePath () + "] could not be read", ioe, RC_USAGE);
                return false;
            }
            
            // props file overrides have second highest precendence:
            m_propertyOverrides = Property.combine (m_propertyOverrides, fileOverrides); 
        }
        
        return true;
    
protected final booleanprocessOpt(IOptsParser.IOpt opt)

        final String on = opt.getCanonicalName ();
        
        if ("exit".equals (on)) // 'exit' should always be first in this else-if chain
        {
            m_exit = getOptionalBooleanOptValue (opt);
            return true;
        }
        else if ("p".equals (on))
        {
            m_propertyFile = new File (opt.getFirstValue ());
            return true;
        }
        else if ("verbose".equals (on))
        {
            setPropertyOverride (AppLoggers.PROPERTY_VERBOSITY_LEVEL, ILogLevels.VERBOSE_STRING);
            return true;
        }
        else if ("quiet".equals (on))
        {
            setPropertyOverride (AppLoggers.PROPERTY_VERBOSITY_LEVEL, ILogLevels.WARNING_STRING);
            return true;
        }
        else if ("silent".equals (on))
        {
            setPropertyOverride (AppLoggers.PROPERTY_VERBOSITY_LEVEL, ILogLevels.SEVERE_STRING);
            return true;
        }
        else if ("debug".equals (on))
        {
            if (opt.getValueCount () == 0)
                setPropertyOverride (AppLoggers.PROPERTY_VERBOSITY_LEVEL, ILogLevels.TRACE1_STRING);
            else
                setPropertyOverride (AppLoggers.PROPERTY_VERBOSITY_LEVEL, opt.getFirstValue ());
            
            return true;
        }
        else if ("debugcls".equals (on))
        {
            setPropertyOverride (AppLoggers.PROPERTY_VERBOSITY_FILTER, Strings.toListForm (Strings.merge (opt.getValues (), COMMA_DELIMITERS, true), ',")); 
            return true;
        }
        
        return false;
    
public abstract voidrun()

private voidsetPropertyOverride(java.lang.String key, java.lang.String value)


    // package: ...............................................................
    
    // private: ...............................................................
    

    /*
     * Lazily instantiates m_propertyOverrides if necessary.
     */
            
    
        Properties propertyOverrides = m_propertyOverrides;
        if (propertyOverrides == null)
        {
            m_propertyOverrides = propertyOverrides = new XProperties ();
        }
        
        propertyOverrides.setProperty (key, value);
    
protected static java.lang.StringtoolNameToCommandName(java.lang.String toolName)

        final int lastDot = toolName.lastIndexOf ('.");
        
        return lastDot > 0 ? toolName.substring (lastDot + 1) : toolName;
    
protected abstract java.lang.StringusageArgsMsg()

protected static java.lang.StringusageMsgPrefix(java.lang.String toolName)

        return toolNameToCommandName (toolName).concat (" usage: ");
    
protected static java.lang.StringusageResName(java.lang.String toolName)

        return toolName.replace ('.", '/").concat ("_usage.res");
    
protected final voidusageexit(com.vladium.util.args.IOptsParser parser, int level, java.lang.String msg)

        if (msg != null)
        {
            m_out.print (usageMsgPrefix (m_usageToolName));
            m_out.println (msg);
        }
        
        if (parser != null)
        {
            m_out.println ();
            m_out.print (usageMsgPrefix (m_usageToolName));
            m_out.println (toolNameToCommandName (m_usageToolName) + " " + usageArgsMsg () + ",");
            m_out.println ("  where options include:");
            m_out.println ();
            parser.usage (m_out, level, STDOUT_WIDTH);            
        }
        
        m_out.println ();
        exit (true, null, null, RC_USAGE);