FileDocCategorySizeDatePackage
Command.javaAPI DocGlassfish v2 API18298Fri May 04 22:25:20 BST 2007com.sun.enterprise.cli.framework

Command

public abstract class Command extends Object implements ICommand
Command object is the super Base class for all the commands. This is a generic Command Class which would fit into any Command Line Interface (CLI) module.
version
$Revision: 1.10 $

Fields Summary
private static final char
REPLACE_START_CHAR
private static final char
REPLACE_END_CHAR
private static final char
VARIABLE_START_CHAR
private static final char
OPERAND_START_CHAR
private static final String
PATTERN_MATCHING
protected String
name
protected OptionsMap
optionsMap
protected Vector
operands
protected String
usageStr
protected Hashtable
properties
Constructors Summary
public Command()
Creates new Command

    
        
      
    
        operands = new Vector();
        optionsMap = new OptionsMap();
    
Methods Summary
private java.lang.StringaddEscapeToLiteral(java.lang.String strToAdd)
This method adds escape character preceding the '$' character. The reason for doing this is because match.appendReplacement will throw an IllegalArgumentException if the literal '$' does not precede by an escape character. The java api class Matcher, uses the regular expression '$' as a group reference. So if it encounters '$ it'll throw an IllegalArgumentException. In order to by pass this exception, the literal '$' must precede with an escape character.

