RbUtils 0.13

org.ktc.rbutils.rb
Class AbstractUtility

java.lang.Object
  extended by org.ktc.rbutils.rb.AbstractUtility
All Implemented Interfaces:
Utility
Direct Known Subclasses:
RbCheckerUtility, RbContentListerUtility, RbGeneratorUtility

public abstract class AbstractUtility
extends Object
implements Utility

Provides common functionnalities to the command line utilities.

Since:
RbUtils 0.9.3.3
Version:
$Revision: 1.2 $
Author:
moishi

Field Summary
protected  CommandLine cmdLine
          Command line of this utility.
protected static String ERROR_MSG_EXTENSION_NBR
          Error mesage when more than one extension is specified.
protected static String ERROR_MSG_ROOT_NBR
          Error mesage when more than one root directory is specified.
protected  String[] internalArgs
          Internal arguments to be passed to the utility.
protected static String MSG_MISSING_REQUIRED_OPTION
          Displayed message for user (to be used by subclasses when error occurs on parameter parsing).
protected static String OPTION_EXTENSION
          The e option (for properties files extension).
protected static String OPTION_FILE
          The f option (for files to be processed).
protected static String OPTION_LOG
          The l option (for loggers to be registered).
protected static String OPTION_ROOT
          The r option (for root directory).
protected  Options options
          Options of this utility.
protected static String REQUIRED_HEADER
          Header for required field used when the usage of the utility is displayed.
protected  File root
          The root directory to be used by this utility.
static char SEP_LOG_OPTION
          Separator used to separate logger type and logging file.
protected  String taskName
          Name of the RbUtils task of this utility.
 
Constructor Summary
AbstractUtility()
           
 
Method Summary
protected static Options clone(Options options)
          Clone an Options instance.
protected static Options getCommonOptions()
          Returns a copy of the common options shared by all RbUtils command line utilities.
 String getExtension()
          Parses the arguments of this utility in order to get the extension of properties files to be used by this utility.
 List getFilesToBeProcessed()
          Returns a list of properties files to be processed (-f option).
 Collection getLoggers()
          Parses the arguments of this utility in order to get the loggers to be used by this utility.
 File getRootDir()
          Parses the arguments of this utility in order to get the root directory to be used by this utility.
protected  File getUniqueDir(String optionName, String errorMsg)
          Parses the command line options in order to get a directory which must have been specified only once.
protected  String getUniqueOptionValue(String optionName, String errorMsg, boolean isRequired)
          Parses the command line options in order to get an option value which must have been specified at most once.
 void parse(Exception exception, PrintWriter pw)
          Parses an Exception and provides a user readable messages which is printed in the PrintWriter.
 void parseParameters()
          Parses the internalArgs field according to the options field.
 void usage()
          Prints the usage information of this utility in the Standard Output.
 void usage(PrintWriter pw)
          Prints the usage information of this utility.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OPTION_EXTENSION

protected static final String OPTION_EXTENSION
The e option (for properties files extension).

See Also:
Constant Field Values

ERROR_MSG_EXTENSION_NBR

protected static final String ERROR_MSG_EXTENSION_NBR
Error mesage when more than one extension is specified.

See Also:
Constant Field Values

OPTION_FILE

protected static final String OPTION_FILE
The f option (for files to be processed).

See Also:
Constant Field Values

OPTION_LOG

protected static final String OPTION_LOG
The l option (for loggers to be registered).

See Also:
Constant Field Values

SEP_LOG_OPTION

public static final char SEP_LOG_OPTION
Separator used to separate logger type and logging file.

See Also:
Constant Field Values

OPTION_ROOT

protected static final String OPTION_ROOT
The r option (for root directory).

See Also:
Constant Field Values

ERROR_MSG_ROOT_NBR

protected static final String ERROR_MSG_ROOT_NBR
Error mesage when more than one root directory is specified.

See Also:
Constant Field Values

MSG_MISSING_REQUIRED_OPTION

protected static final String MSG_MISSING_REQUIRED_OPTION
Displayed message for user (to be used by subclasses when error occurs on parameter parsing).

See Also:
Constant Field Values

taskName

protected String taskName
Name of the RbUtils task of this utility. Subclasses MUST set this field in their constructor.


options

protected Options options
Options of this utility. Subclasses MUST set this field in their constructor.


internalArgs

protected String[] internalArgs
Internal arguments to be passed to the utility. Subclasses MUST set this field in their constructor.


