Skip to content

Commit

Permalink
Add support of the default AdoptOpenJDK registry keys to capabilities…
Browse files Browse the repository at this point in the history
… scanner (#3533)

* Add support for default AdoptOpenJDK regestry keys

* Add support for AdoptOpenJDK with jvmOpenj9 jvm

* Better naming

* Better naming

* Check default AdoptOpenJDK paths only if not found any JDK/JRE in JavaSoft

Co-authored-by: Anatoly Bolshakov <[email protected]>
  • Loading branch information
EzzhevNikita and Anatoly Bolshakov authored Sep 21, 2021
1 parent 0eff66a commit 0299fb6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/Misc/layoutbin/powershell/Add-JavaCapabilities.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ $jre9AndGreaterName = "Software\JavaSoft\Java Runtime Environment"
$jdk9AndGreaterName = "Software\JavaSoft\Java Development Kit"
$minimumMajorVersion9 = 9

# JRE/JDK keys for AdoptOpenJDK
$jdk9AndGreaterNameAdoptOpenJDK = "Software\AdoptOpenJDK\JDK"
$jdk9AndGreaterNameAdoptOpenJRE = "Software\AdoptOpenJDK\JRE"

# These keys required for latest versions of AdoptOpenJDK since they started to publish under Eclipse Foundation name from 24th July 2021 https://blog.adoptopenjdk.net/2021/03/transition-to-eclipse-an-update/
$jdk9AndGreaterNameAdoptOpenJDKEclipse = "Software\Eclipse Foundation\JDK"
$jdk9AndGreaterNameAdoptOpenJREEclipse = "Software\Eclipse Foundation\JRE"

# JRE/JDK keys for AdoptOpenJDK with openj9 runtime
$jdk9AndGreaterNameAdoptOpenJDKSemeru = "Software\Semeru\JDK"
$jdk9AndGreaterNameAdoptOpenJRESemeru = "Software\Semeru\JRE"

# JVM subdirectories for AdoptOpenJDK, since it could be ditributed with two different JVM versions, which are located in different subdirectories inside version directory
$jvmHotSpot = "hotspot\MSI"
$jvmOpenj9 = "openj9\MSI"

# Check for JRE.
$latestJre = $null
$null = Add-CapabilityFromRegistry -Name 'java_6' -Hive 'LocalMachine' -View 'Registry32' -KeyName $jre6KeyName -ValueName 'JavaHome' -Value ([ref]$latestJre)
Expand All @@ -37,6 +53,15 @@ if (-not (Test-Path env:DISABLE_JAVA_CAPABILITY_HIGHER_THAN_9)) {
}
}

# Check default reg keys for AdoptOpenJDK in case we didn't find JRE in JavaSoft
if (-not $latestJre) {
# AdoptOpenJDK section
$null = Add-CapabilityFromRegistryWithLastVersionAvailable -PrefixName 'java_' -PostfixName '_x64' -Hive 'LocalMachine' -View 'Registry64' -KeyName $jdk9AndGreaterNameAdoptOpenJRE -ValueName 'Path' -Value ([ref]$latestJre) -VersionSubdirectory $jvmHotSpot -MinimumMajorVersion $minimumMajorVersion9
$null = Add-CapabilityFromRegistryWithLastVersionAvailable -PrefixName 'java_' -PostfixName '_x64' -Hive 'LocalMachine' -View 'Registry64' -KeyName $jdk9AndGreaterNameAdoptOpenJRE -ValueName 'Path' -Value ([ref]$latestJre) -VersionSubdirectory $jvmOpenj9 -MinimumMajorVersion $minimumMajorVersion9
$null = Add-CapabilityFromRegistryWithLastVersionAvailable -PrefixName 'java_' -PostfixName '_x64' -Hive 'LocalMachine' -View 'Registry64' -KeyName $jdk9AndGreaterNameAdoptOpenJREEclipse -ValueName 'Path' -Value ([ref]$latestJre) -VersionSubdirectory $jvmHotSpot -MinimumMajorVersion $minimumMajorVersion9
$null = Add-CapabilityFromRegistryWithLastVersionAvailable -PrefixName 'java_' -PostfixName '_x64' -Hive 'LocalMachine' -View 'Registry64' -KeyName $jdk9AndGreaterNameAdoptOpenJRESemeru -ValueName 'Path' -Value ([ref]$latestJre) -VersionSubdirectory $jvmOpenj9 -MinimumMajorVersion $minimumMajorVersion9
}

if ($latestJre) {
# Favor x64.
Write-Capability -Name 'java' -Value $latestJre
Expand All @@ -63,6 +88,15 @@ if (-not (Test-Path env:DISABLE_JAVA_CAPABILITY_HIGHER_THAN_9)) {
}
}

# Check default reg keys for AdoptOpenJDK in case we didn't find JDK in JavaSoft
if (-not $latestJdk) {
# AdoptOpenJDK section
$null = Add-CapabilityFromRegistryWithLastVersionAvailable -PrefixName 'jdk_' -PostfixName '_x64' -Hive 'LocalMachine' -View 'Registry64' -KeyName $jdk9AndGreaterNameAdoptOpenJDK -ValueName 'Path' -Value ([ref]$latestJdk) -VersionSubdirectory $jvmHotSpot -MinimumMajorVersion $minimumMajorVersion9
$null = Add-CapabilityFromRegistryWithLastVersionAvailable -PrefixName 'jdk_' -PostfixName '_x64' -Hive 'LocalMachine' -View 'Registry64' -KeyName $jdk9AndGreaterNameAdoptOpenJDK -ValueName 'Path' -Value ([ref]$latestJdk) -VersionSubdirectory $jvmOpenj9 -MinimumMajorVersion $minimumMajorVersion9
$null = Add-CapabilityFromRegistryWithLastVersionAvailable -PrefixName 'jdk_' -PostfixName '_x64' -Hive 'LocalMachine' -View 'Registry64' -KeyName $jdk9AndGreaterNameAdoptOpenJDKEclipse -ValueName 'Path' -Value ([ref]$latestJdk) -VersionSubdirectory $jvmHotSpot -MinimumMajorVersion $minimumMajorVersion9
$null = Add-CapabilityFromRegistryWithLastVersionAvailable -PrefixName 'jdk_' -PostfixName '_x64' -Hive 'LocalMachine' -View 'Registry64' -KeyName $jdk9AndGreaterNameAdoptOpenJDKSemeru -ValueName 'Path' -Value ([ref]$latestJdk) -VersionSubdirectory $jvmOpenj9 -MinimumMajorVersion $minimumMajorVersion9
}

if ($latestJdk) {
# Favor x64.
Write-Capability -Name 'jdk' -Value $latestJdk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,19 @@ function Add-CapabilityFromRegistryWithLastVersionAvailableForSubkey {

[Parameter(Mandatory = $true)]
[string]$ValueName,

# Registry subkey
[Parameter(Mandatory = $true)]
[string]$Subkey,

# Regkey subdirectory inside particular version
[Parameter(Mandatory = $false)]
[string]$VersionSubdirectory,

# Major version of tool to be added as capability
[Parameter(Mandatory = $true)]
[int]$MajorVersion,

# Minimum major version of tool to be added as capability. All versions detected less than this version - will be ignored.
# This is helpful for backward compatibility with already existing logic for previous versions
[Parameter(Mandatory = $false)]
Expand All @@ -129,8 +136,14 @@ function Add-CapabilityFromRegistryWithLastVersionAvailableForSubkey {
return $false
}

$wholeKey = Join-Path -Path $KeyName -ChildPath $Subkey

$wholeKey = ""
if ( -not [string]::IsNullOrEmpty($VersionSubdirectory)) {
$versionDir = Join-Path -Path $KeyName -ChildPath $Subkey
$wholeKey = Join-Path -Path $versionDir -ChildPath $VersionSubdirectory
} else {
$wholeKey = Join-Path -Path $KeyName -ChildPath $Subkey
}

$capabilityValue = Get-RegistryValue -Hive $Hive -View $View -KeyName $wholeKey -ValueName $ValueName

if ([string]::IsNullOrEmpty($capabilityValue)) {
Expand Down Expand Up @@ -179,6 +192,10 @@ function Add-CapabilityFromRegistryWithLastVersionAvailable {
[Parameter(Mandatory = $true)]
[string]$KeyName,

# Regkey subdirectory inside particular version
[Parameter(Mandatory = $false)]
[string]$VersionSubdirectory,

[Parameter(Mandatory = $true)]
[string]$ValueName,
# Minimum major version of tool to be added as capability. All versions detected less than this version - will be ignored.
Expand All @@ -195,7 +212,7 @@ function Add-CapabilityFromRegistryWithLastVersionAvailable {

$sortedVersionSubkeys = $versionSubkeys | Sort-Object -Property @{Expression = {$_.Item1}; Descending = $False}
Write-Host $sortedVersionSubkeys[-1].Item1.Major
$res = Add-CapabilityFromRegistryWithLastVersionAvailableForSubkey -PrefixName $PrefixName -PostfixName $PostfixName -Hive $Hive -View $View -KeyName $KeyName -ValueName $ValueName -Subkey $sortedVersionSubkeys[-1].Item2 -MajorVersion $sortedVersionSubkeys[-1].Item1.Major -Value $Value -MinimumMajorVersion $MinimumMajorVersion
$res = Add-CapabilityFromRegistryWithLastVersionAvailableForSubkey -PrefixName $PrefixName -PostfixName $PostfixName -Hive $Hive -View $View -KeyName $KeyName -ValueName $ValueName -Subkey $sortedVersionSubkeys[-1].Item2 -VersionSubdirectory $VersionSubdirectory -MajorVersion $sortedVersionSubkeys[-1].Item1.Major -Value $Value -MinimumMajorVersion $MinimumMajorVersion

if (!$res) {
Write-Host "An error occured while trying to get last available version for capability: " $PrefixName + "<version>" + $PostfixName
Expand Down

0 comments on commit 0299fb6

Please sign in to comment.