Skip to content

Commit

Permalink
[1.2.1 - 2024-04-10]
Browse files Browse the repository at this point in the history
ConfigParamsWrapper and Logger
  • Loading branch information
fugerit79 committed Apr 9, 2024
1 parent 793613e commit 3a82107
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.2.1 - 2024-04-10]

### Added

- ConfigParamsWrapper and Logger

## [1.2.0 - 2024-04-09]

### Added
Expand Down
2 changes: 1 addition & 1 deletion data-service-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-service-helper-bom</artifactId>
<version>1.2.0</version>
<version>${revision}</version>
</parent>

<name>Data Service Base</name>
Expand Down
2 changes: 1 addition & 1 deletion data-service-jvfs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-service-helper-bom</artifactId>
<version>1.2.0</version>
<version>${revision}</version>
</parent>

<name>Data Service JVFS</name>
Expand Down
2 changes: 1 addition & 1 deletion emp-exception-mapper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-service-helper-bom</artifactId>
<version>1.2.0</version>
<version>${revision}</version>
</parent>

<name>Eclipse MicroProfile Exception Mapper</name>
Expand Down
2 changes: 1 addition & 1 deletion emp-service-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-service-helper-bom</artifactId>
<version>1.2.0</version>
<version>${revision}</version>
</parent>

<name>Eclipse MicroProfile Service Model</name>
Expand Down
2 changes: 1 addition & 1 deletion fj-service-helper-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-service-helper-bom</artifactId>
<version>1.2.0</version>
<version>${revision}</version>
</parent>
<artifactId>fj-service-helper-demo</artifactId>
<name>Fugerit Service Helper DEMO</name>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
<relativePath></relativePath>
</parent>

<version>1.2.0</version>
<version>${revision}</version>
<packaging>pom</packaging>

<name>Fugerit Service Helper BOM</name>
<description>Project helper for service component abstraction</description>
<url>https://www.fugerit.org/</url>

<properties>
<revision>1.1.0</revision>
<revision>1.2.1</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java-version-compliance>8</java-version-compliance>
<maven.compiler.source>${java-version-compliance}</maven.compiler.source>
Expand Down
2 changes: 1 addition & 1 deletion quarkus-redis-service-map/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-service-helper-bom</artifactId>
<version>1.2.0</version>
<version>${revision}</version>
</parent>

<name>Service Map based on Quarkus 3+ Redis Client</name>
Expand Down
2 changes: 1 addition & 1 deletion simple-config-microprofile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-service-helper-bom</artifactId>
<version>1.2.0</version>
<version>${revision}</version>
</parent>

<name>simple-config-microprofile</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
package org.fugerit.java.simple.config.microprofile;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.fugerit.java.simple.config.ConfigParams;
import org.fugerit.java.simple.config.AbstractConfigParams;

import java.util.Optional;

