Skip to content

Commit

Permalink
OAP-244 Add "include" support to hocon configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
nofateg authored Feb 27, 2024
1 parent 50b785a commit fe3018e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ public void testFinalParameter() {
try {
TestDirectoryFixture.deployTestData( getClass() );


assertThatThrownBy( () -> kernel.start( Map.of( "boot.main", "testFinalParameter" ) ) )
.isInstanceOf( ApplicationException.class )
.hasMessageContaining( "al=[val1], a=new value" );
Expand All @@ -425,6 +426,17 @@ public void testFinalParameter() {
}
}

@Test
public void testInclude() {
try( var kernel = new Kernel(
List.of( urlOfTestResource( getClass(), "testInclude.conf" ) ) ) ) {
kernel.start( Map.of( "boot.main", "testInclude" ) );

assertThat( kernel.<Service3>service( "testInclude.service3" ).get().name ).isEqualTo( "a" );
assertThat( kernel.<Service3>service( "testInclude.service32" ).get().name ).isEqualTo( "b" );
}
}

public enum Enum {
ONE, TWO
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services {
service32 {
implementation = oap.application.KernelTest.Service3
parameters {
name = b
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name = testInclude

include required("testInclude-2.conf")

services {
service3 {
implementation = oap.application.KernelTest.Service3
parameters {
name = a
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import lombok.SneakyThrows;
import lombok.ToString;
import oap.io.Resources;
import oap.io.content.ContentReader;
import oap.json.Binder;
import oap.util.Lists;
import oap.util.Strings;
Expand Down Expand Up @@ -63,7 +62,7 @@ public List<URL> urlsFromClassPath() {

@SneakyThrows
public T fromUrl( URL url ) {
return fromString( ContentReader.read( url, ContentReader.ofString() ), Binder.Format.of( url, true ) );
return Binder.Format.of( url, true ).binder.unmarshal( clazz, url );
}

public T fromResource( Class<?> contextClass, String name ) {
Expand Down
15 changes: 7 additions & 8 deletions oap-stdlib/src/main/java/oap/json/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ public static void update( Object obj, String json ) {
}
}

private static String getLimitation( String json ) {
if( json != null && json.length() > 20 ) return json.substring( 0, 20 ) + "...";
return json;
}

public ObjectMapper getMapper() {
return mapper;
}
Expand Down Expand Up @@ -405,8 +410,8 @@ public <T> T unmarshal( Class<T> clazz, Path path ) throws JsonException {
}

public <T> T unmarshal( Class<T> clazz, URL url ) throws JsonException {
try( var in = url.openStream() ) {
return unmarshal( clazz, in );
try {
return mapper.readValue( url, clazz );
} catch( IOException e ) {
throw new JsonException( "Cannot deserialize to class: " + clazz.getCanonicalName(), e );
}
Expand Down Expand Up @@ -620,7 +625,6 @@ public ObjectWriter writerFor( TypeReference<?> ref ) {
return mapper.writerFor( ref );
}


@SuppressWarnings( "unchecked" )
public <T> T clone( T object ) {
return unmarshal( ( Class<T> ) object.getClass(), marshal( object ) );
Expand Down Expand Up @@ -649,9 +653,4 @@ public static Format of( String path, boolean withSystemProperties ) {
return withSystemProperties ? HOCON : HOCON_WO_SYSTEM_PROPERTIES;
}
}

private static String getLimitation( String json ) {
if ( json != null && json.length() > 20 ) return json.substring( 0, 20 ) + "...";
return json;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@
import com.typesafe.config.ConfigException;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigParseOptions;
import lombok.SneakyThrows;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;

public class HoconFactoryWithSystemProperties extends HoconFactory {
private final Logger log;
Expand All @@ -44,9 +50,27 @@ public HoconFactoryWithSystemProperties( Logger log ) {
// if( log.isTraceEnabled() ) System.setProperty( "config.trace", "loads" );
}

@SneakyThrows
@Override
protected HoconTreeTraversingParser _createParser( Reader r, IOContext ctxt ) throws IOException {
var options = ConfigParseOptions.defaults();

Object rawContent = ctxt.contentReference().getRawContent();
if( rawContent instanceof URL urlContext ) {
URL parentURL = Paths.get( urlContext.toURI() ).getParent().toUri().toURL();

options = options.setClassLoader( new URLClassLoader( new URL[] { parentURL } ) );
} else if( rawContent instanceof File fileContext ) {
URL parentURL = Paths.get( fileContext.toURI() ).getParent().toUri().toURL();

options = options.setClassLoader( new URLClassLoader( new URL[] { parentURL } ) );
} else if( rawContent instanceof URI uriContext ) {
URL parentURL = Paths.get( uriContext ).getParent().toUri().toURL();

options = options.setClassLoader( new URLClassLoader( new URL[] { parentURL } ) );
}


var config = ConfigFactory.parseReader( r, options );

var unresolvedConfig = config.withFallback( ConfigFactory.systemProperties() );
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</repositories>

<properties>
<oap.project.version>21.15.0</oap.project.version>
<oap.project.version>21.15.1</oap.project.version>

<oap.deps.config.version>21.0.0</oap.deps.config.version>
<oap.deps.oap-teamcity.version>21.0.1</oap.deps.oap-teamcity.version>
Expand Down Expand Up @@ -109,7 +109,7 @@
Metrics.gauge( "task_queue_extra", Tags.of( "type", "current" ), this, 5 );
-->
<oap.deps.micrometer.version>1.11.2</oap.deps.micrometer.version>
<oap.deps.micrometer.version>1.11.3</oap.deps.micrometer.version>

<oap.deps.fastutil.version>8.5.12</oap.deps.fastutil.version>
<oap.deps.quartz.version>2.3.2</oap.deps.quartz.version>
Expand Down

0 comments on commit fe3018e

Please sign in to comment.