mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-06-25 16:04:02 +08:00
- 补齐 DataGrid、DataViewer、DefinitionViewer、JVM 等模块多语言文案与回归测试 - 收口 JVM 前后端展示、诊断、监控和资源呈现相关多语言路径 - 更新六语言共享词典并保留 raw 边界
107 lines
2.5 KiB
Go
107 lines
2.5 KiB
Go
package jvm
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"GoNavi-Wails/internal/connection"
|
|
)
|
|
|
|
const defaultJMXPort = 9010
|
|
const disallowedModeKey = "jvm.backend.error.disallowed_mode"
|
|
|
|
func NormalizeConnectionConfig(raw connection.ConnectionConfig) (connection.ConnectionConfig, error) {
|
|
cfg := raw
|
|
if strings.ToLower(strings.TrimSpace(cfg.Type)) != "jvm" {
|
|
return connection.ConnectionConfig{}, fmt.Errorf("unexpected connection type: %s", cfg.Type)
|
|
}
|
|
|
|
cfg.Type = "jvm"
|
|
cfg.JVM.Environment = strings.ToLower(strings.TrimSpace(cfg.JVM.Environment))
|
|
if cfg.JVM.ReadOnly == nil {
|
|
cfg.JVM.ReadOnly = boolPtr(true)
|
|
}
|
|
if cfg.JVM.JMX.Port <= 0 {
|
|
if cfg.Port > 0 {
|
|
cfg.JVM.JMX.Port = cfg.Port
|
|
} else {
|
|
cfg.JVM.JMX.Port = defaultJMXPort
|
|
}
|
|
}
|
|
|
|
cfg.JVM.AllowedModes = normalizeModes(cfg.JVM.AllowedModes)
|
|
|
|
preferredMode := strings.ToLower(strings.TrimSpace(cfg.JVM.PreferredMode))
|
|
if preferredMode == "" || !containsMode(cfg.JVM.AllowedModes, preferredMode) {
|
|
cfg.JVM.PreferredMode = cfg.JVM.AllowedModes[0]
|
|
} else {
|
|
cfg.JVM.PreferredMode = preferredMode
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func ResolveProviderMode(raw connection.ConnectionConfig, requestedMode string) (connection.ConnectionConfig, string, error) {
|
|
cfg, err := NormalizeConnectionConfig(raw)
|
|
if err != nil {
|
|
return connection.ConnectionConfig{}, "", err
|
|
}
|
|
|
|
selectedMode := strings.ToLower(strings.TrimSpace(requestedMode))
|
|
if selectedMode == "" {
|
|
selectedMode = cfg.JVM.PreferredMode
|
|
}
|
|
if !containsMode(cfg.JVM.AllowedModes, selectedMode) {
|
|
return connection.ConnectionConfig{}, "", &LocalizedError{
|
|
Key: disallowedModeKey,
|
|
Params: map[string]any{
|
|
"mode": selectedMode,
|
|
},
|
|
}
|
|
}
|
|
|
|
cfg.JVM.PreferredMode = selectedMode
|
|
return cfg, selectedMode, nil
|
|
}
|
|
|
|
func normalizeModes(input []string) []string {
|
|
if len(input) == 0 {
|
|
return []string{ModeJMX}
|
|
}
|
|
|
|
result := make([]string, 0, len(input))
|
|
seen := make(map[string]struct{}, len(input))
|
|
for _, item := range input {
|
|
mode := strings.ToLower(strings.TrimSpace(item))
|
|
switch mode {
|
|
case ModeJMX, ModeEndpoint, ModeAgent:
|
|
default:
|
|
continue
|
|
}
|
|
if _, exists := seen[mode]; exists {
|
|
continue
|
|
}
|
|
seen[mode] = struct{}{}
|
|
result = append(result, mode)
|
|
}
|
|
|
|
if len(result) == 0 {
|
|
return []string{ModeJMX}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func containsMode(items []string, target string) bool {
|
|
normalizedTarget := strings.ToLower(strings.TrimSpace(target))
|
|
for _, item := range items {
|
|
if strings.ToLower(strings.TrimSpace(item)) == normalizedTarget {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func boolPtr(value bool) *bool {
|
|
return &value
|
|
}
|