class TomlConfigFactory {

    public ConfigBuilder create(InputStream configData, ArgonOptions options, String... filenames) {
        Objects.requireNonNull(configData, "No config data provided. ");
        if (filenames.length != 0) {
            throw new UnsupportedOperationException("keyConfigData arg is not implemented for TomlConfigFactory");
        }

        Toml toml = new Toml().read(configData);
        toml.toMap().forEach((key, value) -> LOGGER.debug("Found entry in toml file : {} {}", key, value));

        final String urlWithoutPort = Optional
            .ofNullable(toml.getString("url"))
            .map(url -> {
                try {
                    return new URL(url);
                } catch (final MalformedURLException e) {
                    throw new RuntimeException("Bad server url given: " + e.getMessage());
                }
            }).map(uri -> uri.getProtocol() + "://" + uri.getHost())
            .orElse(null);

        final Integer port = Optional.ofNullable(toml.getLong("port")).map(Long::intValue).orElse(0);

        final String workdir = toml.getString("workdir", "");
        final String socket = toml.getString("socket");

        final String tls = toml.getString("tls", "off").toUpperCase();

        final List<String> othernodes = toml.getList("othernodes", emptyList());

        final List<String> alwaysSendToKeyPaths = toml.getList("alwayssendto", emptyList());

        final String storage = toml.getString("storage", "memory");

        final List<String> ipwhitelist = toml.getList("ipwhitelist", emptyList());
        final boolean useWhiteList = !ipwhitelist.isEmpty();

        // Server side
        final String tlsservertrust = toml.getString("tlsservertrust", "tofu");
        final Optional<String> tlsserverkey = Optional.ofNullable(toml.getString("tlsserverkey"));
        final Optional<String> tlsservercert = Optional.ofNullable(toml.getString("tlsservercert"));
        final Optional<List<String>> tlsserverchainnames = Optional.of(toml.getList("tlsserverchain", emptyList()));
        final Optional<String> tlsknownclients = Optional.ofNullable(toml.getString("tlsknownclients"));

        // Client side
        final String tlsclienttrust = toml.getString("tlsclienttrust", "tofu");
        final Optional<String> tlsclientkey = Optional.ofNullable(toml.getString("tlsclientkey"));
        final Optional<String> tlsclientcert = Optional.ofNullable(toml.getString("tlsclientcert"));
        final Optional<List<String>> tlsclientchainnames = Optional.of(toml.getList("tlsclientchain", emptyList()));
        final Optional<String> tlsknownservers = Optional.ofNullable(toml.getString("tlsknownservers"));

        ConfigBuilder configBuilder = ConfigBuilder.create()
            .serverPort(port)
            .serverHostname(urlWithoutPort)
            .unixSocketFile(socket)
            .sslAuthenticationMode(SslAuthenticationMode.valueOf(tls))
            .sslServerTrustMode(SslTrustModeFactory.resolveByLegacyValue(tlsservertrust))
            .sslClientTrustMode(SslTrustModeFactory.resolveByLegacyValue(tlsclienttrust))
            .peers(othernodes)
            .alwaysSendTo(alwaysSendToKeyPaths)
            .useWhiteList(useWhiteList)
            .workdir(workdir);

        tlsserverkey.ifPresent(configBuilder::sslServerTlsKeyPath);
        tlsservercert.ifPresent(configBuilder::sslServerTlsCertificatePath);
        tlsserverchainnames.ifPresent(configBuilder::sslServerTrustCertificates);
        tlsknownclients.ifPresent(configBuilder::sslKnownClientsFile);
        tlsclientkey.ifPresent(configBuilder::sslClientTlsKeyPath);
        tlsclientcert.ifPresent(configBuilder::sslClientTlsCertificatePath);
        tlsclientchainnames.ifPresent(configBuilder::sslClientTrustCertificates);
        tlsknownservers.ifPresent(configBuilder::sslKnownServersFile);

        Optional.ofNullable(storage)
            .map(JdbcConfigFactory::fromLegacyStorageString)
            .ifPresent(configBuilder::jdbcConfig);

        return configBuilder;
    }

}