void JBossAppServerInstance::UpdateJBoss7Ports()

in source/code/providers/support/appserver/jbossappserverinstance.cpp [905:1240]


    void JBossAppServerInstance::UpdateJBoss7Ports()
    {
        string xmlcontent;
        SCXFilePath filename(m_basePath);

        if (m_serverName.length() == 0)
        {
            const string cServerNodeName("server");
            const string cHostNodeName("host");
            const string cServersNodeName("servers");
            const string cServerGroupName("server-group");
            const string cSocketBindingGroupNodeName("socket-binding-group");
            const string cPortOffsetAttributeName("port-offset");
            const string cSocketBindingNodeName("socket-binding");
            const string cNameAttributeName("name");
            const string cHttpName("http");
            const string cHttpsName("https");
            const string cPortAttributeName("port");
            if(m_jbossStandaloneConfigFile.find(L".xml") != std::string::npos)
            {
                filename.Set(m_jbossStandaloneConfigFile);
            }
            else
            {
                filename.Set(m_jbossStandaloneConfigFile + L"standalone.xml");
            }

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

                XElementPtr topNode;
                XElement::Load(xmlcontent, topNode);
                if (topNode->GetName() == cServerNodeName)
                {
                    XElementPtr socketBindingGroupNode;
                    if(topNode->GetChild(cSocketBindingGroupNodeName, socketBindingGroupNode))
                    {
                        bool httpPortFound = false;
                        bool httpsPortFound = false;
                        bool portsFound = false;
                        bool portOffsetFound = false;
                        unsigned int baseHttpPort = 0;
                        unsigned int baseHttpsPort = 0;
                        unsigned int portOffset = 0;
                        string HttpPort;
                        string HttpsPort;
                        string offset;
                    
                        //Regex for port-offset and http/https
                        SCXRegex re(L"[0-9]+");

                        if(m_portsBinding.size() > 0)
                        {
                            TryReadInteger(portOffset, portOffsetFound, m_portsBinding, L"Failed to Parse port offset");
                        }
                        // chek if port-offset attribute, if not then value is preset to 0
                        else if(socketBindingGroupNode->GetAttributeValue(cPortOffsetAttributeName,offset))
                        {
                            // There are two ways to set port-offset, one with port-offset="100",
                            // and the second with port-offset="${jboss.socket.binding.port-offset:100}";
                            // check if it includes jboss.socket.bind.port-offset beginning and parse accordingly
                        
                            wstring wPortOffset = SCXCoreLib::StrFromUTF8(offset);
                            std::vector<std::wstring> v_offset;
                        
                            if(re.ReturnMatch(wPortOffset,v_offset,0))
                            {
                                wPortOffset = v_offset[0];
                            }
                            TryReadInteger(portOffset, portOffsetFound, wPortOffset, L"Failed to Parse port offset");
                        }
                        
                        // XMl Document Example for Standalone.xml
                        // <socket-binding-group name="standard sockets>
                        //   <socket-binding name="http" port="8080"/>
                        //   <socket-binding name="https" port="8443"/>
                        //   <socket-binding name="....." port="...."/>
                        // </socket-binding group>
                    
                        XElementList socketBindings;
                        socketBindingGroupNode->GetChildren(socketBindings);
                    
                        // In WildFly (JBoss AS 8 the ports can be set
                        // using ${jboss.http:8080}
                        // will use Regex re from above
                        // to determine the numbers from ports
                    
                        std::vector<std::wstring> v_ports;

                        for(size_t idx = 0; !portsFound && idx <socketBindings.size();++idx)
                        {
                            string name;
                            if(socketBindings[idx]->GetName() == cSocketBindingNodeName &&
                               socketBindings[idx]->GetAttributeValue(cNameAttributeName, name))
                            {
                                if(cHttpName == name &&
                                   socketBindings[idx]->GetAttributeValue(cPortAttributeName, HttpPort))
                                {
                                    wstring wHttpPort;
                                    wHttpPort = SCXCoreLib::StrFromUTF8(HttpPort);
                                    if(re.ReturnMatch(wHttpPort, v_ports,0))
                                    {
                                        wHttpPort = v_ports[0];
                                    }
                                    TryReadInteger(baseHttpPort, httpPortFound, wHttpPort, L"Failed to parse HTTP port");
                                }
                                else if(cHttpsName == name &&
                                        socketBindings[idx]->GetAttributeValue(cPortAttributeName, HttpsPort))
                                {
                                    wstring wHttpsPort;
                                    wHttpsPort = SCXCoreLib::StrFromUTF8(HttpsPort);
                                
                                    // Clear port vector for use with https
                                    v_ports.clear();

                                    if(re.ReturnMatch(wHttpsPort, v_ports,0))
                                    {
                                        wHttpsPort = v_ports[0];
                                    }
                                    TryReadInteger(baseHttpsPort, httpsPortFound, wHttpsPort, L"Failed to parse HTTPS port");
                                }

                                if(httpPortFound && httpsPortFound) portsFound = true;
                            }
                        }
                        if(httpPortFound)
                        {
                            m_httpPort = StrFrom(baseHttpPort + portOffset);
                        }
                        if(httpsPortFound)
                        {
                            m_httpsPort = StrFrom(baseHttpsPort + portOffset);
                        }
                    }
                }
            }
            catch (SCXFilePathNotFoundException&)
            {
                SCX_LOGERROR(m_log, wstring(L"JBossAppServerInstance::UpdateJBoss7Ports() - ").append(GetId()).append(L" - Could not find file: ").append(filename));
            }
            catch (SCXUnauthorizedFileSystemAccessException&)
            {
                SCX_LOGERROR(m_log, wstring(L"JBossAppServerInstance::UpdateJBoss7Ports() - ").append(GetId()).append(L" - not authorized to open file: ").append(filename));
            }
            catch (XmlException&)
            {
                SCX_LOGERROR(m_log, wstring(L"JBossAppServerInstance::UpdateJBoss7Ports() - ").append(GetId()).append(L" - Could not load XML from file: ").append(filename));
            }
        }
        else
        {
            try
            {
				vector<wstring> a = GetJBossWildflyServerHostXmlInformation();
				

				const string cDomainNodeName = "domain";
				const string cServerGroupsNodeName = "server-groups";
				const string cServerGroupNodeName = "server-group";
				const string cNameAttributeName = "name";
				const string cSocketBindingGroupNodeName = "socket-binding-group";
				const string cRefAttributeName = "ref";

				const string cSocketBindingGroupsNodeName = "socket-binding-groups";
				const string cSocketBindingNodeName = "socket-binding";
				const string cPortAttributeName = "port";
				const string cHttpName = "http";
				const string cHttpsName = "https";

				unsigned int baseHttpPort = 0;
                unsigned int baseHttpsPort = 0;
                unsigned int portOffset = 0;
				
				bool httpPortFound = false;
                bool httpsPortFound = false;
                bool portsFound = false;
                bool portOffsetFound = false;
				bool socketReferenceFound = false;

				wstring varServerGroupName = a[0];
				wstring varPortOffset = a[1];
				wstring wSocketReference = L""; 

				// Convert retrieved port offset to integer if exists
				if(varPortOffset != L"")
				{
					TryReadInteger(portOffset, portOffsetFound, varPortOffset, L"Failed to parse HTTPS port");
				}

				filename.Append(L"domain/configuration/domain.xml");

				SCXHandle<istream> mystream = m_deps->OpenDomainXmlFile(filename.Get());
				SCXRegex re(L"[0-9]+");
				GetStringFromStream(mystream, xmlcontent);

				XElementPtr topNode;
				XElement::Load(xmlcontent, topNode);

				if(topNode->GetName() == cDomainNodeName)
				{
					XElementPtr serverGroupsNode;
					if(topNode->GetChild(cServerGroupsNodeName, serverGroupsNode))
					{
						XElementList serverGroups;
						serverGroupsNode->GetChildren(serverGroups);
						// Iterate through the server-group tags to find node 
						// associated with groupName retrieved from GetJBossWildflyServerHostXmlInformation()
						// ex: <server-group name="main-server-group" profile="full">
						for(size_t idx = 0; idx<serverGroups.size(); ++idx)
						{
							string name;
							if(serverGroups[idx]->GetName() == cServerGroupNodeName &&
							   serverGroups[idx]->GetAttributeValue(cNameAttributeName, name))
							{	
								if(StrFromUTF8(name) == varServerGroupName)
								{
									XElementList socketBindingGroups;
									serverGroups[idx]->GetChildren(socketBindingGroups);
									// Find server-group node and iterate through children
									// to find the socket-binding-group child
									// and retrieve the socket reference attribute
									// ex:
									// <server-group name="main-server-group" profile="full">
									//		<jvm name="default">
									//			<heap size="64m" max-size="512m"/>
									//		</jvm>
									//		<socket-binding-group ref="full-sockets"/>
									// </server-group>
									for(size_t jdx = 0; jdx<socketBindingGroups.size(); ++jdx)
									{
										string ref;
										if(socketBindingGroups[jdx]->GetName() == cSocketBindingGroupNodeName &&
										   socketBindingGroups[jdx]->GetAttributeValue(cRefAttributeName, ref))
										{
											wSocketReference = StrFromUTF8(ref);
											socketReferenceFound = true;
										}
									}
								}
							}
						}
						// If we found the socket reference in domain.xml then we can proceed with determining the port values
						if(socketReferenceFound)
						{
							XElementPtr socketBindingParentGroup;
							if(topNode->GetChild(cSocketBindingGroupsNodeName, socketBindingParentGroup))
							{
								XElementList socketBindingGroups;
								socketBindingParentGroup->GetChildren(socketBindingGroups);
								// Iterate through the socket-binding-group and determine if the name matches the socket reference
								// We can then retrieve ports from the child class
								// ex:
								// <socket-binding-groups name = "socket reference" >
								//		<socket-binding name = "http" port = "${jboss.http.port:8080}"/>
								// </socket-binding-groups>
								for(size_t idx = 0; idx < socketBindingGroups.size(); ++idx)
								{
									string socketRef;
									if(socketBindingGroups[idx]->GetName() == cSocketBindingGroupNodeName &&
									   socketBindingGroups[idx]->GetAttributeValue(cNameAttributeName, socketRef))
									{
										if(StrFromUTF8(socketRef) == wSocketReference)
										{
											XElementList socketBindings;
											socketBindingGroups[idx]->GetChildren(socketBindings);

											std::vector<std::wstring> v_ports;
											for(size_t jdx = 0; !portsFound && jdx < socketBindings.size(); ++jdx)
											{
												string name;
												if(socketBindings[jdx]->GetName() == cSocketBindingNodeName &&
												   socketBindings[jdx]->GetAttributeValue(cNameAttributeName, name))
												{
													string HttpPort, HttpsPort;
													if(cHttpName == name &&
													   socketBindings[jdx]->GetAttributeValue(cPortAttributeName, HttpPort))
													{
														wstring wHttpPort;
														wHttpPort = SCXCoreLib::StrFromUTF8(HttpPort);
														if(re.ReturnMatch(wHttpPort, v_ports,0))
														{
															wHttpPort = v_ports[0];
														}
														TryReadInteger(baseHttpPort, httpPortFound, wHttpPort, L"Failed to parse HTTP port");
													}
													else if(cHttpsName == name &&
															socketBindings[jdx]->GetAttributeValue(cPortAttributeName, HttpsPort))
													{
														wstring wHttpsPort;
														wHttpsPort = SCXCoreLib::StrFromUTF8(HttpsPort);
                                
														// Clear port vector for use with https
														v_ports.clear();

														if(re.ReturnMatch(wHttpsPort, v_ports,0))
														{
															wHttpsPort = v_ports[0];
														}
														TryReadInteger(baseHttpsPort, httpsPortFound, wHttpsPort, L"Failed to parse HTTPS port");
													}

													if(httpPortFound && httpsPortFound) portsFound = true;
												}
											}
										}
									}
								}
							}
							if(httpPortFound)
							{
								m_httpPort = StrFrom(baseHttpPort + portOffset);
							}
							if(httpsPortFound)
							{
								m_httpsPort = StrFrom(baseHttpsPort + portOffset);
							}
						}
					}
				}
			}
            catch (SCXFilePathNotFoundException&)
            {
                SCX_LOGERROR(m_log, wstring(L"JBossAppServerInstance::UpdateJBoss7Ports() - ").append(GetId()).append(L" - Could not find file: ").append(filename));
            }
            catch (SCXUnauthorizedFileSystemAccessException&)
            {
                SCX_LOGERROR(m_log, wstring(L"JBossAppServerInstance::UpdateJBoss7Ports() - ").append(GetId()).append(L" - not authorized to open file: ").append(filename));
            }
            catch (XmlException&)
            {
                SCX_LOGERROR(m_log, wstring(L"JBossAppServerInstance::UpdateJBoss7Ports() - ").append(GetId()).append(L" - Could not load XML from file: ").append(filename));
            }
        }
    }