172 lines
4.8 KiB
C#
172 lines
4.8 KiB
C#
using System.Diagnostics;
|
|
using ZA.CoreService.ESBCertificateManager.Models;
|
|
|
|
namespace ZA.CoreService.ESBCertificateManager.Services;
|
|
|
|
public sealed class RuntimeEnvironmentValidator
|
|
{
|
|
public async Task<RuntimeValidationResult> ValidateAsync(
|
|
RuntimeSettings settings,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
List<string> issues = [];
|
|
|
|
string javaExecutablePath =
|
|
PathResolver.ResolvePath(
|
|
settings.JavaExecutablePath);
|
|
|
|
string sonicClientLibraryPath =
|
|
PathResolver.ResolvePath(
|
|
settings.SonicClientLibraryPath);
|
|
|
|
string javaVersion = string.Empty;
|
|
|
|
if (!File.Exists(javaExecutablePath))
|
|
{
|
|
issues.Add(
|
|
$"Java wurde nicht gefunden: {javaExecutablePath}");
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
javaVersion = await ReadJavaVersionAsync(
|
|
javaExecutablePath,
|
|
cancellationToken);
|
|
|
|
if (!ContainsJava8Version(javaVersion))
|
|
{
|
|
issues.Add(
|
|
"Die gefundene Java-Version ist nicht Java 8. " +
|
|
$"Ausgabe: {javaVersion}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
issues.Add(
|
|
$"Java konnte nicht ausgeführt werden: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
if (!Directory.Exists(sonicClientLibraryPath))
|
|
{
|
|
issues.Add(
|
|
"Der Sonic-Client-Bibliotheksordner wurde " +
|
|
$"nicht gefunden: {sonicClientLibraryPath}");
|
|
}
|
|
else
|
|
{
|
|
string[] requiredJarNames =
|
|
[
|
|
"mgmt_client.jar",
|
|
"mfcontext.jar"
|
|
];
|
|
|
|
foreach (string requiredJarName in requiredJarNames)
|
|
{
|
|
string requiredJarPath = Path.Combine(
|
|
sonicClientLibraryPath,
|
|
requiredJarName);
|
|
|
|
if (!File.Exists(requiredJarPath))
|
|
{
|
|
issues.Add(
|
|
$"Erforderliche Sonic-Bibliothek fehlt: " +
|
|
$"{requiredJarPath}");
|
|
}
|
|
}
|
|
}
|
|
|
|
return new RuntimeValidationResult
|
|
{
|
|
IsValid = issues.Count == 0,
|
|
JavaExecutablePath = javaExecutablePath,
|
|
SonicClientLibraryPath = sonicClientLibraryPath,
|
|
JavaVersion = javaVersion,
|
|
Issues = issues
|
|
};
|
|
}
|
|
|
|
private static async Task<string> ReadJavaVersionAsync(
|
|
string javaExecutablePath,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
ProcessStartInfo startInfo = new()
|
|
{
|
|
FileName = javaExecutablePath,
|
|
UseShellExecute = false,
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
CreateNoWindow = true
|
|
};
|
|
|
|
startInfo.ArgumentList.Add("-version");
|
|
|
|
using Process process = new()
|
|
{
|
|
StartInfo = startInfo
|
|
};
|
|
|
|
if (!process.Start())
|
|
{
|
|
throw new InvalidOperationException(
|
|
"Der Java-Prozess konnte nicht gestartet werden.");
|
|
}
|
|
|
|
Task<string> outputTask =
|
|
process.StandardOutput.ReadToEndAsync(
|
|
cancellationToken);
|
|
|
|
Task<string> errorTask =
|
|
process.StandardError.ReadToEndAsync(
|
|
cancellationToken);
|
|
|
|
await process.WaitForExitAsync(
|
|
cancellationToken);
|
|
|
|
string output = await outputTask;
|
|
string error = await errorTask;
|
|
|
|
string combined = string.Join(
|
|
Environment.NewLine,
|
|
new[] { output, error }
|
|
.Where(value =>
|
|
!string.IsNullOrWhiteSpace(value)));
|
|
|
|
if (process.ExitCode != 0)
|
|
{
|
|
throw new InvalidOperationException(
|
|
$"java -version endete mit Code {process.ExitCode}. " +
|
|
combined);
|
|
}
|
|
|
|
return combined.Trim();
|
|
}
|
|
|
|
private static bool ContainsJava8Version(
|
|
string javaVersionOutput)
|
|
{
|
|
return javaVersionOutput.Contains(
|
|
"\"1.8.",
|
|
StringComparison.OrdinalIgnoreCase)
|
|
|| javaVersionOutput.Contains(
|
|
"version 8",
|
|
StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
}
|
|
|
|
public sealed class RuntimeValidationResult
|
|
{
|
|
public bool IsValid { get; init; }
|
|
|
|
public string JavaExecutablePath { get; init; } =
|
|
string.Empty;
|
|
|
|
public string SonicClientLibraryPath { get; init; } =
|
|
string.Empty;
|
|
|
|
public string JavaVersion { get; init; } =
|
|
string.Empty;
|
|
|
|
public IReadOnlyList<string> Issues { get; init; } = [];
|
|
} |