##########################################################################################
# <copyright file="JEEAppServerLibrary.ps1" company="Microsoft">
# Copyright (c) Microsoft Corporation. All rights reserved.
# </copyright>
# <summary>Library functions to access a JEE application server and validate
# the ServerName, Port and protocol. </summary>
##########################################################################################
Set-PSDebug -Strict
##########################################################################################
#
# Load one assembly
# Parameter assemblyname - Name of assembly to load
# Returns - An Assembly object
#
##########################################################################################
function GetAssembly($assemblyname)
{
return [System.Reflection.Assembly]::LoadWithPartialName($assemblyname)
}
##########################################################################################
#
# Load one assembly and log if fail
# Parameter assemblyname - Name of assembly to load
# Returns - An Assembly object
#
##########################################################################################
function Load-Assembly($assemblyname,$verbose)
{
$assembly = GetAssembly $assemblyname
if (!$assembly)
{
if($verbose)
{
Write-host -foregroundcolor Red "Failed to load assembly $assemblyname"
}
}
return $assembly
}
##########################################################################################
#
# Get the JEE connector for the management group or create one if one does not exist
# Parameter managementGroup - The management group reference object
# Returns - A JEE connector
#
##########################################################################################
function GetJEEConnector($managementGroup)
{
$mcfAdmin = $managementGroup.GetConnectorFrameworkAdministration()
$JEEConnector = new-object Guid("0607F2C4-A652-4ece-87BB-795ACA31E458")
$connectorInfo.DiscoveryDataIsManaged = $true
$connectorInfo.Description = "This is a JEE Connector used for JEE App Server Discovery."
$connectorInfo.DisplayName = "JEE Connector"
$connectorInfo.Name = "JEE Connector"
##########################################################################################
#
# Release the reference to the JEE connector
# Parameter managementGroup - The management group reference object
#
##########################################################################################
function CleanupJEEConnector($managementGroup)
{
$mcfAdmin = $managementGroup.GetConnectorFrameworkAdministration()
$JEEConnector = new-object Guid("0607F2C4-A652-4ece-87BB-795ACA31E458")
try
{
$monitoringConnector = $mcfAdmin.GetMonitoringConnector($JEEConnector)
$monitoringConnector.Uninitialize()
$mcfAdmin.Cleanup($monitoringConnector)
}
catch
{
Write-Host "Unable to Un-Initialize and cleanup the MonitoringConnector"
}
}
##########################################################################################
#
# Get the monitoring class object.
# Parameter managementGroup - ManagementGroup object
# Parameter className - Name of class
# Returns - A MonitoringClass object
#
##########################################################################################
function GetMonitoringClass($managementGroup, $className)
{
$monitoringClasses = $managementGroup.GetMonitoringClasses($className)
return $monitoringClasses[0];
}
##########################################################################################
#
# Get Management Group for computer with given name (need to be FQDN)
# Parameter computername - Name of computer to use
# Returns - A new ManagementGroup object
#
##########################################################################################
function GetManagementGroup($computername)
{
return new-object Microsoft.EnterpriseManagement.ManagementGroup($Computername)
}
##########################################################################################
#
# Get computer object from WMI
# Returns - A WMI object for current computer
#
##########################################################################################
function GetWmiComputer()
{
return Get-WmiObject -Class Win32_ComputerSystem
}
##########################################################################################
#
# Get FQDN for current computer
# Returns - Name of current computer as FQDN
#
##########################################################################################
function GetComputerName()
{
$currentComputer = GetWmiComputer
return $currentcomputer.Name + "." + $currentComputer.Domain
}
##########################################################################################
#
# Parse the input URL and return the Domain , Protocol and port.
# Parameter jeeAppServer - URL for the Application server
# Returns - appServerHost, appServerPort, protocol
# or NULL if the URL is invalid
#
##########################################################################################
function ParseURL($jeeAppServer)
{
$appServer = $null
$appServerHost = $null
$appServerPort = $null
$protocol = $null
$result = $null
if ($jeeAppServer)
{
$appServer = $jeeAppServer.Trim()
$intLen = $appServer.Length
if (($intLen -gt 0) -and (!$appServer.StartsWith("'")))
{
$intProtocol = $appServer.IndexOf("://")
if ($intProtocol -gt 0)
{
$protocol = $appServer.Substring(0, $intProtocol)
#Get the HostName from the URL httpx://HostName:1234/abc
$intHost = $appServer.IndexOf(":", $intProtocol+3)
if ($intHost -gt 0)
{
# If a port has been supplied
if(($intLen-$intHost) -gt 1)
{
$appServerHost = $appServer.Substring($intProtocol+3, $intHost-$intProtocol-3)
$thePort = $appServer.Substring($intHost+1)
trap
{
continue
}
$appServerPort = [int]$thePort
}
}
else
{
#Get the HostName from the URL httpx://HostName/abc
$intHost = $appServer.IndexOf("/", $intProtocol+3)
if ($intHost -gt 0)
{
$appServerPort = 80;
$appServerHost = $appServer.Substring($intProtocol+3, $intHost-$intProtocol-3)
}
else
{
#Get the HostName from the URL httpx://HostName
$appServerPort = 80;
$appServerHost = $appServer.Substring($intProtocol + 3)
}
}
##########################################################################################
#
# Validate whether the supplied server type and version are supported.
# Parameter serverType - a string representation of the server type e.g. "Tomcat", "JBoss"
# Parameter serverVersion - the version number of the application server
# Returns - true if the application server and version are supported else false
#
##########################################################################################
function IsSupportedAppServer($serverType, $serverVersion)
{
$isSupported = $false;
if ($serverType.CompareTo("Tomcat") -eq 0)
{
if (($serverVersion -eq "5") -or
($serverVersion -eq "6") -or
($serverVersion -eq "7"))
{
$isSupported = $true
}
}
elseif ($serverType.CompareTo("JBoss") -eq 0)
{
if (($serverVersion -eq "4") -or
($serverVersion -eq "5") -or
($serverVersion -eq "6"))
{
$isSupported = $true
}
}
elseif ($serverType.CompareTo("WebSphere") -eq 0)
{
if (($serverVersion -eq "6") -or
($serverVersion -eq "7"))
{
$isSupported = $true
}
}
elseif ($serverType.CompareTo("WebLogic") -eq 0)
{
if (($serverVersion -eq "10") -or
($serverVersion -eq "11"))
{
$isSupported = $true
}
}
return $isSupported
}
##########################################################################################
#
# Perform a webclient call to retrieve the application server name and version.
# If a username is supplied to this function is imples that secure communication is requested, the function
# will request the password from the user and use these credentials to connect to the application server.
# Parameter jeeAppServerQuery - the URL to the Stats/Info BeanSpy query.
# Parameter locUserName - the user name for basic authentication.
# Returns - the xml response recieved from the call or NULL if the connection failed.
#
##########################################################################################
function DoRequest($jeeAppServerQuery, $locUserName)
{
$probe = $null
##########################################################################################
#
# Perform a webclient call to retrieve the application server name and version.
# Parameter jeeAppServer - the URL to the application server.
# Parameter locAppServerType - optional value to override the server type.
# Parameter locAppServerVersion - optional value to override the server version.
# Parameter locUserName - the user name for basic authentication.
# Returns - appServerType and appServerVersion
#
##########################################################################################
function QueryAppServer($jeeAppServer, $locAppServerType, $locAppServerVersion, $locUserName)
{
$appServerType = "unknown"
$appServerVersion = "unknown"
$prName = $null
$prVersion = $null
$requestResult = $null
$Result = $null
######################################################################################
# If the user has specified the ServerType and Version then there is no need to
# connect to the URL and atempt to retrieve them.
######################################################################################
if (($locAppServerType -ne "") -and ($locAppServerVersion -ne "") -and
(IsSupportedAppServer $locAppServerType $locAppServerVersion))
{
$appServerType = $locAppServerType
$appServerVersion = $locAppServerVersion
$Result = @{"appServerType" = $appServerType; "appServerVersion" = $appServerVersion}
}
else
{
if ($jeeAppServer.EndsWith("/"))
{
$jeeAppServer = $jeeAppServer.SubString(0, $jeeAppServer.Length -1)
}
$infoUrl = $jeeAppServer + "/BeanSpy/Stats/Info"