com.github.lontime.extjobrunr.configuration.OptionResolver Maven / Gradle / Ivy
package com.github.lontime.extjobrunr.configuration;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import com.github.lontime.base.commonj.constants.Consts;
import com.github.lontime.extconfig.ConfigHelper;
import com.github.lontime.extjobrunr.common.JobDetailsGeneratorKind;
import com.github.lontime.extjobrunr.common.SerializerKind;
import com.github.lontime.extjobrunr.common.StorageProviderKind;
import com.github.lontime.shaded.com.google.common.base.Suppliers;
import com.github.lontime.shaded.io.helidon.config.Config;
/**
* OptionResolver.
*
* @author lontime
* @since 1.0
*/
public class OptionResolver {
public static final String FILE_JOBRUNR = "jobrunr";
//------------------singleton------------------
private static Supplier supplier = Suppliers.memoize(OptionResolver::new);
public static OptionResolver getInstance() {
return supplier.get();
}
//------------------singleton------------------
private final List jobrunrOptions;
public OptionResolver() {
this.jobrunrOptions = ConfigHelper.resolve(FILE_JOBRUNR, true)
.as(new MapperList()).orElse(Collections.emptyList());
}
public Options getDefaultJobrunrOption() {
return jobrunrOptions.stream().filter(s -> Consts.DEFAULT_OBJECT_NAME.equals(s.getName()))
.findFirst().orElse(new Options());
}
public List getJobrunrOptions() {
return jobrunrOptions;
}
public boolean haveStorageUsingSql() {
return jobrunrOptions.stream().anyMatch(s -> StorageProviderKind.isSql(s.getStorageKind()));
}
public static class MapperList implements Function> {
@Override
public List apply(Config config) {
return config.asList(new Mapper()).orElse(Collections.emptyList());
}
}
public static class Mapper implements Function {
@Override
public Options apply(Config config) {
final Options options = new Options();
config.get("name").asString().ifPresent(options::setName);
config.get("serializerKind").asString().map(String::toUpperCase)
.map(SerializerKind::valueOf).ifPresent(options::setSerializerKind);
config.get("generatorKind").asString().map(String::toUpperCase)
.map(JobDetailsGeneratorKind::valueOf).ifPresent(options::setGeneratorKind);
config.get("storageKind").asString().map(String::toUpperCase)
.map(StorageProviderKind::valueOf).ifPresent(options::setStorageKind);
config.get("enableJmxExtensions").asBoolean().ifPresent(options::setEnableJmxExtensions);
config.get("enableDashboard").asBoolean().ifPresent(options::setEnableDashboard);
// config.get("scheduler").as(new JobSchedulerMapper()).ifPresent(options::setScheduler);
config.get("server").as(new BackgroundJobServerMapper()).ifPresent(options::setServer);
config.get("database").as(new DatabaseMapper()).ifPresent(options::setDatabase);
config.get("dashboard").as(new DashboardMapper()).ifPresent(options::setDashboard);
return options;
}
}
// public static class JobSchedulerMapper implements Function {
//
// @Override
// public JobSchedulerOption apply(Config config) {
// final JobSchedulerOption option = new JobSchedulerOption();
// config.get("generatorKind").asString().map(String::toUpperCase)
// .map(JobDetailsGeneratorKind::valueOf).ifPresent(option::setGeneratorKind);
// config.get("storageKind").asString().map(String::toUpperCase)
// .map(StorageProviderKind::valueOf).ifPresent(option::setStorageKind);
// return option;
// }
// }
public static class BackgroundJobServerMapper implements Function {
@Override
public BackgroundJobServerOption apply(Config config) {
final BackgroundJobServerOption option = new BackgroundJobServerOption();
config.get("workerCount").asInt().ifPresent(option::setWorkerCount);
config.get("pollIntervalInSeconds").asInt().ifPresent(option::setPollIntervalInSeconds);
config.get("deleteSucceededJobsAfter").asString().map(Duration::parse).ifPresent(option::setDeleteSucceededJobsAfter);
config.get("permanentlyDeleteDeletedJobsAfter").asString().map(Duration::parse).ifPresent(option::setPermanentlyDeleteDeletedJobsAfter);
return option;
}
}
public static class DatabaseMapper implements Function {
@Override
public DatabaseOption apply(Config config) {
final DatabaseOption option = new DatabaseOption();
config.get("skipCreate").asBoolean().ifPresent(option::setSkipCreate);
config.get("databaseName").asString().ifPresent(option::setDatabaseName);
config.get("tablePrefix").asString().ifPresent(option::setTablePrefix);
config.get("datasource").asString().ifPresent(option::setDatasource);
return option;
}
}
public static class DashboardMapper implements Function {
@Override
public DashboardOption apply(Config config) {
final DashboardOption option = new DashboardOption();
config.get("port").asInt().ifPresent(option::setPort);
config.get("username").asString().ifPresent(option::setUsername);
config.get("password").asString().ifPresent(option::setPassword);
return option;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy