com.powsybl.computation.local.LocalComputationConfig Maven / Gradle / Ivy
/**
* Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.computation.local;
import com.powsybl.commons.config.ConfigurationException;
import com.powsybl.commons.config.ModuleConfig;
import com.powsybl.commons.config.PlatformConfig;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* @author Geoffroy Jamgotchian {@literal }
*/
public class LocalComputationConfig {
private static final String CONFIG_MODULE_NAME = "computation-local";
static final String DEFAULT_LOCAL_DIR = System.getProperty("java.io.tmpdir");
private static final int DEFAULT_AVAILABLE_CORE = 1;
private final Path localDir;
private final int availableCore;
public static LocalComputationConfig load() {
return load(PlatformConfig.defaultConfig());
}
public static LocalComputationConfig load(PlatformConfig platformConfig) {
return load(platformConfig, FileSystems.getDefault());
}
private static Path getDefaultLocalDir(FileSystem fileSystem) {
return fileSystem.getPath(DEFAULT_LOCAL_DIR);
}
private static Optional getTmpDir(ModuleConfig config, String name) {
return config.getOptionalPathListProperty(name)
.map(paths -> {
if (paths.isEmpty()) {
throw new ConfigurationException("Empty tmp dir list");
}
List checkedPaths = paths.stream().filter(Files::exists).toList();
if (checkedPaths.isEmpty()) {
throw new ConfigurationException("None of the tmp dir path of the list exist");
}
return checkedPaths.get(0);
});
}
public static LocalComputationConfig load(PlatformConfig platformConfig, FileSystem fileSystem) {
Objects.requireNonNull(platformConfig);
Optional config = platformConfig.getOptionalModuleConfig(CONFIG_MODULE_NAME);
Path localDir = config.flatMap(c -> getTmpDir(c, "tmp-dir").or(() -> getTmpDir(c, "tmpDir")))
.orElse(getDefaultLocalDir(fileSystem));
int availableCore = config.map(c -> c.getOptionalIntProperty("available-core")
.orElse(c.getOptionalIntProperty("availableCore").orElse(DEFAULT_AVAILABLE_CORE)))
.orElse(DEFAULT_AVAILABLE_CORE);
if (availableCore <= 0) {
availableCore = Runtime.getRuntime().availableProcessors();
}
return new LocalComputationConfig(localDir, availableCore);
}
public LocalComputationConfig(Path localDir) {
this(localDir, DEFAULT_AVAILABLE_CORE);
}
public LocalComputationConfig(Path localDir, int availableCore) {
this.localDir = localDir;
this.availableCore = availableCore;
}
public Path getLocalDir() {
return localDir;
}
public int getAvailableCore() {
return availableCore;
}
@Override
public String toString() {
return getClass().getSimpleName() + " [localDir=" + localDir +
", availableCore=" + availableCore +
"]";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy