Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mageddo committed Aug 6, 2024
1 parent 49c7057 commit 181f5bb
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 27 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/mageddo/dnsproxyserver/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void checkHiddenCommands() {
if (this.flags.isCreateTmpDir()) {
this.createTmpDirIfNotExists();
}
log.trace("status=checked");
}

Config findConfig(String[] args) {
Expand All @@ -72,7 +73,9 @@ Config findConfig(String[] args) {
}

void setupLogs() {
log.trace("status=configuring");
new LogSettings().setupLogs(this.config);
log.trace("status=configured");
}

void startContext() {
Expand All @@ -87,6 +90,7 @@ void checkExitCommands() {
if (flags.isHelp() || flags.isVersion()) {
exitGracefully();
}
log.trace("status=checked");
}

void exitGracefully() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ public class Configs {
private static final Context context = Context.create();

public static Config getInstance() {
final Config v = Singletons.get(Config.class);
if (v != null) {
return v;
} else {
return Singletons.createOrGet(Config.class, () -> {
return context.configService().findCurrentConfig();
});
}
return Singletons.createOrGet(Config.class, () -> {
log.trace("status=cacheHotLoading");
return context.configService().findCurrentConfig();
});
}

public static void clear() {
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/com/mageddo/dnsproxyserver/di/StartupEvent.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
package com.mageddo.dnsproxyserver.di;

import java.util.Collection;
import java.util.Objects;

public interface StartupEvent {

static boolean exists(Collection<StartupEvent> events, Class<?> clazz) {
return lookup(events, clazz) != null;
}

static <T> T lookup(Collection<StartupEvent> events, Class<T> clazz) {
return (T) events
.stream()
.filter(it -> Objects.equals(clazz, it.getClass()))
.findFirst()
.orElse(null)
;
}

void onStart();
}
23 changes: 23 additions & 0 deletions src/main/java/com/mageddo/dnsproxyserver/di/StartupEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mageddo.dnsproxyserver.di;

import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.Objects;

@Slf4j
public class StartupEvents {
public static boolean exists(Collection<StartupEvent> events, Class<?> classToFind) {
return lookup(events, classToFind) != null;
}

public static <T> T lookup(Collection<StartupEvent> events, Class<T> classToFind) {
final var found = events
.stream()
.filter(it -> Objects.equals(classToFind, it.getClass()))
.findFirst()
.orElse(null);
log.trace("found={}, classToFind={}", found, classToFind);
return (T) found;
}
}
2 changes: 2 additions & 0 deletions src/test/java/com/mageddo/dnsproxyserver/AppIntTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.qos.logback.classic.Level;
import com.mageddo.commons.concurrent.Threads;
import com.mageddo.dns.utils.Messages;
import com.mageddo.dnsproxyserver.config.application.Configs;
import com.mageddo.dnsproxyserver.server.Starter;
import com.mageddo.dnsproxyserver.solver.SimpleResolver;
import com.mageddo.dnsproxyserver.utils.Ips;
Expand All @@ -28,6 +29,7 @@ public class AppIntTest {
@BeforeEach
void beforeEach() {
Starter.setMustStartFlagActive(true);
Configs.clear();
LogbackUtils.changeLogLevel("com.mageddo", Level.TRACE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void mustBuildConfPathRelativeToWorkDir(@TempDir Path tmpDir){
ConfigDAOCmdArgs.setArgs(flags.getArgs());

// act
final var configPath = ConfigPathBuilder.buildConfigPath(workDir, flags.getConfigPath());
final var configPath = ConfigPathBuilder.build(workDir, flags.getConfigPath());

// assert
assertEquals("config.json", configPath.getFileName().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mageddo.dnsproxyserver.di.Context;
import com.mageddo.dnsproxyserver.di.StartupEvent;
import com.mageddo.dnsproxyserver.di.StartupEvents;
import dagger.sheath.junit.DaggerTest;
import org.junit.jupiter.api.Test;

Expand All @@ -22,7 +23,7 @@ void mustConfigureNetworkEventListener(){
// arrange

// act
final var found = StartupEvent.exists(this.events, EventListener.class);
final var found = StartupEvents.exists(this.events, EventListener.class);

// assert
assertTrue(found);
Expand Down

0 comments on commit 181f5bb

Please sign in to comment.