| 
 | 1 | +package com.walmartlabs.concord.agent.cfg;  | 
 | 2 | + | 
 | 3 | +/*-  | 
 | 4 | + * *****  | 
 | 5 | + * Concord  | 
 | 6 | + * -----  | 
 | 7 | + * Copyright (C) 2017 - 2025 Walmart Inc.  | 
 | 8 | + * -----  | 
 | 9 | + * Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 10 | + * you may not use this file except in compliance with the License.  | 
 | 11 | + * You may obtain a copy of the License at  | 
 | 12 | + *   | 
 | 13 | + *      http://www.apache.org/licenses/LICENSE-2.0  | 
 | 14 | + *   | 
 | 15 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 16 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 18 | + * See the License for the specific language governing permissions and  | 
 | 19 | + * limitations under the License.  | 
 | 20 | + * =====  | 
 | 21 | + */  | 
 | 22 | + | 
 | 23 | +import com.typesafe.config.Config;  | 
 | 24 | +import org.slf4j.Logger;  | 
 | 25 | +import org.slf4j.LoggerFactory;  | 
 | 26 | + | 
 | 27 | +import javax.inject.Inject;  | 
 | 28 | +import java.io.IOException;  | 
 | 29 | +import java.nio.file.Path;  | 
 | 30 | +import java.nio.file.Paths;  | 
 | 31 | +import java.util.*;  | 
 | 32 | + | 
 | 33 | +import static com.walmartlabs.concord.agent.cfg.Utils.*;  | 
 | 34 | +import static java.util.stream.Collectors.joining;  | 
 | 35 | + | 
 | 36 | +public class RuntimeConfiguration {  | 
 | 37 | + | 
 | 38 | +    private static final Logger log = LoggerFactory.getLogger(RuntimeConfiguration.class);  | 
 | 39 | + | 
 | 40 | +    private final Map<String, Entry> configs;  | 
 | 41 | + | 
 | 42 | +    @Inject  | 
 | 43 | +    public RuntimeConfiguration(Config config) {  | 
 | 44 | +        var runtimes = config.getObject("runtimes");  | 
 | 45 | +        var configs = new HashMap<String, Entry>();  | 
 | 46 | +        for (var runtime : runtimes.keySet()) {  | 
 | 47 | +            var cfg = Entry.parse(config.getConfig("runtimes." + runtime));  | 
 | 48 | +            configs.put(runtime, cfg);  | 
 | 49 | +        }  | 
 | 50 | +        log.info("Available runtimes: {}", configs.keySet().stream().sorted().collect(joining(", ")));  | 
 | 51 | +        this.configs = Collections.unmodifiableMap(configs);  | 
 | 52 | +    }  | 
 | 53 | + | 
 | 54 | +    public Optional<Entry> getForRuntime(String runtime) {  | 
 | 55 | +        var cfg = configs.get(runtime);  | 
 | 56 | +        return Optional.ofNullable(cfg);  | 
 | 57 | +    }  | 
 | 58 | + | 
 | 59 | +    public record Entry(Path path,  | 
 | 60 | +                        Path cfgDir,  | 
 | 61 | +                        String javaCmd,  | 
 | 62 | +                        List<String> jvmParams,  | 
 | 63 | +                        String mainClass,  | 
 | 64 | +                        Path persistentWorkDir,  | 
 | 65 | +                        boolean cleanRunnerDescendants,  | 
 | 66 | +                        boolean segmentedLogs) {  | 
 | 67 | + | 
 | 68 | +        public static Entry parse(Config cfg) {  | 
 | 69 | +            var pathString = getStringOrDefault(cfg, "path", () -> {  | 
 | 70 | +                // support local development, use .properties files to get JAR paths  | 
 | 71 | +                // the .properties files are populated during build  | 
 | 72 | + | 
 | 73 | +                if (!cfg.hasPath("propertiesFile")) {  | 
 | 74 | +                    throw new IllegalStateException(".path or .propertiesFile are required");  | 
 | 75 | +                }  | 
 | 76 | +                var fallback = cfg.getString("propertiesFile");  | 
 | 77 | + | 
 | 78 | +                var props = new Properties();  | 
 | 79 | +                try (var in = Entry.class.getResourceAsStream(fallback)) {  | 
 | 80 | +                    if (in == null) {  | 
 | 81 | +                        throw new IllegalStateException("Resource not found: " + fallback);  | 
 | 82 | +                    }  | 
 | 83 | +                    props.load(in);  | 
 | 84 | +                } catch (IOException e) {  | 
 | 85 | +                    throw new RuntimeException(e);  | 
 | 86 | +                }  | 
 | 87 | + | 
 | 88 | +                return props.getProperty("path");  | 
 | 89 | +            });  | 
 | 90 | +            var path = Paths.get(pathString);  | 
 | 91 | +            var cfgDir = getOrCreatePath(cfg, "cfgDir");  | 
 | 92 | +            var javaCmd = getJavaCmd(cfg);  | 
 | 93 | +            var jvmParams = cfg.getStringList("jvmParams");  | 
 | 94 | +            var mainClass = cfg.getString("mainClass");  | 
 | 95 | +            var persistentWorkDir = getOptionalAbsolutePath(cfg, "persistentWorkDir");  | 
 | 96 | +            var cleanRunnerDescendants = cfg.getBoolean("cleanRunnerDescendants");  | 
 | 97 | +            var segmentedLogs = cfg.getBoolean("segmentedLogs");  | 
 | 98 | +            return new Entry(path, cfgDir, javaCmd, jvmParams, mainClass, persistentWorkDir, cleanRunnerDescendants, segmentedLogs);  | 
 | 99 | +        }  | 
 | 100 | + | 
 | 101 | +        private static String getJavaCmd(Config cfg) {  | 
 | 102 | +            var path = "javaCmd";  | 
 | 103 | + | 
 | 104 | +            if (cfg.hasPath(path)) {  | 
 | 105 | +                var s = cfg.getString(path);  | 
 | 106 | +                if (s != null) {  | 
 | 107 | +                    return s;  | 
 | 108 | +                }  | 
 | 109 | +            }  | 
 | 110 | + | 
 | 111 | +            var javaHome = System.getProperty("java.home");  | 
 | 112 | +            if (javaHome != null) {  | 
 | 113 | +                return javaHome + "/bin/java";  | 
 | 114 | +            }  | 
 | 115 | + | 
 | 116 | +            return "java";  | 
 | 117 | +        }  | 
 | 118 | +    }  | 
 | 119 | +}  | 
0 commit comments