Skip to content

Commit

Permalink
OAP-117 Support for properties for classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nofateg authored Apr 8, 2024
1 parent 3acfc8e commit 6d9e827
Show file tree
Hide file tree
Showing 73 changed files with 785 additions and 634 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
package oap.application.testng;

import com.google.common.base.Preconditions;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import oap.application.ApplicationConfiguration;
import oap.application.Kernel;
import oap.application.module.Module;
import oap.http.test.HttpAsserts;
import oap.io.IoStreams;
import oap.io.Resources;
import oap.json.Binder;
import oap.json.JsonException;
Expand All @@ -40,6 +42,8 @@
import org.apache.commons.io.FilenameUtils;

import javax.annotation.Nonnull;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URL;
Expand Down Expand Up @@ -77,6 +81,7 @@ public abstract class AbstractKernelFixture<Self extends AbstractKernelFixture<S
private final ArrayList<Pair<Class<?>, String>> confd = new ArrayList<>();
private final ArrayList<Pair<Class<?>, String>> conf = new ArrayList<>();
private final LinkedHashMap<String, AbstractFixture<?>> dependencies = new LinkedHashMap<>();
private final LinkedHashSet<String> bootMain = new LinkedHashSet<>();
public Kernel kernel;
protected Path confdPath;
private int testHttpPort;
Expand Down Expand Up @@ -167,6 +172,17 @@ public Self withLocalConfResource( Class<?> clazz, String confdResource ) throws
return withConfResource( clazz, cr );
}

public Self withAllowActiveByDefault( boolean allowActiveByDefault ) {
return define( "main.allowActiveByDefault", allowActiveByDefault );
}

@SuppressWarnings( "unchecked" )
public Self withBootMain( String... modules ) {
this.bootMain.addAll( List.of( modules ) );

return ( Self ) this;
}

private void initConfd() {
if( this.confdPath == null )
this.confdPath = testDirectoryFixture.testPath( "/application.test.confd" );
Expand Down Expand Up @@ -198,6 +214,7 @@ public <T> List<T> ofClass( String moduleName, Class<T> clazz ) {
return kernel.ofClass( moduleName, clazz );
}

@SneakyThrows
@Override
protected void before() {
Preconditions.checkArgument( this.kernel == null );
Expand All @@ -217,12 +234,18 @@ protected void before() {
}

for( var cd : conf ) {
var p = Resources.filePath( cd._1, cd._2 );
p.ifPresentOrElse( path -> {
Path destPath = confdPath.resolve( path.getFileName() );
log.info( "Copying file " + path + " -> " + destPath );
oap.io.Files.copy( path, PLAIN, destPath, PLAIN );
}, () -> log.warn( "Configuration file " + cd + " is not found" ) );
var url = Resources.url( cd._1, cd._2 ).orElse( null );
if( url == null ) {
throw new FileNotFoundException( "Configuration file " + cd + " is not found" );
}
Path destPath = confdPath.resolve( FilenameUtils.getName( url.toString() ) );
log.info( "Copying file " + url + " -> " + destPath );

try( var is = IoStreams.in( url ) ) {
IoStreams.write( destPath, PLAIN, is );
} catch( IOException e ) {
throw new UncheckedIOException( e );
}
}

var moduleConfigurations = Module.CONFIGURATION.urlsFromClassPath();
Expand All @@ -233,6 +256,10 @@ protected void before() {

var kernelProperties = new LinkedHashMap<>( properties );

if( !bootMain.isEmpty() ) {
profiles.add( "boot.main = ${boot.main} [" + String.join( ",", bootMain ) + "]" );
}

dependencies.forEach( ( name, fixture ) -> {
kernelProperties.put( name, fixture.getProperties() );
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,31 @@ public void testServiceWithoutImplementation() {
@Test
public void testLoadModules() {
var modules = List.of(
url( "deps/m1.yaml" ),
url( "deps/m2.yaml" ),
url( "deps/m3.yaml" ),
url( "deps/m4.yaml" )
url( "deps/m1.conf" ),
url( "deps/m2.conf" ),
url( "deps/m3.conf" ),
url( "deps/m4.conf" ),
url( "deps/activeByDefault.conf" ),
url( "deps/m5.conf" )
);

try( var kernel = new Kernel( modules ) ) {
kernel.start( Map.of( "boot.main", "m1" ) );
kernel.start( Map.of( "boot", Map.of( "main", "m1", "allowActiveByDefault", true ) ) );

assertThat( kernel.service( "m1.s11" ) ).isPresent();
assertThat( kernel.service( "m2.s21" ) ).isNotPresent();
assertThat( kernel.service( "m1.s31" ) ).isNotPresent();
assertThat( kernel.service( "m3.s31" ) ).isPresent();
assertThat( kernel.service( "m4.s41" ) ).isPresent();
assertThat( kernel.service( "activeByDefault.sa" ) ).isPresent();
assertThat( kernel.service( "m5.s5" ) ).isPresent();
}

try( var kernel = new Kernel( modules ) ) {
kernel.start( Map.of( "boot.main", "m1" ) );

assertThat( kernel.service( "activeByDefault.sa" ) ).isNotPresent();
assertThat( kernel.service( "m5.s5" ) ).isNotPresent();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name = activeByDefault
activation.activeByDefault = true

dependsOn = m5

services.sa.implementation = oap.application.KernelTest.Service1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = m1
dependsOn = m3
services.s11.implementation = oap.application.KernelTest.Service1

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = m2
services.s21.implementation = oap.application.KernelTest.Service1

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = m3
dependsOn = m4
services.s31.implementation = oap.application.KernelTest.Service1

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = m4
services.s41.implementation = oap.application.KernelTest.Service1

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = m5
services.s5.implementation = oap.application.KernelTest.Service1
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
boot.main = oap-module-with-remoting

profiles = [
http
remoting
]

services {
oap-http {
oap-http-server.parameters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ static ApplicationConfiguration load( Map<String, Object> properties ) {
public static List<URL> getConfdUrls( Path confd ) {
return confd != null
? Stream
.of( wildcard( confd, "*.conf", "*.yaml" ) )
.map( Files::toUrl )
.toList()
.of( wildcard( confd, "*.conf", "*.yaml" ) )
.map( Files::toUrl )
.toList()
: List.of();
}

Expand Down Expand Up @@ -233,6 +233,7 @@ public boolean isEnabled() {
@ToString
public static class ModuleBoot {
public final LinkedHashSet<String> main = new LinkedHashSet<>();
public boolean allowActiveByDefault = false;
}

public static class ProfileMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void start( ApplicationConfiguration config ) throws ApplicationException
checkForUnknownServices( config.services );

log.debug( "init modules from main: {}", config.boot.main );
var map = ModuleHelper.init( this.modules, this.profiles, config.boot.main, this );
var map = ModuleHelper.init( this.modules, this.profiles, config.boot.main, config.boot.allowActiveByDefault, this );

var servicesMap = instantiateServices( map );
registerServices( servicesMap );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ private static ModuleItemTree init( LinkedHashSet<Kernel.ModuleWithLocation> mod
public static ModuleItemTree init( LinkedHashSet<Kernel.ModuleWithLocation> modules,
LinkedHashSet<String> profiles,
LinkedHashSet<String> main,
boolean allowActiveByDefault,
Kernel kernel ) throws ApplicationException {
log.trace( "Init modules: {}, profiles: {}, main: {}", modules, profiles, main );
log.trace( "Init modules {} profiles {} main {} allowActiveByDefault {}", modules, profiles, main, allowActiveByDefault );
var map = init( modules, profiles );
loadOnlyMainModuleAndDependsOn( map, main, profiles );
loadOnlyMainModuleAndDependsOn( map, main, allowActiveByDefault, profiles );

validateModuleName( map );
validateServiceName( map );
Expand Down Expand Up @@ -217,10 +218,11 @@ else if( value instanceof Map<?, ?> ) {
}
}

private static void loadOnlyMainModuleAndDependsOn( ModuleItemTree map, LinkedHashSet<String> main, LinkedHashSet<String> profiles ) {
private static void loadOnlyMainModuleAndDependsOn( ModuleItemTree map, LinkedHashSet<String> main,
boolean allowActiveByDefault, LinkedHashSet<String> profiles ) {
var modules = map.clone();
log.info( "loading main modules: {} with profiles: {}", main, profiles );
loadOnlyMainModuleAndDependsOn( modules, main, profiles, new LinkedHashSet<>() );
log.info( "loading main modules {} with profiles {}", main, profiles );
loadOnlyMainModuleAndDependsOn( modules, main, allowActiveByDefault, profiles, new LinkedHashSet<>() );

for( var moduleItem : modules.values() ) {
log.debug( "unload module {}", moduleItem.getName() );
Expand All @@ -230,13 +232,26 @@ private static void loadOnlyMainModuleAndDependsOn( ModuleItemTree map, LinkedHa

private static void loadOnlyMainModuleAndDependsOn( ModuleItemTree modules,
final LinkedHashSet<String> main,
boolean allowActiveByDefault,
final LinkedHashSet<String> profiles,
final LinkedHashSet<String> loaded ) {
for( var module : main ) {

var mainWithAllowActiveByDefault = new LinkedHashSet<>( main );
if( allowActiveByDefault ) {
for( var moduleName : modules.keySet() ) {
var moduleItem = modules.get( moduleName );
if( moduleItem.module.activation.activeByDefault ) {
mainWithAllowActiveByDefault.add( moduleName );
}
}
}

for( var module : mainWithAllowActiveByDefault ) {
var moduleItem = modules.get( module );

if( moduleItem == null && !loaded.contains( module ) )
if( moduleItem == null && !loaded.contains( module ) ) {
throw new ApplicationException( "main.boot: unknown module name '" + module + "', already loaded: " + loaded );
}

if( moduleItem != null ) {
log.trace( "Loading module: {}, already loaded: {}", moduleItem.getName(), loaded );
Expand All @@ -254,7 +269,7 @@ private static void loadOnlyMainModuleAndDependsOn( ModuleItemTree modules,
log.trace( "dependant module {} disabled for module {}", depends.name, module );
}
}
loadOnlyMainModuleAndDependsOn( modules, dependsOn, profiles, loaded );
loadOnlyMainModuleAndDependsOn( modules, dependsOn, allowActiveByDefault, profiles, loaded );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class Module {
@JsonIgnore
public LinkedHashMap<String, Object> ext = new LinkedHashMap<>();

public final ModuleActivation activation = new ModuleActivation();

@JsonCreator
public Module( String name ) {
this.name = name;
Expand All @@ -70,4 +72,8 @@ public Map<String, Object> getUnknown() {
return ext;
}

@ToString
public static class ModuleActivation {
public boolean activeByDefault = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ dependsOn = oap-http
services {
remoting {
implementation = oap.application.remote.Remote
profiles = [
http
remoting
]
parameters {
server = <modules.oap-http.oap-http-server>
context = /remote/
Expand Down
2 changes: 0 additions & 2 deletions oap-http/oap-http/src/main/resources/META-INF/oap-module.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ services {
}

oap-http-server {
profile = http
implementation = oap.http.server.nio.NioHttpServer
parameters {
defaultPort {
Expand Down Expand Up @@ -65,7 +64,6 @@ services {
}

oap-http-health-handler {
profile = http
implementation = oap.http.server.nio.health.HealthHttpHandler
parameters {
server = <modules.this.oap-http-server>
Expand Down
13 changes: 1 addition & 12 deletions oap-mail/oap-mail/src/main/resources/META-INF/oap-module.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,13 @@ name = oap-mail
services {
oap-mail-password-authenticator.implementation = oap.mail.PasswordAuthenticator

oap-mail-transport-smtp {
profile = oap-mail-javamail
name = oap-mail-transport
oap-mail-transport {
implementation = oap.mail.SmtpTransport
parameters {
authenticator = <modules.this.oap-mail-password-authenticator>
}
}

oap-mail-transport-mock {
profile = -oap-mail-javamail
name = oap-mail-transport
implementation = oap.mail.TransportMock
parameters {
authenticator = <modules.this.oap-mail-password-authenticator>
}
}

oap-mail-queue.implementation: oap.mail.MailQueue

oap-mail-mailman {
Expand Down
1 change: 0 additions & 1 deletion oap-message/src/main/resources/META-INF/oap-module.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ dependsOn = oap-http
services {
oap-http-message-handler {
profiles = [
http
oap-message-server
]
implementation = oap.message.MessageHttpHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ boot.main = [
]

profiles = [
http
oap-message-sender
oap-message-server
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ dependsOn = [
services {
message-listener-mock {
profiles = [
http
oap-message-server
]
implementation = oap.message.MessageListenerMock
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name = oap-statsdb-master

configurations = [
{
loader = oap.json.TypeIdFactory
config = {
mock-value = oap.statsdb.StatsDBTest.MockValue
mock-child2 = oap.statsdb.StatsDBTest.MockChild2
mock-child1 = oap.statsdb.StatsDBTest.MockChild1
}
}
]
Loading

0 comments on commit 6d9e827

Please sign in to comment.