root

protected File root
The root directory to be used by this utility.


cmdLine

protected CommandLine cmdLine
Command line of this utility.


REQUIRED_HEADER

protected static final String REQUIRED_HEADER
Header for required field used when the usage of the utility is displayed.

See Also:
Constant Field Values
Constructor Detail

AbstractUtility

public AbstractUtility()
Method Detail

parseParameters

public void parseParameters()
                     throws ParseException
Parses the internalArgs field according to the options field.

Specified by:
parseParameters in interface Utility
Throws:
ParseException - if an error occurs during parsing.

getRootDir

public File getRootDir()
                throws RbUtilsException,
                       FileNotFoundException
Parses the arguments of this utility in order to get the root directory to be used by this utility.
This method must store the returned value in an instance field in order the getFilesToBeProcessed() works.

The Utility.parseParameters() method MUST have been previously called.

Specified by:
getRootDir in interface Utility
Returns:
the root directory.
Throws:
RbUtilsException - if more than one -r option is specified.
FileNotFoundException - if the -r option specifies a directory that does not exist in the file system.

getUniqueDir

protected File getUniqueDir(String optionName,
                            String errorMsg)
                     throws RbUtilsException,
                            FileNotFoundException
Parses the command line options in order to get a directory which must have been specified only once.

Parameters:
optionName - the name of the option to be parsed.
errorMsg - the error message to be used if a RbUtilsException is thrown.
Returns:
the directory.
Throws:
RbUtilsException - if the option is specified more than once.
FileNotFoundException - if the directory does not exist in the file system.
NotDirectoryException - if the -r value specifies a file in the file system instead of a directory.

getUniqueOptionValue

protected String getUniqueOptionValue(String optionName,
                                      String errorMsg,
                                      boolean isRequired)
                               throws RbUtilsException
Parses the command line options in order to get an option value which must have been specified at most once.

Parameters:
optionName - the name of the option to be parsed.
errorMsg - the error message to be used if a RbUtilsException is thrown.
isRequired - true if the optionName must have been specified.
Returns:
the option value. null if no specified option.
Throws:
RbUtilsException - if the option is specified more than once.

getFilesToBeProcessed

public List getFilesToBeProcessed()
                           throws FileNotFoundException
Returns a list of properties files to be processed (-f option).

NOTE: The parseParameters() and getRootDir() methods must have been called before the call of this method.

Specified by:
getFilesToBeProcessed in interface Utility
Returns:
the files to be processed.
Throws:
FileNotFoundException - if a provided file does not exist in the file system.

getExtension

public String getExtension()
                    throws RbUtilsException
Parses the arguments of this utility in order to get the extension of properties files to be used by this utility.

The Utility.parseParameters() method MUST have been previously called.

Specified by:
getExtension in interface Utility
Returns:
the extension.
Throws:
RbUtilsException - if more than one -e option is specified.

getLoggers

public Collection getLoggers()
                      throws UnSpecifiedFileException,
                             IOException
Parses the arguments of this utility in order to get the loggers to be used by this utility.

Specified by:
getLoggers in interface Utility
Returns:
the Collection of loggers. This will be empty if no loggers are specified.
Throws:
UnSpecifiedFileException - if a Logger to be created requires a logging file and loggingFile is null
IOException - if an error occurs on file logger initialization.

getCommonOptions

protected static Options getCommonOptions()
Returns a copy of the common options shared by all RbUtils command line utilities.

Returns:
the common options.

clone

protected static Options clone(Options options)
Clone an Options instance.

Parameters:
options - the instance to be cloned.
Returns:
the clone.

usage

public final void usage()
Prints the usage information of this utility in the Standard Output.

Specified by:
usage in interface Utility

usage

public final void usage(PrintWriter pw)
Prints the usage information of this utility. The Writer will be flushed at the end of this method but won't be closed.

Specified by:
usage in interface Utility
Parameters:
pw - the Writer where the information will be print.

parse

public final void parse(Exception exception,
                        PrintWriter pw)
Parses an Exception and provides a user readable messages which is printed in the PrintWriter. The usage of the provided utility is also printed.

The Writer is not closed by this method.

Specified by:
parse in interface Utility
Parameters:
exception - the Exception to be parsed.
pw - the Writer where the message is printed.

Back to the RbUtils Home Page

Copyright © 2005-2009 RbUtils Project. All Rights Reserved.