public class ConfigParamsMicroprofile implements ConfigParams {
public class ConfigParamsMicroprofile extends AbstractConfigParams {

public ConfigParamsMicroprofile() {
this(DEFAULT_NAMESPACE, ConfigProvider.getConfig());
}

public ConfigParamsMicroprofile(String namespace, Config config) {
super(namespace);
this.config = config;
}

private Config config;

@Override
public String getValue(String name) {
return ConfigProvider.getConfig().getValue( name, String.class );
protected Optional<String> getOptionalValueNamespace(String name) {
return this.config.getOptionalValue( name, String.class );
}

@Override
public Optional<String> getOptionalValue(String name) {
return ConfigProvider.getConfig().getOptionalValue( name, String.class );
protected String getValueNamespace(String name) {
return this.config.getValue( name, String.class );
}

}
7 changes: 7 additions & 0 deletions simple-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ ConfigParams config = new ConfigParamsDefault( configProperties );
String value1 = config.getValue( "testconfig.param1" );
Optional<String> value2 = config.getOptionalValue( "testconfig.param2" );
```

Or added behaviours, for instance logging :

```java
ConfigParams config = ConfigParamsLogger.wrapLogDebug( new ConfigParamsDefault( configProperties ) );
```

2 changes: 1 addition & 1 deletion simple-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-service-helper-bom</artifactId>
<version>1.2.0</version>
<version>${revision}</version>
</parent>

<name>simple-config</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.fugerit.java.simple.config;

import java.util.Optional;

public abstract class AbstractConfigParams implements ConfigParams {

public static final String DEFAULT_NAMESPACE = "";

private String configNamespace;

protected AbstractConfigParams(String configNamespace) {
this.configNamespace = configNamespace == null ? DEFAULT_NAMESPACE : configNamespace;
}

protected abstract String getValueNamespace( String name );

protected abstract Optional<String> getOptionalValueNamespace( String name );

protected String toNamespace( String name ) {
return this.configNamespace+name;
}

@Override
public String getValue(String name) {
return this.getValueNamespace( this.toNamespace( name ) );
}

@Override
public Optional<String> getOptionalValue(String name) {
return this.getOptionalValueNamespace( this.toNamespace( name ) );
}

}
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
package org.fugerit.java.simple.config;

import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.Properties;

@Slf4j
public class ConfigParamsDefault implements ConfigParams {

private String configNamespace;
public class ConfigParamsDefault extends AbstractConfigParams implements ConfigParams {

private Properties configProperties;

public ConfigParamsDefault(String configNamespace, Properties configProperties) {
this.configNamespace = configNamespace;
super( configNamespace );
this.configProperties = configProperties;
}

public ConfigParamsDefault(Properties configProperties) {
this( "", configProperties );
}

@Override
public String getValue(String name) {
String key = this.configNamespace+name;
String value = this.configProperties.getProperty( this.configNamespace+name );
log.debug( "key : {}, value : {}", key, value );
return value;
this( DEFAULT_NAMESPACE, configProperties );
}

@Override
public Optional<String> getOptionalValue(String name) {
String value = this.getValue( name );
protected Optional<String> getOptionalValueNamespace(String name) {
String value = this.getValueNamespace( name );
if ( value == null ) {
return Optional.empty();
} else {
return Optional.of( value );
}
}

@Override
protected String getValueNamespace(String name) {
return this.configProperties.getProperty( name );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.fugerit.java.simple.config;

import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.function.BiConsumer;

@Slf4j
public class ConfigParamsLogger extends ConfigParamsWrapper {

public static ConfigParams wrapLogDebug( ConfigParams params ) {
return new ConfigParamsLogger( params );
}

private BiConsumer<String, Object> logFun;

public ConfigParamsLogger(final ConfigParams wrapped) {
this( wrapped, ( n, v ) -> log.debug( "wraps : {}, name : {}, value : {} ", wrapped, n, v ) );
}

public ConfigParamsLogger(final ConfigParams wrapped, final BiConsumer<String, Object> logFun) {
super( wrapped );
this.logFun = logFun;
}

@Override
public String getValue(String name) {
String value = super.getValue( name );
this.logFun.accept( name, value );
return value;
}

@Override
public Optional<String> getOptionalValue(String name) {
Optional<String> value = super.getOptionalValue( name );
this.logFun.accept( name, value );
return value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.fugerit.java.simple.config;

import java.util.Optional;

public class ConfigParamsWrapper implements ConfigParams {

public ConfigParamsWrapper(ConfigParams wrapped) {
this.wrapped = wrapped;
}

private ConfigParams wrapped;

@Override
public String getValue(String name) {
return this.wrapped.getValue(name);
}

@Override
public Optional<String> getOptionalValue(String name) {
return this.wrapped.getOptionalValue(name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.fugerit.java.simple.config.ConfigParams;
import org.fugerit.java.simple.config.ConfigParamsDefault;
import org.fugerit.java.simple.config.ConfigParamsLogger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -18,13 +19,17 @@ void testConfigParams() throws IOException {
try (InputStream is = new FileInputStream( "src/test/resources/testconfig.properties" ) ) {
Properties configProperties = new Properties();
configProperties.load( is );
ConfigParams config = new ConfigParamsDefault( configProperties );
ConfigParams config = ConfigParamsLogger.wrapLogDebug( new ConfigParamsDefault( configProperties ) );
String value1 = config.getValue( "testconfig.param1" );
Assertions.assertEquals( "value1", value1 );
Optional<String> value2 = config.getOptionalValue( "testconfig.param2" );
Assertions.assertEquals( "value2", value2.get() );
Optional<String> valueX = config.getOptionalValue( "testconfig.paramX" );
Assertions.assertFalse( valueX.isPresent() );
// test null namepsace
ConfigParams configAlt = new ConfigParamsDefault( null, configProperties );
String value3 = config.getValue( "testconfig.param3" );
Assertions.assertEquals( "value3", value3 );
}
}

Expand Down

0 comments on commit 3a82107

Please sign in to comment.