using ZA.CoreService.ESBCertificateManager.Services; namespace ZA.CoreService.ESBCertificateManager.Models; /// /// Aus dbo.SonicConnection geladene Verbindungsdaten. /// Zugangsdaten liegen ausschließlich in dbo.SonicCredential. /// public sealed class DatabaseSonicConnection { public int Id { get; init; } public required string Name { get; init; } public required string ManagementHost { get; init; } public int ManagementPort { get; init; } public string? ConnectionProtocol { get; init; } public string? DomainName { get; init; } public string? EnvironmentCode { get; init; } public bool IsActive { get; init; } public string BuildConnectionUrl() { if (string.IsNullOrWhiteSpace(ConnectionProtocol)) { throw new InvalidOperationException( $"Bei der Sonic-Verbindung '{Name}' fehlt ConnectionProtocol."); } if (string.IsNullOrWhiteSpace(ManagementHost)) { throw new InvalidOperationException( $"Bei der Sonic-Verbindung '{Name}' fehlt ManagementHost."); } if (ManagementPort is < 1 or > 65535) { throw new InvalidOperationException( $"Bei der Sonic-Verbindung '{Name}' fehlt ein gültiger ManagementPort."); } string protocol = ConnectionProtocol .Trim() .TrimEnd(':', '/'); string host = ManagementHost.Trim(); return $"{protocol}://{host}:{ManagementPort}"; } public SonicConnection ToRuntimeConnection( RuntimeSettings runtimeSettings, SonicCredentialProfile credential) { ArgumentNullException.ThrowIfNull(credential); if (string.IsNullOrWhiteSpace(DomainName)) { throw new InvalidOperationException( $"Bei der Sonic-Verbindung '{Name}' fehlt DomainName."); } if (!credential.IsComplete) { throw new InvalidOperationException( $"Bei der Sonic-Verbindung '{Name}' fehlen die Zugangsdaten. " + "Bitte die Einstellungen öffnen und ein Benutzerprofil hinterlegen."); } string? javaExecutablePath = JavaRuntimeLocator.Resolve( runtimeSettings.JavaExecutablePath); if (javaExecutablePath is null) { throw new InvalidOperationException( "Java 8 wurde nicht gefunden. " + "Bitte Runtime:JavaExecutablePath auf " + @"jre1.8*\bin\java.exe setzen."); } string sonicClientLibraryPath = PathResolver.ResolvePath( runtimeSettings.SonicClientLibraryPath); string javaBinDirectory = Path.GetDirectoryName(javaExecutablePath) ?? string.Empty; string javaHome = Directory.GetParent(javaBinDirectory)?.FullName ?? string.Empty; string sonicHome = Directory.GetParent(sonicClientLibraryPath)?.FullName ?? string.Empty; return new SonicConnection { Name = Name, DomainName = DomainName, ConnectionUrl = BuildConnectionUrl(), Username = credential.UserName, // Nur Laufzeitwert, bereits clientseitig entschlüsselt. Password = credential.Secret, JavaPath = javaExecutablePath, JavaHome = javaHome, MfClientLibPath = sonicClientLibraryPath, SonicHome = sonicHome, TimeoutSeconds = 120, PostRestartDelaySeconds = 20 }; } }