/**
     From the necessary file, read the domains and return a unique list
     of potential domains.
     
     */
    vector<SCXFilePath> WebLogicFileReader::GetDomains()
    {
        SCX_LOGTRACE(m_log, L"WebLogicFileReader::GetDomains");

        vector<SCXFilePath> domains;

        // Logic necessary for reading WebLogic 11g domains
        SCXFilePath domainRegistryXml;
        domainRegistryXml.SetDirectory(m_installationPath);
        domainRegistryXml.SetFilename(
                WEBLOGIC_DOMAIN_REGISTRY_XML_FILENAME);
        
        if (DoesDomainRegistryXmlExist(domainRegistryXml))
        {
            ReadDomainRegistryXml(domainRegistryXml, domains);
        }

        // Logic necessary for reading WebLogic 10g domains
        SCXFilePath nodemanagerDomains;
        nodemanagerDomains.SetDirectory(m_installationPath);
        nodemanagerDomains.AppendDirectory(WEBLOGIC_NODEMANAGER_DOMAINS_DIRECTORY);
        nodemanagerDomains.SetFilename(WEBLOGIC_NODEMANAGER_DOMAINS_FILENAME);

        if (DoesNodemanagerDomainsExist(nodemanagerDomains))
        {
            ReadNodemanagerDomains(nodemanagerDomains, domains);
        }
        
        // There may be duplicates in the list, it is necessary to
        // sort the list of domains and return only the unique instances.
        sort(domains.begin(), domains.end(), SortPath());
        vector<SCXFilePath>::iterator tmp = 
                unique(domains.begin(), domains.end());
        domains.resize(tmp-domains.begin());
                
        SCX_LOGTRACE(m_log, 
                wstring(L"WebLogicFileReader::GetDomains() - ").
                append(L"Found ").append(StrFrom(domains.size())).append(L" domain(s)"));

        return domains;
    }
    /**
       From the necessary XML configuration file, read information about
       the known instances.
       
       The file should be:
           ${Install}/${Domain}/config/config.xml

       \param[in]  domains    List of domains to find instances for

       \param[out] instances  vector to add instances to
       
    */    
    void WebLogicFileReader::GetInstances(
            const SCXFilePath& domain,
            vector<SCXHandle<AppServerInstance> >& instances)
    {
        SCXFilePath configXml;
        configXml.SetDirectory(domain.Get());
        configXml.AppendDirectory(WEBLOGIC_CONFIG_DIRECTORY);
        configXml.Append(WEBLOGIC_CONFIG_FILENAME);
        
        if (DoesConfigXmlExist(configXml))        
           {
            SCX_LOGTRACE(m_log, 
                    wstring(L"WebLogicFileReader::GetInstances() - ").
                    append(L"Reading ").append(configXml.Get()));
            ReadConfigXml(domain, configXml, instances);
           }
        else
        {
            SCX_LOGTRACE(m_log, 
                    wstring(L"WebLogicFileReader::GetInstances() - ").
                    append(L"Expected configuration file '").
                    append(configXml.Get()).append(L"' does not exist."));
        }
    }
    /**
       Read a simple XML file to find the locations of the both 
       the Admin and Managed servers for a WebLogic 11g R1 installation.
       
       Example:
       <?xml version="1.0" encoding="UTF-8"?>
       <domain ...>
         <name>base_domain</name>
         <domain-version>10.3.2.0</domain-version>
         <security-configuration ...>
            ...
         </security-configuration>
         <server>
           <name>AdminServer</name>
           <ssl>
             <name>AdminServer</name>
             <enabled>true</enabled>
             <listen-port>7012</listen-port>
           </ssl>
           <machine>new_UnixMachine_1</machine>
           <listen-port>7011</listen-port>
           <listen-address/>
         </server>
         <server>
           <name>new_ManagedServer_1</name>
           <ssl>
             <name>new_ManagedServer_1</name>
             <enabled>true</enabled>
             <listen-port>7513</listen-port>
           </ssl>
           <machine>new_UnixMachine_1</machine>
           <listen-port>7013</listen-port>
           <listen-address/>
         </server>
         <embedded-ldap>
           <name>base_domain</name>
           <credential-encrypted>{AES}RVX+Cadq8XJ5EvV7/1Ta2qGZrJlxve6t5CEa2A9euGUkYOMDTAwAqytymqDBS00Q</credential-encrypted>
         </embedded-ldap>
         <configuration-version>10.3.2.0</configuration-version>
         <machine xsi:type="unix-machineType">
           <name>new_UnixMachine_1</name>
           <node-manager>
             <name>new_UnixMachine_1</name>
             <listen-address>localhost</listen-address>
             <listen-port>5566</listen-port>
           </node-manager>
         </machine>
         <admin-server-name>AdminServer</admin-server-name>
       </domain>

              \param[in]  domainDir         Directory of the domain (needed
                                            for build the path to the server
       
              \param[in]  configXml         File object of the XML file
                                            to open.
                                            
              \param[out] instances         vector that will contain the
                                            list of server instances for the
                                            given domain.
       
     */
    void WebLogicFileReader::ReadConfigXml(
            const SCXFilePath& domainDir,
            const SCXFilePath& configXml,
            vector<SCXHandle<AppServerInstance> >& instances)
    {
        SCX_LOGTRACE(m_log, L"WebLogicFileReader::ReadConfigXml");
        SCX_LOGTRACE(m_log, 
                wstring(L"WebLogicFileReader::ReadConfigXml() - ").
                append(L"Reading the file: ").append(configXml.Get()));
        string xml;

        try {
            SCXHandle<istream> reader = 
                    OpenConfigXml(configXml.Get());
            GetStringFromStream(reader, xml);

            XElementPtr domainNode;
            XElement::Load(xml, domainNode);
            
            if (domainNode->GetName() == WEBLOGIC_DOMAIN_XML_NODE)
            {
                string version;
                ReadConfigXmlForVersion(domainNode, version);
                
                string adminServerName;
                ReadConfigXmlForAdminServerName(domainNode, adminServerName);
                
                XElementList serverNodes;
                domainNode->GetChildren(serverNodes);
                for (size_t index = 0; index < serverNodes.size(); ++index)
                {
                    if (serverNodes[index]->GetName() == WEBLOGIC_SERVER_XML_NODE)
                    {
                        bool isAdminServer = false;
                        bool isSslEnabled = false;
                        string name = "";
                        string httpPort = "";
                        string httpsPort = "";

                        XElementList childNodes;
                        serverNodes[index]->GetChildren(childNodes);
                        for (size_t j = 0; j < childNodes.size(); ++j)
                        {
                            /*
                             *   <server>
                             *     <name>new_ManagedServer_1</name>
                             *     <ssl>
                             *       <name>new_ManagedServer_1</name>
                             *       <enabled>true</enabled>
                             *       <listen-port>7513</listen-port>
                             *     </ssl>
                             *     <machine>new_UnixMachine_1</machine>
                             *     <listen-port>7013</listen-port>
                             *     <listen-address/>
                             *   </server>
                             * 
                             */
                            if (childNodes[j]->GetName() == WEBLOGIC_NAME_XML_NODE)
                            {
                                name = childNodes[j]->GetContent();
                                isAdminServer = adminServerName == name;                                       
                            } 
                            else if (childNodes[j]->GetName() == WEBLOGIC_SSL_XML_NODE)
                            {
                                ReadConfigXmlForSslInformation(
                                        childNodes[j],
                                        isSslEnabled,
                                        httpsPort);                            } 
                            else if (childNodes[j]->GetName() == WEBLOGIC_LISTEN_PORT_XML_NODE)
                            {
                                httpPort = childNodes[j]->GetContent();
                            }                            
                        }
                        /*
                         * Having found the server node, 
                         * read the children
                         */
                        wstring wideName = StrFromUTF8(name);
                        SCXFilePath pathOnDisk;
                        pathOnDisk.SetDirectory(domainDir.Get());
                        pathOnDisk.AppendDirectory(WEBLOGIC_SERVERS_DIRECTORY);
                        pathOnDisk.AppendDirectory(wideName);

                        if (DoesServerDirectoryExist(pathOnDisk))
                        {
                            SCX_LOGTRACE(m_log, 
                                    wstring(L"WebLogicFileReader::ReadConfigXml() - ").
                                    append(L"Adding instance for ID='").append(pathOnDisk.Get()).
                                    append(L"'"));
                            // when the HTTP port is not set for the AdminServer,
                            // default to the default weblogic HTTP port (i.e. 7001)
                            wstring wideHttpPort = StrFromUTF8(httpPort);
                            if(isAdminServer && L"" == wideHttpPort)
                            {
                                wideHttpPort = DEFAULT_WEBLOGIC_HTTP_PORT;
                            }

                            // when the HTTPS port is not set, default to
                            // the default HTTPS port (i.e. 7002)
                            wstring wideHttpsPort = StrFromUTF8(httpsPort);
                            if(L"" == wideHttpsPort)
                            {
                                wideHttpsPort = DEFAULT_WEBLOGIC_HTTPS_PORT;
                            }
                        
                            wstring wideVersion = StrFromUTF8(version);
                        
                            SCXHandle<AppServerInstance> instance(
                                 new WebLogicAppServerInstance (
                                        pathOnDisk.GetDirectory()));
                        
                            instance->SetHttpPort(wideHttpPort);
                            instance->SetHttpsPort(wideHttpsPort);
                            instance->SetIsDeepMonitored(false, PROTOCOL_HTTPS);
                            instance->SetIsRunning(false);
                            instance->SetVersion(wideVersion);
                        
                            instance->SetServer(
                                    isAdminServer ?
                                            WEBLOGIC_SERVER_TYPE_ADMIN :
                                            WEBLOGIC_SERVER_TYPE_MANAGED);
                           
                            instances.push_back(instance);
                        }
                        else
                        {
                            SCX_LOGTRACE(m_log, 
                                    wstring(L"WebLogicFileReader::ReadConfigXml() - ").
                                    append(L"The directory (").append(pathOnDisk.Get()).
                                    append(L") does not exist on disk, ignoring this instance"));
                        }
                    }
                }
            }
        }
        catch (SCXFilePathNotFoundException&)
        {
            SCX_LOGERROR(m_log, 
                    wstring(L"WebLogicFileReader::ReadConfigXml() - ").
                    append(m_installationPath).append(L" - Could not find file: ").
                    append(configXml.Get()));
        }
        catch (SCXUnauthorizedFileSystemAccessException&)
        {
            SCX_LOGERROR(m_log, 
                    wstring(L"WebLogicFileReader::ReadConfigXml() - ").
                    append(m_installationPath).append(L" - not authorized to open file: ").
                    append(configXml.Get()));
        }
        catch (XmlException& x)
        {
            SCX_LOGERROR(m_log, 
                    wstring(L"WebLogicFileReader::ReadConfigXml() - ").
                    append(m_installationPath).append(L" - Could not load XML from file: ").
                    append(configXml.Get()));
        }
    }