param
strToEscape - to the string to check for '$'
returns
the string with the esacpe character added to '$'

        StringBuffer strbuf = new StringBuffer();
        int previousIndex =0;
        int index = strToAdd.indexOf('$");
        while (index > -1)
        {
            strbuf.append(strToAdd.substring(previousIndex, index)+"\\$");
            previousIndex = index+1;
            index = strToAdd.indexOf('$", index+1);
        }
        strbuf.append(strToAdd.substring(previousIndex));
        return strbuf.toString();

    
private java.lang.StringescapeTheEscape(java.lang.String strToEscape)
This method adds escape character to each escape chars encounters the purpose for doing this is is that match.appendReplacement drops the escape character.

param
strToEscape - to the string to check for escape characters
returns
the string with the esacpe characters escaped

        StringBuffer strbuf = new StringBuffer();
        int previousIndex =0;
        int index =strToEscape.indexOf("\\");
        while (index > -1)
        {
            if (index<strToEscape.length()-1 && strToEscape.charAt(index+1) == '$")
                strbuf.append(strToEscape.substring(previousIndex, index));
            else
                strbuf.append(strToEscape.substring(previousIndex, index)+"\\\\");
            previousIndex = index+1;
            index = strToEscape.indexOf("\\", index+1);
        }
        strbuf.append(strToEscape.substring(previousIndex));
        return strbuf.toString();
    
private java.lang.StringfindPatternStringValue(java.lang.String pattern, java.lang.String key)
this method finds the pattern for option and operand option pattern starts with "$" while operand pattern starts with "#" once key is determined, the option or operand value is returned

param
pattern
param
key to get option or operand values
return
option/operand value if could not find value, then empty string is returned

        String value = null;
        try 
        {
            if (pattern.equals(String.valueOf(OPERAND_START_CHAR)))
            {
                if (operands.size() > 0)
                    value = (String)getOperands().get(Integer.parseInt(key)-1);
            }
            else if (pattern.equals(String.valueOf(VARIABLE_START_CHAR)))
                value = getOption(key);
        }
        catch(Exception e)
        {
            throw new CommandException(e);
        }
        // (bug 6363010), temporary fix, return null and make sure this isn't 
        //  propagated into the attributeList to the backend
        //return (value==null)?"":value;
        return value;
    
protected booleangetBooleanOption(java.lang.String optionName)
Finds the option with the give name

return
boolean return boolean type of the option value

        return Boolean.valueOf(getOption(optionName)).booleanValue();
    
public java.lang.StringgetCLOption(java.lang.String optionName)
Finds the option with the give name

return
Option return option if found else return null

        Map<String, String> map = this.optionsMap.getCLOptions();
        return map.get(optionName);
    
public java.util.MapgetCLOptions()
Gets the list of options read from command line for this Command

return
Map of options for this command

        return this.optionsMap.getCLOptions();
        
    
protected intgetDelimeterIndex(java.lang.String searchStr, java.lang.String delimeter, int fromIndex)
returns the index of the delimeter string in the search string.

param
searchStr the string to be searched
param
delimeterStr the delimeter string to be searched in the searchStr
param
fromIndex the delimeter to be searched at the specified location
return
delimeterIndex starting index of the delimeter in search string

        return searchStr.indexOf(delimeter, fromIndex);
    
public java.lang.StringgetENVOption(java.lang.String optionName)
Finds the option with the give name

return
Option return option if found else return null

        Map<String, String> map = this.optionsMap.getEnvOptions();
        return map.get(optionName);
    
public java.util.MapgetENVOptions()
Gets the list of options read from environment for this Command

return
Map of options for this command

        return this.optionsMap.getEnvOptions();
    
protected intgetIntegerOption(java.lang.String optionName)
Finds the option with the give name

return
Option return option if found else return -1

        //assert
        assert(!optionNameExist(optionName));
        return (Integer.valueOf(getOption(optionName)).intValue());
    
protected java.lang.StringgetLocalizedString(java.lang.String key)
returns the localized string from the properties file as defined in the CommandProperties element of CLIDescriptor.xml file Calls the LocalStringsManagerFactory.getCommandLocalStringsManager() method, returns "Key not found" if it cannot find the key

param
key, the string to be localized

        LocalStringsManager lsm = null;
        try
        {
            lsm = LocalStringsManagerFactory.getCommandLocalStringsManager();
        }
        catch (CommandValidationException cve)
        {
            return LocalStringsManager.DEFAULT_STRING_VALUE;
        }
        return lsm.getString(key);
    
protected java.lang.StringgetLocalizedString(java.lang.String key, java.lang.Object[] toInsert)
returns the localized string from the properties file as defined in the CommandProperties element of CLIDescriptor.xml file Calls the LocalStringsManagerFactory.getCommandLocalStringsManager() method, returns "Key not found" if it cannot find the key

param
key, the string to be localized
param
toInsert, the strings to be inserted in the placeholders

        LocalStringsManager lsm = null;
        try
        {
            lsm = LocalStringsManagerFactory.getCommandLocalStringsManager();
            return lsm.getString(key, toInsert);
        }
        catch (CommandValidationException cve)
        {
            return LocalStringsManager.DEFAULT_STRING_VALUE;
        }
    
public java.lang.StringgetName()
Gets the name of the command

return
String the name of the command

        return name;
    
public java.util.VectorgetOperands()
Gets the list of operands of this command

return
Vector the list of operands.

        return operands;
    
public java.lang.StringgetOption(java.lang.String optionName)
Finds the option with the give name

return
Option return option if found else return null

        return this.optionsMap.getOption(optionName);
    
public java.util.MapgetOptions()
Gets the list of options for this Command

return
Vector List of options for this command

        return this.optionsMap.getOptions();
    
public java.lang.StringgetOtherOption(java.lang.String optionName)
Finds the option with the give name Options set bo "Other" are usually set by the command module

return
Option return option if found else return null

        Map<String, String> map = this.optionsMap.getOtherOptions();
        return map.get(optionName);
    
protected java.util.HashtablegetProperties(java.lang.String key)
returns the properties list return the property

        return properties;
    
public java.lang.ObjectgetProperty(java.lang.String key)
Searches for the property with the specified key in this properties list return the property

        return properties.get(key);
    
public java.lang.StringgetUsageText()
Gets the Usage text for this command

return
String returns usage text for this command

        return usageStr;
    
private booleanoptionNameExist(java.lang.String optionName)
returns true if the option name exist in the options list

true
if option name exist

        return this.optionsMap.containsName(optionName);
    
private java.lang.StringprepareStringForAppend(java.lang.String str)
this method is a hack to add escape character to each esacpe chars encounters except the escape character is follow by a '$'. match.appendReplacement will throw an IllegalArgumentException if literal $ does not preceed by an escape char. the purpose for doing this is for the match.appendReplacement which drops the escape character.

        final String strTmp = escapeTheEscape(str);
        return addEscapeToLiteral(strTmp);
    
public java.lang.StringreplacePattern(java.lang.String replaceValue)
this method replaces pattern with option or operand values

param
replaceValue - value to replace
param
regexp - regular expression pattern
return
replaced string or null if could not replace string

        if (replaceValue == null) return null;
        final Pattern patt = Pattern.compile(PATTERN_MATCHING);
        final Matcher match = patt.matcher(replaceValue);
        String outstr = replaceValue;
        
        try 
        {
            if (match.find()) 
            {
                StringBuffer strbuf = new StringBuffer();
                do 
                {
                    String value = findPatternStringValue(match.group(1),
                                                          match.group(2));
                        //value = escapeTheEscape(value);
                    if (value == null) 
                        return value;
                    value = prepareStringForAppend(value);
                    match.appendReplacement(strbuf, value);
                    CLILogger.getInstance().printDebugMessage("strbuf = " + strbuf);
                } while (match.find());
                match.appendTail(strbuf);
                outstr = strbuf.toString();
            }
        }
        catch (java.lang.IllegalArgumentException iae)
        {
            try 
            {
                final LocalStringsManager lsm = 
                LocalStringsManagerFactory.getFrameworkLocalStringsManager();
                throw new CommandException(lsm.getString("RequireEscapeChar"), iae);
            }
            catch (CommandValidationException cve)
            {
                throw new CommandException(cve);
            }
        }
        catch (Exception e)
        {
            throw new CommandException(e);
        }
        return (outstr.length()<1)?null:outstr;
    
public abstract voidrunCommand()
An abstract method that Executes the command

throws
CommandException

public voidsetName(java.lang.String name)
Sets the name of the command

param
String the name of the command

        this.name = name;
    
public voidsetOperands(java.util.Vector operands)
Sets the list of operands for this command

param
Vector the list of operands.

        this.operands = operands;
    
public voidsetOption(java.lang.String optionName, java.lang.String optionValue)
Sets the option value for the give name

param
optionName name of the option
param
optionValue value of the option

        this.optionsMap.addOptionValue(optionName, optionValue);
    
public voidsetOptionsMap(OptionsMap options)
Sets OptionsMap

param
options List of options for this command

        this.optionsMap = options;
    
protected voidsetProperties(java.util.Hashtable properties)
Sets the properties of the command

param
properties, the list of properties for this command

        this.properties = properties;
    
public voidsetProperty(java.lang.String key, java.lang.Object value)
Sets the properties of the command

param
properties, the list of properties for this command

        properties.put(key, value);
    
public voidsetUsageText(java.lang.String usageText)
Sets the Usage text for this command

param
String usage-text for this command

        this.usageStr = usageText;
    
public java.lang.StringtoString()
Overrides the Object's toString() method

return
String The string representation of Command

        StringBuffer strbuf = new StringBuffer();

        strbuf.append(getName());
        final Map<String, String> clOptions = this.optionsMap.getOptions();

        Iterator optionNames = clOptions.keySet().iterator();
        while (optionNames.hasNext())
        {
            final String optionKey = (String) optionNames.next();
            strbuf.append(" --" + optionKey );
            final String optionVal = (String)clOptions.get(optionKey);
                //check if the value is boolean
            if (Boolean.TRUE.toString().equalsIgnoreCase(optionVal) ||
                Boolean.FALSE.toString().equalsIgnoreCase(optionVal) )
                strbuf.append("=");
            else
                strbuf.append(" ");
            strbuf.append(optionVal);
        }
        for (int ii=0; ii<operands.size(); ii++)
        {
            strbuf.append(" "+ operands.get(ii).toString());
        }
        return strbuf.toString();
    
public abstract booleanvalidateOptions()
An abstract method that validates the options on the specification in the xml properties file This method verifies for the correctness of number of operands and if all the required options are supplied by the client.

return
boolean returns true if success else returns false