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

CLIDescriptorsReader

public final class CLIDescriptorsReader extends Object
author
pa100654

Fields Summary
public static final int
DONT_SERIALIZE
public static final int
SERIALIZE_COMMANDS_TO_FILES
public static final int
SERIALIZE_COMMANDS_TO_FILE
public static final String
SERIALIZED_DESCRIPTOR_FILE
public static final String
SERIALIZED_COMMANDS_DEFAULT_DIR
public static final String
ENVIRONMENT_PREFIX
public static final String
ENVIRONMENT_FILENAME
private static CLIDescriptorsReader
cliDescriptorsReader
private String
serializeDir
private int
serializeDescriptors
private ValidCommandsList
commandsList
private HashMap
validOptions
private HashMap
replaceOptions
private Vector
descriptors
private Vector
properties
private String
defaultCommand
private String
helpClass
private static final String
DESCRIPTOR_FILE_NAME
Constructors Summary
private CLIDescriptorsReader()
Construct CLIDescriptorsReader object

                    
     
    
        initialize();
    
private CLIDescriptorsReader(URL descriptor)
Construct CLIDescriptorsReader object with the given arguments.

param
descriptor, the URL for the CLI Descriptor file

        setDescriptor(descriptor);
        initialize();
    
private CLIDescriptorsReader(Vector descriptors)
Construct CLIDescriptorsReader object with the given arguments.

param
descriptors, the URL's for the CLI Descriptor file

        this.descriptors = descriptors;
        initialize();
    
Methods Summary
private ValidOptionfindOption(java.lang.String optionStr)
find the option in the options list given the option name

param
optionStr the name of the option
return
ValidOption, the ValidOption if found else return null

        ValidOption option = null;
        option = (ValidOption) validOptions.get(optionStr);
        return option;
    
private ValidCommandgenerateCommand(org.w3c.dom.Document document, org.w3c.dom.Node grandKid)
Generate a Valid Command by populating it with its name, numberofoperands, classname, validoptions, requiredoptions, usage-text and help-text fields

param
document the Document object
param
grandKid, the node in the xml document tree where the command need to be searched
return
returns a ValidCommand

        ValidCommand command = new ValidCommand();

        NamedNodeMap nodeMap = grandKid.getAttributes();
        String nameAttr = nodeMap.getNamedItem("name").getNodeValue();
        String classNameAttr = nodeMap.getNamedItem("classname").getNodeValue();
        String numberOfOperandsAttr = nodeMap.getNamedItem("numberofoperands").getNodeValue();

        String defaultOperandAttr = null;
        if (nodeMap.getNamedItem("defaultoperand") != null)
            defaultOperandAttr = nodeMap.getNamedItem("defaultoperand").getNodeValue();

        Node usageTextNode = nodeMap.getNamedItem("usage-text");
        String usageTextAttr = null;
        if (usageTextNode != null)
            usageTextAttr = usageTextNode.getNodeValue();
        command.setName(nameAttr);
        command.setNumberOfOperands(numberOfOperandsAttr);
        command.setDefaultOperand(defaultOperandAttr);
        command.setClassName(classNameAttr);
        command.setUsageText(usageTextAttr);

        for (Node nextGrandKid = grandKid.getFirstChild(); nextGrandKid != null;
                nextGrandKid = nextGrandKid.getNextSibling()) 
        {
            String grandKidName = nextGrandKid.getNodeName();
            LocalStringsManager lsm = LocalStringsManagerFactory.getFrameworkLocalStringsManager();
            if (grandKidName.equalsIgnoreCase("ValidOption"))
            {
                final NamedNodeMap validOptionNodeMap = 
                                nextGrandKid.getAttributes();
                final String validOption = 
                        validOptionNodeMap.getNamedItem("name").getNodeValue();
                if (command.hasValidOption(validOption)) {
                    throw new CommandValidationException(lsm.getString("OptionAlreadyDefined",
                                                                       new Object[] {validOption,
                                                                       command.getName()} ));
                }
                
                String deprecatedOption = null;
                if (validOptionNodeMap.getNamedItem("deprecatedoption") != null)
                {
                    deprecatedOption = validOptionNodeMap.getNamedItem("deprecatedoption").getNodeValue();
                }
                String defaultValue = null;
                if (validOptionNodeMap.getNamedItem("defaultvalue") != null) {
                    defaultValue = validOptionNodeMap.getNamedItem("defaultvalue").getNodeValue();
                }

                final ValidOption option = findOption(validOption);
                if (option == null) {
                    throw new CommandValidationException(lsm.getString("ValidOptionNotDefined",
                                                                       new Object[] {validOption} ));
                }
                ValidOption newOption = new ValidOption(option);
                if (deprecatedOption != null)
                    newOption.setDeprecatedOption(deprecatedOption);
                if (defaultValue != null)
                    newOption.setDefaultValue(defaultValue);
                
                command.addValidOption(newOption);
            }
            else if (grandKidName.equalsIgnoreCase("RequiredOption"))
            {
                final NamedNodeMap reqdOptionNodeMap = 
                                nextGrandKid.getAttributes();
                final String reqdOption = 
                        reqdOptionNodeMap.getNamedItem("name").getNodeValue();
                if (command.hasRequiredOption(reqdOption)) {
                    throw new CommandValidationException(lsm.getString("OptionAlreadyDefined",
                                                                       new Object[] {reqdOption,
                                                                       command.getName()} ));
                }
                
                String deprecatedOption = null;
                if (reqdOptionNodeMap.getNamedItem("deprecatedoption") != null)
                {
                    deprecatedOption = 
                        reqdOptionNodeMap.getNamedItem("deprecatedoption").getNodeValue();
                }
                final ValidOption option = findOption(reqdOption);
                if (option == null)
                {
                    throw new CommandValidationException(lsm.getString("RequiredOptionNotDefined",
                                                                       new Object[] {reqdOption} ));
                }
                if (deprecatedOption != null)
                    option.setDeprecatedOption(deprecatedOption);
                command.addRequiredOption(new ValidOption(option));
            }
            else if (grandKidName.equalsIgnoreCase("DeprecatedOption"))
            {
                final String deprecatedOption = nextGrandKid.getFirstChild().getNodeValue();
                if (command.hasDeprecatedOption(deprecatedOption)) {
                    throw new CommandValidationException(lsm.getString("OptionAlreadyDefined",
                                                                       new Object[] {deprecatedOption,
                                                                       command.getName()} ));
                }
                
                final ValidOption option = findOption(deprecatedOption);
                if (option == null)
                {
                    throw new CommandValidationException(lsm.getString("DeprecatedOptionNotDefined",
                                                                       new Object[] {deprecatedOption} ));
                }
                command.addDeprecatedOption(option);
            }
            else if (grandKidName.equalsIgnoreCase("properties"))
            {
                for (Node nextGreatGrandKid = nextGrandKid.getFirstChild(); 
                        nextGreatGrandKid != null;
                        nextGreatGrandKid = nextGreatGrandKid.getNextSibling()) 
                {
                    String greatGrandKidName = nextGreatGrandKid.getNodeName();
                    if (greatGrandKidName.equalsIgnoreCase("property"))
                    {
                        nodeMap = nextGreatGrandKid.getAttributes();
                        String propertyNameAttr = 
                                    nodeMap.getNamedItem("name").getNodeValue();

                        Vector<String> values = new Vector<String>(); 
                        for (Node nextGreatGreatGrandKid = nextGreatGrandKid.getFirstChild();
                                nextGreatGreatGrandKid != null;
                                nextGreatGreatGrandKid = nextGreatGreatGrandKid.getNextSibling()) 
                        {
                            String greatGreatGrandKidName = nextGreatGreatGrandKid.getNodeName();
                            if (greatGreatGrandKidName.equalsIgnoreCase("value"))
                            {
				String value = null;

				if (nextGreatGreatGrandKid.getFirstChild() != null)
				    value = nextGreatGreatGrandKid.getFirstChild().getNodeValue();
                                values.add(value);
                            }
                        
                        }
                        command.setProperty(propertyNameAttr, values);
                    }
                }
            }
        }
        
        return command;
    
private ValidOptiongenerateOption(org.w3c.dom.Document document, org.w3c.dom.Node grandKid)
Generate a Valid Option by populating it with its name, value-required, shortoption, default, help-text fields

param
document the Document object
param
grandKid the node in the xml document where the option need to be searched
return
returns a ValidOption

        ValidOption option = new ValidOption();
        NamedNodeMap nodeMap = grandKid.getAttributes();
        String nameAttr = nodeMap.getNamedItem("name").getNodeValue();
        String typeAttr = nodeMap.getNamedItem("type").getNodeValue();
        boolean valueReqdAttr = Boolean.valueOf(nodeMap.getNamedItem("value-required").getNodeValue()).booleanValue();
        Node defaultAttrNode = nodeMap.getNamedItem("default");
        String defaultAttr = null;
        if (defaultAttrNode != null)
            defaultAttr = defaultAttrNode.getNodeValue();
        option.setName(nameAttr);
        option.setType(typeAttr);
        option.setRequired(valueReqdAttr?ValidOption.REQUIRED:ValidOption.OPTIONAL);
        option.setDefaultValue(defaultAttr);
        for (Node nextGrandKid = grandKid.getFirstChild(); nextGrandKid != null;
                nextGrandKid = nextGrandKid.getNextSibling()) 
        {
            String grandKidName = nextGrandKid.getNodeName();
            if (grandKidName.equalsIgnoreCase("shortoption"))
            {
                String shortOption = nextGrandKid.getFirstChild().getNodeValue();
                shortOption = shortOption.trim();
                if (shortOption != null)
                {
                    option.setShortName(shortOption);
                }
            }
        }
        return option;
    
private voidgenerateOptionsAndCommands(org.w3c.dom.Document document)
Get All the Valid, Required, Deprecated Options and the commands from the descriptor file

        if (document != null) 
        {
            for (Node nextKid = document.getDocumentElement().getFirstChild();
                    nextKid != null; nextKid = nextKid.getNextSibling()) 
            {
                String nodeName = nextKid.getNodeName();
                if (nodeName.equalsIgnoreCase("CommandProperties"))
                {
                    Properties props = new Properties();
                    for (Node grandKid = nextKid.getFirstChild();
                       grandKid != null; grandKid = grandKid.getNextSibling()) 
                    {
                        String grandKidNodeName = grandKid.getNodeName();
                        if (grandKidNodeName.equalsIgnoreCase("CommandProperty")) 
                        {
                            NamedNodeMap nodeMap = grandKid.getAttributes();
                            String nameAttr = 
                                    nodeMap.getNamedItem("name").getNodeValue();
                            String valueAttr = 
                                    nodeMap.getNamedItem("value").getNodeValue();
                            props.setProperty(nameAttr, valueAttr);
                        }
                    }
		    final NamedNodeMap commandPropertiesAttribute = nextKid.getAttributes();
		    if (commandPropertiesAttribute != null &&
			commandPropertiesAttribute.getNamedItem("defaultcommand") != null )
			defaultCommand = commandPropertiesAttribute.getNamedItem(
					 "defaultcommand").getNodeValue();
		    if (commandPropertiesAttribute != null &&
			commandPropertiesAttribute.getNamedItem("helpclass") != null )
			helpClass = commandPropertiesAttribute.getNamedItem(
					 "helpclass").getNodeValue();
                                      
                    properties.add(props);
                }
                else if (nodeName.equalsIgnoreCase("Options")) 
                {
                    for (Node grandKid = nextKid.getFirstChild();
                       grandKid != null; grandKid = grandKid.getNextSibling()) 
                    {
                        String grandKidNodeName = grandKid.getNodeName();
                        if (grandKidNodeName.equalsIgnoreCase("Option")) 
                        {
                            ValidOption option = generateOption(document, grandKid);
                            final String optionName =  option.getName();
                                //do not allow duplication option name defined
                            if (!validOptions.containsKey(optionName)) {
                                validOptions.put(optionName, option);
                            }
                            else {
                                LocalStringsManager lsm = 
                                                    LocalStringsManagerFactory.getFrameworkLocalStringsManager();
                                throw new CommandValidationException(lsm.getString("DuplicateOptionDeclaration", new Object[]{optionName}) );
                            }
                            
                        }
                        else if (grandKidNodeName.equalsIgnoreCase("ReplaceOption")) 
                        {
                            ValidOption option = generateOption(document, grandKid);
                            replaceOptions.put(option.getName(), option);
                        }

                    }
                }
                //Generates the Valid Commands List from the descriptor file by 
                // reading all the Valid Commands
                else if (nodeName.equalsIgnoreCase("Commands")) 
                {
                    for (Node grandKid = nextKid.getFirstChild();
                       grandKid != null; grandKid = grandKid.getNextSibling()) 
                    {
                        String grandKidNodeName = grandKid.getNodeName();
                        if (grandKidNodeName.equalsIgnoreCase("Command")) 
                        {
                            ValidCommand command = generateCommand(document, grandKid);
                            commandsList.addCommand(command);
                        }
                    }
                }
            }
        }
    
public ValidCommandgetCommand(java.lang.String commandName)
Returns the command object. if the commandName is null then the default command object is returned.

param
commandName the command name
return
ValidCommand the valid command object

        ValidCommand command = null;
        if ((commandsList == null) || (commandsList.size() == 0))
        {
            command = getCommandFromFileOrDescriptor(commandName);
        }
        else 
        {
            command = commandsList.getValidCommand(commandName);
        }
        return command;
    
private ValidCommandgetCommandFromFileOrDescriptor(java.lang.String commandName)
Returns the command object

param
commandName the command name
return
ValidCommand the valid command object

        ValidCommand command = null;
        
	/************************************************************************/
	/** how should we handle defaultcommand if using serialized descriptor **/
	/************************************************************************/
        if (serializeDescriptors == SERIALIZE_COMMANDS_TO_FILES)
        {
            command = getSerializedCommand(commandName);
            if (command != null)
            {
                return command;
            }
        }

            //preserve serializedDescriptor setting so commands do not get serialized
            //if command could not be found
        int saveSerializedDescriptor = getSerializeDescriptorsProperty();
        setSerializeDescriptorsProperty(DONT_SERIALIZE);
            //setSerializeDescriptorsProperty(SERIALIZE_COMMANDS_TO_FILE);
        loadAllCommandsFromFilesOrDescriptors();
            //put back setting for serizliedDescriptor
        setSerializeDescriptorsProperty(saveSerializedDescriptor);
        if (commandName != null)
            return commandsList.getValidCommand(commandName);
        else if (defaultCommand != null)
            return commandsList.getValidCommand(defaultCommand);
        else
            throw new CommandValidationException(
                LocalStringsManagerFactory.getFrameworkLocalStringsManager().
                getString("CommandNotSpecified", null));

    
public ValidCommandsListgetCommandsList()
Returns the commands list if not already read, the commands are read either from the serialized command file(s) or the command descriptor(s)

return
ValidCommandsList the list of valid commands

        if ((commandsList == null) || (commandsList.size() == 0))
        {
            loadAllCommandsFromFilesOrDescriptors();
        }
        return commandsList;
    
public java.lang.StringgetDefaultCommand()
returns the default command string

return
defaultCommand

	    return defaultCommand;
    
private java.util.VectorgetDescriptors()
gets the descriptors

        if (descriptors != null)
            return descriptors;
        
        try
        {
            String descriptor_file_name = System.getProperty("DESCRIPTOR_FILE");
            if(descriptor_file_name == null)
                descriptor_file_name = DESCRIPTOR_FILE_NAME;
            Enumeration urls = CLIDescriptorsReader.class.getClassLoader().getResources(
                descriptor_file_name);
            if ((urls == null) || (!urls.hasMoreElements()))
            {
                return descriptors;
            }

            while (urls.hasMoreElements())
            {
                URL url = (URL) urls.nextElement();
                setDescriptor(url);
            }
        }
        catch(IOException ioe)
        {
	    LocalStringsManager lsm = 
		LocalStringsManagerFactory.getFrameworkLocalStringsManager();
	    CLILogger.getInstance().printMessage(lsm.getString("CouldNotLoadDescriptor"));
        }
        return descriptors;
    
public java.lang.StringgetEnvironmentFileName()

        if(properties != null){
        for(int i = 0 ; i < properties.size(); i++){
            Properties props = (Properties)properties.get(i);
            String file = props.getProperty(ENVIRONMENT_FILENAME);
            if(file != null)
            {
                return file;
            }
        }
        }
        return ".cliprefs";
    
public java.lang.StringgetEnvironmentPrefix()

        if(properties != null){
            for(int i = 0 ; i < properties.size(); i++){
                Properties props = (Properties)properties.get(i);
                String prefix = props.getProperty(ENVIRONMENT_PREFIX);
                if(prefix != null)
                {
                    return prefix;
                }
            }
        }
        return null;
    
public java.lang.StringgetHelpClass()
returns the help class string

return
helpClass

    	return helpClass;
    
public static synchronized com.sun.enterprise.cli.framework.CLIDescriptorsReadergetInstance()
returns the instance of the CLIDescriptorsReader

        if (cliDescriptorsReader == null)
        {
            cliDescriptorsReader= new CLIDescriptorsReader();
        }
        return cliDescriptorsReader;
    
public java.util.IteratorgetProperties()
Returns the properties list of all the commands module's

return
Iterator the list of Properties

        return properties.iterator();
    
private intgetSerializeDescriptorsProperty()
returns the serializeDescriptors property

        return serializeDescriptors;
    
public java.lang.StringgetSerializeDir()
Returns the File object of the directory containing the multile serialized files, each containing a command

return
File file object of the directory containing all the command files

        return serializeDir;
    
private ValidCommandgetSerializedCommand(java.lang.String commandName)
Loads the specified serialized command from the file in the SERIALIZED_COMMANDS_DIR

param
commandFile file name of the command to be read
return
returns the ValidCommand object


        ValidCommand command  = null;
        String encodedCommandName = null;
		try{
		    encodedCommandName = URLEncoder.encode(commandName,"UTF-8");
			}catch(UnsupportedEncodingException ue){
                encodedCommandName = commandName;
			}
        final InputStream in = CLIDescriptorsReader.class.getClassLoader().getResourceAsStream(serializeDir + "/." + encodedCommandName);
        
        if (in!=null)
        {
            command = getSerializedCommand(in);
        }
        else {
            return null;
        }
         
        /*
        File fileName = new File(serializeDir + "/." + commandName);
        if (fileName.exists())
        {
            command = getSerializedCommand(fileName);
        }
        */
        
        return command;
    
private ValidCommandgetSerializedCommand(java.io.InputStream commandFile)
Loads the specified serialized command from the file in the SERIALIZED_COMMANDS_DIR

param
commandFile file name of the command to be read

        try
        {
            ValidCommand command = null;
            ObjectInputStream in = new ObjectInputStream(commandFile);
            
                //ObjectInputStream in = new ObjectInputStream(
                //                        new FileInputStream(commandFile));
            
            /** read CommandProperties to serialized file **/
            defaultCommand = (String) in.readObject();
            helpClass = (String) in.readObject();
            properties = (Vector) in.readObject();

            command = (ValidCommand) in.readObject();
    	    CLILogger.getInstance().printDebugMessage("++++++++++++++++++++++++++++ Command loaded from file and it is " + command);
            return command;
        }
        catch (Exception e)
        {
	    throw new CommandValidationException(e);
        }
    
private java.lang.String[]getSerializedCommandsFileList(java.io.File commandsDir)

        String[] commandFiles = commandsDir.list(new FilenameFilter() {
            public boolean accept(File parentDir, String name) {
                return (name.startsWith("."));
            }});
        return commandFiles;
    
private java.util.VectorgetSerializedDescriptorFile()
Returns the Vector contains all the serialized descriptor files.

return
Vector. an empty vector is returned if there are no serialized descriptor file.

        Vector<URL> descriptorFiles = new Vector<URL>();
        try
        {
            final String sDescriptorFile = serializeDir + "/" +
                                           SERIALIZED_DESCRIPTOR_FILE;
            
            Enumeration<URL> urls = CLIDescriptorsReader.class.getClassLoader().getResources(sDescriptorFile);
            if ((urls == null) || (!urls.hasMoreElements()))
            {
                //return an empty vector
                return descriptorFiles;
            }

            while (urls.hasMoreElements())
            {
                URL url = (URL) urls.nextElement();
                descriptorFiles.add(url);
            }
        }
        catch(IOException ioe)
        {
   	       LocalStringsManager lsm = 
		   LocalStringsManagerFactory.getFrameworkLocalStringsManager();
	       CLILogger.getInstance().printMessage(lsm.getString("CouldNotLoadDescriptor"));
        }
        return descriptorFiles;
    
private voidinitialize()
Initialize the CLIDescriptorsReader object and read the components

        commandsList = new ValidCommandsList();
        validOptions = new HashMap<String, ValidOption>();
        replaceOptions = new HashMap<String, ValidOption>();
        properties = new Vector<Properties>();
    
private voidloadAllCommandsFromFilesOrDescriptors()
Initialize the commandsList based on the serializeDescriptors property

        if (serializeDescriptors == DONT_SERIALIZE)
        {
            readDescriptors();
        }
        else if (serializeDescriptors == SERIALIZE_COMMANDS_TO_FILES)
        {
                // currently not supported
            loadCommandsFromMultipleFiles();
        }
        else if (serializeDescriptors == SERIALIZE_COMMANDS_TO_FILE)
        {
            loadCommandsFromSingleFile();
        }
    
private voidloadCommandsFromMultipleFiles()

            //currently not supported and may not be a feasible feature
            //since reading all the serialized commands is timing consuming.
            //so for now, read the descriptor file only.
        readDescriptors();
    
private voidloadCommandsFromSingleFile()

        final Vector<URL> serializedDescriptorFile = getSerializedDescriptorFile();

            //if there are no serialized descriptor file, then directly read 
            //and parse the CLIDescriptor.xml file
        if (serializedDescriptorFile.size() > 0) {
            loadSerializedDescriptorFile(serializedDescriptorFile);
        }
        else {
            readDescriptors();
        }
    
private booleanloadSerializedCommands()
Loads the serialized commands from the files in the SERIALIZED_COMMANDS_DIR

return
true if loaded succesfully else false

        File commandsDir = new File(serializeDir);
        String[] commandFiles = getSerializedCommandsFileList(commandsDir);

        if (!((commandsDir != null) && (commandsDir.exists()) && 
              (commandFiles.length > 0)))
        {
            return false;
        }
        
        boolean loaded = true;
        for (int i = 0; (i < commandFiles.length) && loaded; i++)
        {
            //File commandFile = new File(commandFiles[i]);
            final InputStream in = CLIDescriptorsReader.class.getClassLoader().getResourceAsStream(serializeDir + "/." + commandFiles[i] );

            try
            {
                ValidCommand command = getSerializedCommand(in);
                //ValidCommand command = getSerializedCommand(commandFile);
                if (command != null)
                {
                    commandsList.addCommand(command);
                }
                else
                {
                    loaded = false;
                }
            }
            catch (CommandValidationException e)
            {
                loaded = false;
                break;
            }
        }
        return loaded;
    
private voidloadSerializedDescriptorFile(java.util.Vector descriptorFile)
Loads commands from serialized descriptor file

        try
        {
            for (int ii=0; ii<descriptorFile.size(); ii++) {
                final URL descriptorURL = (URL) descriptorFile.get(ii);
                ObjectInputStream ois = new ObjectInputStream(descriptorURL.openStream());
                
                defaultCommand = (String) ois.readObject();
                helpClass = (String) ois.readObject();
                properties = (Vector) ois.readObject();
                
                final ValidCommandsList validCommandsList = (ValidCommandsList)ois.readObject();
                    //if commandsList is not empty then need to load each
                    //command in commandsList.
                if (commandsList != null  && commandsList.size() > 0) {
                    final Iterator<ValidCommand> commands = validCommandsList.getCommands();
                    while (commands.hasNext()) {
                        commandsList.addCommand((ValidCommand)commands.next());
                    }
                }
                else {
                    commandsList = validCommandsList;
                }
                
                CLILogger.getInstance().printDebugMessage("++++++++++++++++++++++++++++ Commands loaded from file");
            }
        }
        catch(FileNotFoundException fnfe)
        {
            throw new CommandValidationException(fnfe);
        }
        catch(IOException ioe)
        {
            throw new CommandValidationException(ioe);
        }
        catch(ClassNotFoundException cnfe)
        {
            throw new CommandValidationException(cnfe);
        }
    
public static voidmain(java.lang.String[] args)
main method to test if the functionality works

        try
        {
            //read CLI descriptor
            final CLIDescriptorsReader cliDescriptorsReader = CLIDescriptorsReader.getInstance();
            
            final String SERIALIZE_TYPE = System.getProperty("SERIALIZE_TYPE");

                //default is to serialize the commands to files
            if (SERIALIZE_TYPE == null)
               cliDescriptorsReader.setSerializeDescriptorsProperty(CLIDescriptorsReader.SERIALIZE_COMMANDS_TO_FILES);
            else {
                if (SERIALIZE_TYPE.equals("SINGLE_FILE"))
                    cliDescriptorsReader.setSerializeDescriptorsProperty(CLIDescriptorsReader.SERIALIZE_COMMANDS_TO_FILE);
                if (SERIALIZE_TYPE.equals("MULTIPLE_FILES"))
                    cliDescriptorsReader.setSerializeDescriptorsProperty(CLIDescriptorsReader.SERIALIZE_COMMANDS_TO_FILES);
                if (SERIALIZE_TYPE.equals("DONT_SERIALIZE"))            
                    cliDescriptorsReader.setSerializeDescriptorsProperty(CLIDescriptorsReader.DONT_SERIALIZE);
            }
            

            cliDescriptorsReader.readDescriptors();

                /*
            while(commands.hasNext())
                System.out.println((ValidCommand)commands.next());
            Iterator propertiesList = cliDescriptorsReader.getProperties();
            System.out.println("Properties = ");
            while (propertiesList.hasNext())
            {
                Properties properties = (Properties) propertiesList.next();
                System.out.println(properties.toString());
            }
            System.out.println(cliDescriptorsReader.getCommand("deploy"));
                */

        }
        catch(CommandValidationException cve)
        {
            System.out.println(cve.getLocalizedMessage());
            System.exit(1);
        }
    
public voidreadDescriptors()
Parse and read the XML Descriptors file and populate it into commandsList

        commandsList.removeAllCommands();
        //DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //factory.setValidating(true);   
        //factory.setNamespaceAware(true);
        try 
        {
            // setup schema processing
            DOMParser parser = new DOMParser();
            parser.setFeature( "http://apache.org/xml/features/dom/defer-node-expansion", true);
            parser.setFeature( "http://xml.org/sax/features/validation", true );
            parser.setFeature( "http://xml.org/sax/features/namespaces", true );
            parser.setFeature( "http://apache.org/xml/features/validation/schema", true );
            parser.setFeature ("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
            parser.setEntityResolver(new CLIEntityResolver());

            //factory.setValidating(true);
	        // CLILogger.getInstance().printDebugMessage("Validation against DTD is " + factory.isValidating());
            //DocumentBuilder builder = factory.newDocumentBuilder();
            if (descriptors == null)
            {
                getDescriptors();
                if (descriptors == null) 
		{
		    LocalStringsManager lsm = 
			LocalStringsManagerFactory.getFrameworkLocalStringsManager();
		    throw new CommandValidationException(lsm.getString("NoDescriptorsDefined"));
		}
            }
            for (int i = 0; i < descriptors.size(); i++)
            {
                CLILogger.getInstance().printDebugMessage(i + " descriptor = " + descriptors.get(i));
                
                URL descriptorURL = (URL) descriptors.get(i);
                //InputSource is = new InputSource(descriptorURL.toString());
                //Document document = builder.parse(is);
                parser.parse(new InputSource(descriptorURL.toString()));
                Document document = parser.getDocument();
                generateOptionsAndCommands(document);
            }
                //call this last after all descriptors have been read since we want to
                //replace all the options with the options defined in ReplaceOptions
                //element in the last XML in the list.
            replaceOptionsInCommandsList(replaceOptions);
            validateOptionsInCommand();
            
            if (serializeDescriptors == SERIALIZE_COMMANDS_TO_FILES)
            {
                saveCommandsAsMultipleFiles();
            }
            else if (serializeDescriptors == SERIALIZE_COMMANDS_TO_FILE)
            {
                saveCommandsAsSingleFile();
            }

        } 
        catch (SAXException sxe) 
        {
           // Error generated during parsing)
            Exception  x = sxe;
            if (sxe.getException() != null)
               x = sxe.getException();
	    throw new CommandValidationException(x);
        }
        catch (IOException ioe) 
        {
            // I/O error
	    throw new CommandValidationException(ioe);
            //throw new CommandValidationException(ioe.getLocalizedMessage());
        }
    
private voidreplaceOptionsInCommandsList(java.util.HashMap replaceOptions)

        Iterator replaceOptionNames = replaceOptions.keySet().iterator();
        while (replaceOptionNames.hasNext())
        {
            final String replaceOptionName = (String) replaceOptionNames.next();
            Iterator commands = commandsList.getCommands();
            while (commands.hasNext())
            {
                ValidCommand command = (ValidCommand) commands.next();
                if ( command.hasValidOption(replaceOptionName) ||
                     command.hasRequiredOption(replaceOptionName) ||
                     command.hasDeprecatedOption(replaceOptionName) )
                {
                    command.replaceAllOptions((ValidOption)replaceOptions.get(replaceOptionName));
                }
            }
        }
    
private voidsaveCommandsAsMultipleFiles()
serialize the ValidCommandsList object to file named SERIALIZED_COMPONENTS_FILE

        Iterator commands = commandsList.getCommands();
        while (commands.hasNext())
        {
            try
            {
                ValidCommand command = (ValidCommand) commands.next();
                File file = new File(serializeDir);
                if (!file.exists())
                {
                    file.mkdir();
                }
                String encodedCommandName = null; 
				try{
                    encodedCommandName = URLEncoder.encode(command.getName(),"UTF-8");
				}catch(UnsupportedEncodingException ue){
                    encodedCommandName = command.getName();
				}
                ObjectOutputStream os = new ObjectOutputStream(
                                           new FileOutputStream(serializeDir + 
                                                    "/."+ encodedCommandName));
                /** write CommandProperties to serialized file **/
                os.writeObject(defaultCommand);
                os.writeObject(helpClass);
                os.writeObject(properties);
                    
                os.writeObject(command);
            }
            catch(Exception e)
            {
		//do we want to escalate this exception or just print a message.
		LocalStringsManager lsm = 
		    LocalStringsManagerFactory.getFrameworkLocalStringsManager();
		CLILogger.getInstance().printWarning(lsm.getString(
						     "CouldNotWriteCommandToFile", 
						     new Object[] {e.getMessage()} ));
            }
        }
    
private voidsaveCommandsAsSingleFile()
serialize the ValidCommand objects to file with cli/.{command_name}

        try
        {
            ObjectOutputStream os = new ObjectOutputStream(
                                       new FileOutputStream(serializeDir + 
                                            "/"+ SERIALIZED_DESCRIPTOR_FILE)); 
            os.writeObject(defaultCommand);
            os.writeObject(helpClass);
            os.writeObject(properties);
                    
            os.writeObject(commandsList);
        }
        catch(Exception e)
        {
	    //do we want to escalate this exception or just print a message.
	    LocalStringsManager lsm = 
		LocalStringsManagerFactory.getFrameworkLocalStringsManager();
	    CLILogger.getInstance().printWarning(lsm.getString(
						 "CouldNotWriteComponentToFile", 
						 new Object[] {e.getMessage()} ));

        }
    
public voidsetDescriptor(java.net.URL descriptor)
Sets the descriptor file

param
descriptor the descriptor file containing the command specifications

        if (descriptor != null)
        {
            if (descriptors == null)
            {
                descriptors = new Vector<URL>();
            }
            descriptors.add(descriptor);
            //reverse the order in the Vector so that the last CLIDescriptor.xml will be parsed first.
            java.util.Collections.reverse(descriptors);
        }
    
public voidsetDescriptors(java.util.Vector descriptors)
Sets the descriptor files (components)

param
descriptors the descriptor files containing the command specifications

        this.descriptors = descriptors;
    
public voidsetSerializeDescriptorsProperty(int serializeDescriptors)
set the serializeDescriptors property. DONT_SERIALIZE CLIDescriptor's will not be loaded from the file nor will be saved (serialized) to a file SERIALIZE_COMMANDS_TO_FILE CLIDescriptor's will be loaded/saved from/to a single file (cli/.CLIDescriptors) if exists SERIALIZE_COMMANDS_TO_FILES CLIDescriptor's will be loaded/saved from the multiple files (cli/.{command_name's}) if exists

        if ((serializeDescriptors <= 2) && (serializeDescriptors >=0))
            this.serializeDescriptors = serializeDescriptors;
    
private voidsetSerializeDir(java.lang.String serializeDir)

        try
        {
            File file = new File(serializeDir);
            if (file.exists())
            {
                this.serializeDir = serializeDir;
            }
            else
            {
		LocalStringsManager lsm = 
		    LocalStringsManagerFactory.getFrameworkLocalStringsManager();
		CLILogger.getInstance().printWarning(lsm.getString(
						     "InvalidFilePath", 
						     new Object[] {serializeDir}));
            }
        }
        catch (NullPointerException npe)
        {
	    LocalStringsManager lsm = 
		LocalStringsManagerFactory.getFrameworkLocalStringsManager();
            throw new CommandValidationException(lsm.getString("CouldNoSetSerializeDirectory"),
						 npe);
        }
    
private voidvalidateOptionsInCommand()
This method checks for clashing option names and short options in a command.

        final Iterator commands = commandsList.getCommands();
        while (commands.hasNext())
        {
            final ValidCommand command = (ValidCommand) commands.next();
            Vector<ValidOption> vvo = command.getOptions();
            final LocalStringsManager lsm = LocalStringsManagerFactory.getFrameworkLocalStringsManager();
            
            
            int ii = 1;
            final int size = vvo.size();
            for (ValidOption vo : vvo)
            {
                final String optionName = vo.getName();
                    //get rest of the options and compare
                if (ii<size) {
                    java.util.List<ValidOption> sub_vvo = vvo.subList(ii,size);
                    for (ValidOption sub_vo : sub_vvo)
                    {
//System.out.println("***** Compare: optionName = " + optionName + " and " + sub_vo.getName() + " for " + command.getName());
                        if (optionName.equals(sub_vo.getName()))
                        {
                            throw new CommandValidationException(lsm.getString("OptionAlreadyDefined",
                                                                 new Object[] {optionName, command.getName()} ));
                        }
                            //check short option clash
                        if (vo.hasShortName()) 
                        {
                            final Vector<String> shortNames = vo.getShortNames();
                            for (String shortName : shortNames)
                            {
                                final Vector<String> sub_shortNames = sub_vo.getShortNames();
                                if (sub_shortNames.contains(shortName))
                                {
                                    throw new CommandValidationException(lsm.getString("ShortOptionAlreadyDefined",
                                                                         new Object[] {shortName,
                                                                         vo.getName(), sub_vo.getName(),
                                                                         command.getName()} ));
                                }
                            }
                        }
                    }
                }
                ii++;
            }
        }