Skip to content

Commit

Permalink
Integrerer fp-konfig i fp-felles.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsladek committed Nov 17, 2023
2 parents 4eb338f + 1e3582c commit c761931
Show file tree
Hide file tree
Showing 23 changed files with 1,567 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package no.nav.foreldrepenger.konfig;

import static java.lang.System.getenv;

import java.util.Optional;

public class Application {

private final String name;

private Application(String name) {
this.name = name;
}

public static Application of(String name) {
return new Application(name);
}

public String getName() {
return name;
}

public static Application current() {
return Application.of(Optional.ofNullable(getenv(NaisProperty.APPLICATION.propertyName())).orElse("vtp"));
}

@Override
public String toString() {
return getClass().getSimpleName() + "[application=" + name + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package no.nav.foreldrepenger.konfig;

import static java.lang.System.getenv;
import static no.nav.foreldrepenger.konfig.StandardPropertySource.APP_PROPERTIES;

import java.io.IOException;
import java.util.Optional;
import java.util.Properties;

import jakarta.enterprise.context.Dependent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import no.nav.foreldrepenger.konfig.KonfigVerdi.Converter;

@Dependent
public class ApplicationPropertiesKonfigProvider extends PropertiesKonfigVerdiProvider {

static class Init {
// lazy init singleton
static final Properties PROPS = lesFra();
private static final String SUFFIX = ".properties";
private static final String PREFIX = "application";

private Init() {
}

private static Properties lesFra() {
var c = new Properties();
lesFra(namespaceKonfig(), lesFra(clusterKonfig(), lesFra("", new Properties())))
.forEach((k, v) -> c.put(k.toString().toLowerCase(), v.toString()));
return c;
}

private static Properties lesFra(String infix, Properties p) {
if (infix == null) {
return p;
}
String navn = PREFIX + infix + SUFFIX;
try (var is = ApplicationPropertiesKonfigProvider.class.getClassLoader().getResourceAsStream(navn)) {
if (is != null) {
LOG.info("Laster properties fra {}", navn);
p.load(is);
return p;
}
} catch (IOException e) {
LOG.info("Propertyfil {} ikke lesbar", navn);
}
LOG.info("Propertyfil {} ikke funnet", navn);
return p;
}

private static String namespaceKonfig() {
var namespaceName = namespaceName();
if (namespaceName != null) {
return clusterKonfig() + "-" + namespaceName;
} else {
var appName = System.getProperty("app.name");
if (appName != null) {
return clusterKonfig() + "-" + appName;
}
}
return null;
}

private static String namespaceName() {
return getenv(NaisProperty.NAMESPACE.propertyName());
}

private static String clusterKonfig() {
return "-" + clusterName();
}

private static String clusterName() {
return Optional.ofNullable(getenv(NaisProperty.CLUSTER.propertyName()))
.orElse(Cluster.VTP.clusterName());
}
}

private static final int PRIORITET = EnvPropertiesKonfigVerdiProvider.PRIORITET + 1;

private static final Logger LOG = LoggerFactory.getLogger(ApplicationPropertiesKonfigProvider.class);

public ApplicationPropertiesKonfigProvider() {
super(Init.PROPS, APP_PROPERTIES);
}

@Override
public <V> V getVerdi(String key, Converter<V> converter) {
return Optional.ofNullable(super.getVerdi(key.toLowerCase(), converter))
.orElse(null);
}

@Override
public boolean harVerdi(String key) {
return super.harVerdi(key.toLowerCase());
}

@Override
public int getPrioritet() {
return PRIORITET;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package no.nav.foreldrepenger.konfig;

import static java.lang.System.getenv;

public class ClientId {

private static final String DELIMIT = ":";

private final String id;

private ClientId(String clientId) {
this.id = clientId;
}

public static ClientId of(String clientId) {
return new ClientId(clientId);
}

public static ClientId of(Cluster cluster, Namespace namespace, Application application) {
return of(cluster.clusterName(), namespace.getName(), application.getName());
}

public static ClientId of(Cluster cluster, Namespace namespace, String application) {
return of(cluster.clusterName(), namespace.getName(), application);
}

private static ClientId of(String cluster, String namespace, String application) {
return of(cluster + DELIMIT + namespace + DELIMIT + application);
}

public String getClientId() {
return id;
}

public static ClientId current() {
return ClientId.of(getenv(NaisProperty.CLIENTID.propertyName()));
}

@Override
public String toString() {
return getClass().getSimpleName() + "[clientId=" + id + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package no.nav.foreldrepenger.konfig;

import static java.lang.System.getenv;

import java.util.Arrays;
import java.util.Objects;

public enum Cluster {
VTP("vtp"),
DEV_FSS("dev-fss"),
DEV_GCP("dev-gcp"),
PROD_GCP("prod-gcp"),
PROD_FSS("prod-fss");

private static final String PROD = "prod";
private static final String DEV = "dev";
private static final String FSS = "fss";
private static final String GCP = "gcp";

private final String name;

Cluster(String name) {
this.name = name;
}

public String clusterName() {
return name;
}

public boolean isProd() {
return name.startsWith(PROD);
}

public boolean isDev() {
return name.startsWith(DEV);
}

boolean isVTP() {
return name.startsWith(VTP.name);
}

boolean isFss() {
return name.endsWith(FSS);
}

boolean isGcp() {
return name.endsWith(GCP);
}

public boolean isLocal() {
return !isProd() && !isDev();
}

public boolean isSameClass(Cluster other) {
return name.substring(0, 2).equals(other.name.substring(0, 2));
}

public boolean isCoLocated(Cluster other) {
return name.substring(name.length() - 3).equals(other.name.substring(other.name.length() - 3));
}

public static Cluster current() {
var active = getenv(NaisProperty.CLUSTER.propertyName());
return Arrays.stream(values())
.filter(c -> active != null && Objects.equals(active, c.name))
.findFirst()
.orElse(VTP);
}

public static Cluster of(String name) {
return Arrays.stream(values())
.filter(v -> v.name.equals(name))
.findFirst()
.orElseThrow();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package no.nav.foreldrepenger.konfig;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

import no.nav.foreldrepenger.konfig.KonfigVerdi.Converter;

public class DefaultValueKonfigProvider implements KonfigVerdiProvider {

@Override
public PropertySourceMetaData getAllProperties() {
return new PropertySourceMetaData(StandardPropertySource.DEFAULT, new Properties());
}

@Override
public StandardPropertySource getSource() {
return StandardPropertySource.DEFAULT;
}

@Override
public <V> V getVerdi(String verdi, Converter<V> converter) {
return converter.tilVerdi(verdi);
}

@Override
public <V> List<V> getVerdier(String verdier, Converter<V> converter) {
return Arrays.stream(verdier.split(",\\s*"))
.map(converter::tilVerdi)
.toList();

}

@Override
public <V> Map<String, V> getVerdierAsMap(String verdier, Converter<V> converter) {
return Arrays.stream(verdier.split(",\\s*"))
.map(s -> s.split(":\\s*"))
.collect(
Collectors.toMap(
e -> e[0],
e -> converter.tilVerdi(e[1])
));
}

@Override
public boolean harVerdi(String key) {
return true;
}

@Override
public int getPrioritet() {
return 0;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package no.nav.foreldrepenger.konfig;

import no.nav.foreldrepenger.konfig.KonfigVerdi.Converter;

import jakarta.enterprise.context.Dependent;
import java.util.Optional;
import java.util.Properties;

import static no.nav.foreldrepenger.konfig.StandardPropertySource.ENV_PROPERTIES;

@Dependent
public class EnvPropertiesKonfigVerdiProvider extends PropertiesKonfigVerdiProvider {

public static final int PRIORITET = SystemPropertiesKonfigVerdiProvider.PRIORITET + 1;

static class Init {
// lazy init singleton
static final Properties ENV = getEnv();

private Init() {
}

private static Properties getEnv() {
var p = new Properties();
p.putAll(System.getenv());
return p;
}
}

public EnvPropertiesKonfigVerdiProvider() {
super(Init.ENV, ENV_PROPERTIES);
}

@Override
public <V> V getVerdi(String key, Converter<V> converter) {
return Optional.ofNullable(super.getVerdi(key, converter))
.orElse(super.getVerdi(upper(key), converter));
}

@Override
public boolean harVerdi(String key) {
return super.harVerdi(key) || super.harVerdi(upper(key));
}

private static String upper(String key) {
return key.toUpperCase().replace('.', '_');
}

@Override
public int getPrioritet() {
return PRIORITET;
}
}
Loading

0 comments on commit c761931

Please sign in to comment.