|
| 1 | +/* |
| 2 | + * Copyright (c) 2023. Smart Operating Block |
| 3 | + * |
| 4 | + * Use of this source code is governed by an MIT-style |
| 5 | + * license that can be found in the LICENSE file or at |
| 6 | + * https://opensource.org/licenses/MIT. |
| 7 | + */ |
| 8 | + |
| 9 | +package config; |
| 10 | + |
| 11 | +import cartago.Artifact; |
| 12 | +import cartago.OPERATION; |
| 13 | +import infrastructure.configuration.model.Configuration; |
| 14 | +import org.yaml.snakeyaml.Yaml; |
| 15 | +import org.yaml.snakeyaml.constructor.Constructor; |
| 16 | +import org.yaml.snakeyaml.representer.Representer; |
| 17 | + |
| 18 | +import java.io.BufferedInputStream; |
| 19 | +import java.io.FileInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.Objects; |
| 23 | + |
| 24 | +/** |
| 25 | + * CArtAgO artifact to load the user-provided configuration of the automation management. |
| 26 | + */ |
| 27 | +public class ConfigurationArtifact extends Artifact { |
| 28 | + private static final String CONFIGURATION_PATH_VARIABLE = "CONFIG_PATH"; |
| 29 | + /** |
| 30 | + * Initialize the configuration artifact. |
| 31 | + */ |
| 32 | + void init() { |
| 33 | + Objects.requireNonNull(System.getenv(CONFIGURATION_PATH_VARIABLE), "Missing configuration path"); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Operation that loads the configuration provided by the user. |
| 38 | + */ |
| 39 | + @OPERATION |
| 40 | + void load() { |
| 41 | + final Representer represent = new Representer(); |
| 42 | + represent.getPropertyUtils().setSkipMissingProperties(true); |
| 43 | + final Yaml yaml = new Yaml(new Constructor(Configuration.class), represent); |
| 44 | + try ( |
| 45 | + BufferedInputStream configInputStream = new BufferedInputStream( |
| 46 | + new FileInputStream(System.getenv(CONFIGURATION_PATH_VARIABLE)) |
| 47 | + ) |
| 48 | + ) { |
| 49 | + final String fileContent = new String(configInputStream.readAllBytes(), StandardCharsets.UTF_8); |
| 50 | + final Configuration config = yaml.load(fileContent); |
| 51 | + signal("config", config.getOperatingRoomStandbyMode().isEnabled().toString()); // tobe deleted |
| 52 | + } catch (IOException e) { |
| 53 | + throw new IllegalStateException("Impossible to read configuration file", e); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments