All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.lontime.extcopper.configuration.OptionResolver Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.github.lontime.extcopper.configuration;

import java.util.function.Function;
import java.util.function.Supplier;

import com.github.lontime.extconfig.ConfigHelper;
import com.github.lontime.extcopper.common.IdFactoryKind;
import com.github.lontime.shaded.com.google.common.base.Suppliers;
import com.github.lontime.shaded.io.helidon.config.Config;
import org.copperengine.core.audit.AbstractAuditTrail;

/**
 * OptionResolver.
 * @author lontime
 * @since 1.0
 */
public class OptionResolver {
    public static final String NAME = "copper";

    private static Supplier supplier = Suppliers.memoize(OptionResolver::new);

    public static OptionResolver getInstance() {
        return supplier.get();
    }


    private final Options options;

    public OptionResolver() {
        this.options = ConfigHelper.resolve(NAME).as(new Mapper()).orElse(new Options());
    }

    public TransienceOption getTransienceOption() {
        return options.getTransience();
    }

    public PersitenceOption getPersitenceOption() {
        return options.getPersistence();
    }

    static class Mapper implements Function {

        @Override
        public Options apply(Config config) {
            final Options opts = new Options();
            config.get("transience").as(new TransienceMapper()).ifPresent(opts::setTransience);
            config.get("persistence").as(new PersitenceMapper()).ifPresent(opts::setPersistence);
            return opts;
        }
    }

    static class TransienceMapper implements Function {

        @Override
        public TransienceOption apply(Config config) {
            final TransienceOption option = new TransienceOption();
//            config.get("dependencyInjectorKind").asString().as(String::toUpperCase).as(DependencyInjectorKind::valueOf)
//                    .ifPresent(option::setDependencyInjectorKind);
            config.get("idFactoryKind").asString().as(String::toUpperCase).as(IdFactoryKind::valueOf).ifPresent(option::setIdFactoryKind);
            config.get("wfClasspathes").asList(s -> s.asString().get()).ifPresent(option::setWfClasspathes);
            return option;
        }
    }

    static class PersitenceMapper implements Function {

        @Override
        public PersitenceOption apply(Config config) {
            final PersitenceOption option = new PersitenceOption();
            config.get("datasource").asString().ifPresent(option::setDatasource);
            config.get("engineId").asString().ifPresent(option::setEngineId);
//            config.get("dependencyInjectorKind").asString().as(String::toUpperCase).as(DependencyInjectorKind::valueOf)
//                    .ifPresent(option::setDependencyInjectorKind);
//            config.get("idFactoryKind").asString().as(String::toUpperCase).as(IdFactoryKind::valueOf).ifPresent(option::setIdFactoryKind);
            config.get("wfClasspathes").asList(s -> s.asString().get()).ifPresent(option::setWfClasspathes);
            config.get("maxConnectRetries").asInt().ifPresent(option::setMaxConnectRetries);
            config.get("processorPools").asList(s -> processorPool(s)).ifPresent(option::setProcessorPools);
            config.get("executorService").asString().ifPresent(option::setExecutorService);
            config.get("numberOfBatcherThreads").asInt().ifPresent(option::setNumberOfBatcherThreads);
            config.get("statLoggerIntervalSeconds").asInt().ifPresent(option::setStatLoggerIntervalSeconds);
            config.get("auditTrail").as(s -> auditTrail(s)).ifPresent(option::setAuditTrail);
            return option;
        }

        private PersitenceOption.ProcessorPool processorPool(Config config) {
            final PersitenceOption.ProcessorPool option = new PersitenceOption.ProcessorPool();
            config.get("id").asString().ifPresent(option::setId);
            config.get("numberOfThreads").asInt().ifPresent(option::setNumberOfThreads);
            config.get("lowerThreshold").asInt().ifPresent(option::setLowerThreshold);
            config.get("upperThreshold").asInt().ifPresent(option::setUpperThreshold);
            config.get("upperThresholdReachedWaitMSec").asInt().ifPresent(option::setUpperThresholdReachedWaitMSec);
            config.get("emptyQueueWaitMSec").asInt().ifPresent(option::setEmptyQueueWaitMSec);
            config.get("dequeueBulkSize").asInt().ifPresent(option::setDequeueBulkSize);
            config.get("shutdownWaitIntervalMSec").asInt().ifPresent(option::setShutdownWaitIntervalMSec);
            config.get("threadPriority").asInt().ifPresent(option::setThreadPriority);
            return option;
        }

        private PersitenceOption.AuditTrail auditTrail(Config config) {
            final PersitenceOption.AuditTrail option = new PersitenceOption.AuditTrail();
            config.get("level").asInt().ifPresent(option::setLevel);
            config.get("dbTable").asString().ifPresent(option::setDbTable);
            config.get("compressed").asBoolean().ifPresent(option::setCompressed);
            config.get("mapping").asList(s -> mapping(s)).ifPresent(option::setMapping);
            return option;
        }

        private AbstractAuditTrail.Property2ColumnMapping mapping(Config config) {
            final String column = config.get("column").asString().orElse(null);
            final String property = config.get("property").asString().orElse(null);
            if (column != null && property != null) {
                return new AbstractAuditTrail.Property2ColumnMapping(property, column);
            }
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy