void JBossAppServerInstance::UpdateJBoss5Ports()

in source/code/providers/support/appserver/jbossappserverinstance.cpp [647:887]


    void JBossAppServerInstance::UpdateJBoss5Ports()
    {
        const string cDeploymentNodeName("deployment");
        const string cBeanNodeName("bean");
        const string cSetNodeName("set");
        const string cPropertyNodeName("property");
        const string cClassAttributeName("class");
        const string cConstructorNodeName("constructor");
        const string cServiceBindingSetName("org.jboss.services.binding.impl.ServiceBindingSet");
        const string cParameterNodeName("parameter");
        const string cNameAttributeName("name");
        const string cStandardBindingsName("StandardBindings");
        const string cServiceName("serviceName");
        const string cWebServerName("jboss.web:service=WebServer");
        const string cBindingName("bindingName");
        const string cPortName("port");
        const string cHttpConnectorName("HttpConnector");
        const string cHttpsConnectorName("HttpsConnector");
        const string cServiceBindingManagerAttributeName("ServiceBindingManager");
        const string cFactoryNodeName("factory");
        const string cBeanAttributeName("bean");
        const wstring cPortsDefault(L"ports-default");

        string xmlcontent;
        SCXFilePath filename(m_diskPath);

        filename.Append(L"/conf/bindingservice.beans/META-INF/bindings-jboss-beans.xml");

        try {
            SCXHandle<istream> mystream = m_deps->OpenXmlPortsFile(filename.Get());
            GetStringFromStream(mystream, xmlcontent);

            XElementPtr depNode;
            XElement::Load(xmlcontent, depNode);
            if (depNode->GetName() == cDeploymentNodeName)
            {
                XElementList beanNodes;
                depNode->GetChildren(beanNodes);
                bool foundset = false;
                bool foundbase = false;
                unsigned int portOffset = 0; // Use offset 0 if no offset can be found
                XElementPtr baseNode;
                string bindingManager;
                bool foundBinding = false;

                // No PortsBinding has been specified on the commandline, check for the default value set in the XML file. 
                // We start by finding the factory used by the "ServiceBindingManager"
                //
                // <bean name="ServiceBindingManager" class="org.jboss.services.binding.ServiceBindingManager">
                //    <annotation>@org.jboss.aop.microcontainer.aspects.jmx.JMX(name="jboss.system:service=ServiceBindingManager", exposedInterface=org.jboss.services.binding.ServiceBindingManagerMBean.class, registerDirectly=true)</annotation>
                //    <constructor factoryMethod="getServiceBindingManager">
                //       <factory bean="ServiceBindingManagementObject"/>
                //    </constructor>
                // </bean>

                if ( m_portsBinding.empty() )
                {
                    for (size_t idx = 0; !(foundBinding) && idx < beanNodes.size(); ++idx)
                    {
                        string nameprop;
                        string classprop;

                        // Get the "ServiceBindingManager" from the XML file
                        if (beanNodes[idx]->GetName() == cBeanNodeName && 
                            beanNodes[idx]->GetAttributeValue(cNameAttributeName, classprop) && 
                            cServiceBindingManagerAttributeName == classprop)
                        {
                            XElementPtr constrNode;
                            if (beanNodes[idx]->GetChild(cConstructorNodeName, constrNode))
                            {
                                XElementPtr factoryNode;
                                if (constrNode->GetChild(cFactoryNodeName, factoryNode))
                                {
                                    factoryNode->GetAttributeValue(cBeanAttributeName, bindingManager);
                                }        
                            }
                        }
                        
                        // Once we have the factory associated with the "ServiceBindingManager" we need to find the 
                        // parameter containing the default value "${jboss.service.binding.set:ports-01}" and get the configuration option
                        // specified e.g. "ports-01"
                        //
                        // <bean name="ServiceBindingManagementObject" class="org.jboss.services.binding.managed.ServiceBindingManagementObject">
                        //   <constructor>
                        //      <parameter>${jboss.service.binding.set:ports-01}</parameter>
                        //      <parameter>
                        //         <set>
                        //            <inject bean="PortsDefaultBindings"/>
                        //            <inject bean="Ports01Bindings"/>
                        //            <inject bean="Ports02Bindings"/>
                        //            <inject bean="Ports03Bindings"/>
                        //         </set>
                        //      </parameter>
                        //      <parameter>
                        //         <inject bean="StandardBindings"/>
                        //      </parameter>
                        //   </constructor>
                        // </bean>
                        if(bindingManager.length() > 0)
                        {
                            if (beanNodes[idx]->GetName() == cBeanNodeName && 
                                beanNodes[idx]->GetAttributeValue(cNameAttributeName, classprop) && 
                                bindingManager == classprop)
                            {
                                XElementPtr constrNode;
                                if (beanNodes[idx]->GetChild(cConstructorNodeName, constrNode))
                                {
                                    XElementList paramNodes;
                                    
                                    constrNode->GetChildren(paramNodes);
                                    foundBinding = GetPortsBinding (paramNodes, m_portsBinding);
                                }
                            }
                        }
                    }
                }

                // No PortsBinding has been specified on the commandline and we never found the
                // default setting in the ServiceBindingManager so we will atempt to use the default
                if ( m_portsBinding.empty() )
                {
                    m_portsBinding = cPortsDefault;
                }

                for (size_t idx = 0; !(foundset && foundbase) && idx < beanNodes.size(); ++idx)
                {
                    string nameprop;
                    string classprop;

                    if (beanNodes[idx]->GetName() == cBeanNodeName && 
                        beanNodes[idx]->GetAttributeValue(cClassAttributeName, classprop) && 
                        cServiceBindingSetName == classprop)
                    {
                        XElementPtr constrNode;
                        if (beanNodes[idx]->GetChild(cConstructorNodeName, constrNode))
                        {
                            XElementList paramNodes;
                            constrNode->GetChildren(paramNodes);
                            if (paramNodes.size() >= 3 &&
                                paramNodes[0]->GetName() == cParameterNodeName &&
                                paramNodes[0]->GetContent() == StrToUTF8(m_portsBinding))
                            {
                                std::wstring content;
                                paramNodes[2]->GetContent(content);

                                TryReadInteger(portOffset, foundset, content, L"Failed to parse port offset");
                            }
                        }
                    }

                    if (beanNodes[idx]->GetName() == cBeanNodeName && 
                        beanNodes[idx]->GetAttributeValue(cNameAttributeName, nameprop) && 
                        cStandardBindingsName == nameprop)
                    {
                        baseNode = beanNodes[idx];
                        foundbase = true;
                    }
                }

                // use baseNode to figure out base HTTP & HTTPS ports
                // add portOffset to that
                XElementPtr constrNode;
                XElementPtr paramNode;
                XElementPtr setNode;
                if (foundbase && 
                    baseNode->GetChild(cConstructorNodeName, constrNode) && 
                    constrNode->GetChild(cParameterNodeName, paramNode) &&
                    paramNode->GetChild(cSetNodeName, setNode))
                {
                    XElementList setBeanNodes;
                    setNode->GetChildren(setBeanNodes);
                    bool foundHttp = false;
                    bool foundHttps = false;
                    unsigned int baseHttpPort = 0;
                    unsigned int baseHttpsPort = 0;
                    for (size_t idx = 0; !(foundHttp && foundHttps) && idx < setBeanNodes.size(); ++idx)
                    {
                        XElementList propNodes;
                        string nameprop;
                        std::wstring content;

                        setBeanNodes[idx]->GetChildren(propNodes);
                        if (propNodes.size() >= 2 &&
                            propNodes[0]->GetName() == cPropertyNodeName &&
                            propNodes[0]->GetAttributeValue(cNameAttributeName, nameprop) &&
                            cServiceName == nameprop &&
                            propNodes[0]->GetContent() == cWebServerName)
                        {
                            if (propNodes.size() >= 3 &&
                                propNodes[1]->GetName() == cPropertyNodeName &&
                                propNodes[1]->GetAttributeValue(cNameAttributeName, nameprop) &&
                                cBindingName == nameprop &&
                                propNodes[2]->GetName() == cPropertyNodeName &&
                                propNodes[2]->GetAttributeValue(cNameAttributeName, nameprop) &&
                                cPortName == nameprop)
                            {
                                if (propNodes[1]->GetContent() == cHttpConnectorName)
                                {
                                    propNodes[2]->GetContent(content);
                                    TryReadInteger(baseHttpPort, foundHttp, content, L"Failed to parse HTTP port");
                                }
                                else if (propNodes[1]->GetContent() == cHttpsConnectorName)
                                {
                                    propNodes[2]->GetContent(content);
                                    TryReadInteger(baseHttpsPort, foundHttps, content, L"Failed to parse HTTPS port");
                                }
                            }
                            else if (propNodes[1]->GetName() == cPropertyNodeName &&
                                     propNodes[1]->GetAttributeValue(cNameAttributeName, nameprop) &&
                                     cPortName == nameprop)
                            {
                                propNodes[1]->GetContent(content);
                                TryReadInteger(baseHttpPort, foundHttp, content, L"Failed to parse HTTP port");
                            }
                        }
                    }
                    // calculate ports if found
                    if (foundHttp)
                    {
                        m_httpPort = StrFrom(baseHttpPort + portOffset);
                    }
                    if (foundHttps)
                    {
                        m_httpsPort = StrFrom(baseHttpsPort + portOffset);
                    }
                }
            }
        }
        catch (SCXFilePathNotFoundException&)
        {
            SCX_LOGERROR(m_log, wstring(L"JBossAppServerInstance::UpdateJBoss5Ports() - ").append(GetId()).append(L" - Could not find file: ").append(filename));
        }
        catch (SCXUnauthorizedFileSystemAccessException&)
        {
            SCX_LOGERROR(m_log, wstring(L"JBossAppServerInstance::UpdateJBoss5Ports() - ").append(GetId()).append(L" - not authorized to open file: ").append(filename));
        }
        catch (XmlException&)
        {
            SCX_LOGERROR(m_log, wstring(L"JBossAppServerInstance::UpdateJBoss5Ports() - ").append(GetId()).append(L" - Could not load XML from file: ").append(filename));
        }
    }