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

io.github.lc.oss.commons.encryption.config.EncryptedConfig Maven / Gradle / Ivy

The newest version!
package io.github.lc.oss.commons.encryption.config;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import io.github.lc.oss.commons.util.TypedEnumCache;

public abstract class EncryptedConfig {
    public static class User {
        private final String username;
        private final String password;

        public User(String username, String password) {
            this.username = username;
            this.password = password;
        }

        public String getUsername() {
            return this.username;
        }

        public String getPassword() {
            return this.password;
        }
    }

    private final TypedEnumCache keyCache;
    private final Map map = new HashMap<>();

    public > EncryptedConfig(Class keyType) {
        this.keyCache = new TypedEnumCache<>(keyType);
    }

    @JsonAnySetter
    public void set(String key, Object value) {
        if (!this.keyCache.hasName(key)) {
            this.logError(String.format("Ignoring unknown key '%s'.", key));
            return;
        }
        this.set(this.keyCache.byName(key), value);
    }

    @SuppressWarnings("unchecked")
    public void set(ConfigKey key, Object value) {
        if (value instanceof Map) {
            Map map = (Map) value;
            if (map.containsKey("username") && map.containsKey("password") && map.size() == 2) {
                this.map.put(key, new User(map.get("username"), map.get("password")));
            } else {
                this.map.put(key, value);
            }
        } else {
            this.map.put(key, value);
        }
    }

    public Object get(ConfigKey key) {
        return this.map.get(key);
    }

    protected void logError(String message) {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy