Detect finds the JRE provider that should be used If a JRE is explicitly configured, it uses that JRE and fails if detection errors If no JRE is explicitly configured, it uses the configured default JRE Returns the JRE, its name, and any error
()
| 120 | // If no JRE is explicitly configured, it uses the configured default JRE |
| 121 | // Returns the JRE, its name, and any error |
| 122 | func (r *Registry) Detect() (JRE, string, error) { |
| 123 | var detectionErrors []error |
| 124 | |
| 125 | // Check for deprecated JBP_CONFIG_COMPONENTS usage |
| 126 | if componentsEnv := os.Getenv("JBP_CONFIG_COMPONENTS"); componentsEnv != "" { |
| 127 | r.ctx.Log.Warning("JBP_CONFIG_COMPONENTS is deprecated for JRE selection and will be ignored") |
| 128 | r.ctx.Log.Warning("Use JRE-specific environment variables instead:") |
| 129 | r.ctx.Log.Warning(" - JBP_CONFIG_OPEN_JDK_JRE for OpenJDK") |
| 130 | r.ctx.Log.Warning(" - JBP_CONFIG_SAP_MACHINE_JRE for SapMachine") |
| 131 | r.ctx.Log.Warning(" - JBP_CONFIG_ZULU_JRE for Zulu") |
| 132 | r.ctx.Log.Warning(" - JBP_CONFIG_GRAAL_VM_JRE for GraalVM") |
| 133 | r.ctx.Log.Warning(" - JBP_CONFIG_IBM_JRE for IBM Semeru") |
| 134 | r.ctx.Log.Warning(" - JBP_CONFIG_ORACLE_JRE for Oracle") |
| 135 | r.ctx.Log.Warning(" - JBP_CONFIG_ZING_JRE for Azul Platform Prime") |
| 136 | } |
| 137 | |
| 138 | // Check if any JRE is explicitly configured |
| 139 | for _, jre := range r.providers { |
| 140 | detected, err := jre.Detect() |
| 141 | if err != nil { |
| 142 | // Collect detection errors - if a JRE is explicitly configured but fails to detect, |
| 143 | // we should fail the build rather than silently falling back to the default |
| 144 | detectionErrors = append(detectionErrors, fmt.Errorf("%s: %w", jre.Name(), err)) |
| 145 | continue |
| 146 | } |
| 147 | if detected { |
| 148 | return jre, jre.Name(), nil |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // If we had detection errors, fail the build |
| 153 | // This ensures explicit JRE configurations don't silently fall back to defaults |
| 154 | if len(detectionErrors) > 0 { |
| 155 | r.ctx.Log.Error("JRE detection errors occurred:") |
| 156 | for _, err := range detectionErrors { |
| 157 | r.ctx.Log.Error(" - %s", err.Error()) |
| 158 | } |
| 159 | return nil, "", fmt.Errorf("JRE detection failed with %d error(s)", len(detectionErrors)) |
| 160 | } |
| 161 | |
| 162 | // No explicit configuration found, use default JRE |
| 163 | if r.defaultJRE != nil { |
| 164 | r.ctx.Log.Info("No JRE explicitly configured, using default: %s", r.defaultJRE.Name()) |
| 165 | return r.defaultJRE, r.defaultJRE.Name(), nil |
| 166 | } |
| 167 | |
| 168 | // No JRE found and no default configured - this is an error condition |
| 169 | // A Java application cannot run without a JRE |
| 170 | return nil, "", fmt.Errorf("no JRE found and no default JRE configured") |
| 171 | } |
| 172 | |
| 173 | // Component represents a JRE component (memory calculator, jvmkill, etc.) |
| 174 | type Component interface { |
no test coverage detected