Skip to content

Commit

Permalink
joobyRun: improve lookup of logging configuration file on multi-modul…
Browse files Browse the repository at this point in the history
…e project fix #3428
  • Loading branch information
jknack committed May 20, 2024
1 parent 0b2abe1 commit 4800a5a
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 7 deletions.
21 changes: 18 additions & 3 deletions jooby/src/main/java/io/jooby/LoggingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import edu.umd.cs.findbugs.annotations.NonNull;

Expand Down Expand Up @@ -80,7 +82,13 @@ static String configure(@NonNull ClassLoader classLoader, @NonNull List<String>
// We could throw an exception here but nope, let user choose any other way of setup logging.
return null;
}
Path userdir = Paths.get(System.getProperty("user.dir"));
// Helps logging lookup on multi-module project, prefer logback from main module
Path userdir =
Stream.of(System.getProperty("jooby.dir"), System.getProperty("user.dir"))
.filter(Objects::nonNull)
.map(Paths::get)
.findFirst()
.orElseThrow((() -> new IllegalStateException("No base directory found")));

var loggingService = lookup.get();
var resources = logFiles(userdir, names, loggingService.getLogFileName());
Expand All @@ -95,8 +103,10 @@ static String configure(@NonNull ClassLoader classLoader, @NonNull List<String>
.filter(Path.class::isInstance)
.map(Path.class::cast)
.filter(Files::exists)
.findFirst()
.map(Path::toAbsolutePath);
.map(Path::toAbsolutePath)
// Skip build directories from maven/gradle
.filter(it -> !isBinary(it, "target") && !isBinary(it, "build"))
.findFirst();
if (logPath.isPresent()) {
System.setProperty(loggingService.getPropertyName(), logPath.get().toString());
return logPath.get().toString();
Expand Down Expand Up @@ -126,6 +136,11 @@ static String configure(@NonNull ClassLoader classLoader, @NonNull List<String>
return null;
}

static boolean isBinary(Path path, String segment) {
return StreamSupport.stream(path.spliterator(), false)
.anyMatch(it -> it.toString().equals(segment));
}

private static List<Object> logFiles(
Path basedir, List<String> profiles, List<String> logFileNames) {
for (String logFileName : logFileNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void run() throws Throwable {

JoobyRunOptions config = new JoobyRunOptions();
config.setUseSingleClassLoader(useSingleClassLoader = Boolean.TRUE);
config.setBasedir(current.getProjectDir().toPath());
config.setMainClass(mainClass);
config.setPort(port);
if (compileExtensions != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ protected void doExecute(List<MavenProject> projects, String mainClass) throws T

private JoobyRunOptions createOptions(String mainClass) {
JoobyRunOptions options = new JoobyRunOptions();
options.setBasedir(project.getBasedir().toPath());
options.setMainClass(mainClass);
if (compileExtensions != null) {
options.setCompileExtensions(compileExtensions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public void ensureConfigurationOptions() {
Stream.of(JoobyRunOptions.class.getDeclaredFields())
.filter(
field ->
!field.getName().equals("projectName") && !Modifier.isStatic(field.getModifiers()))
!field.getName().equals("projectName")
&& !field.getName().equals("basedir")
&& !Modifier.isStatic(field.getModifiers()))
.forEach(
field -> {
try {
Expand Down
33 changes: 30 additions & 3 deletions modules/jooby-run/src/main/java/io/jooby/run/JoobyRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Clock;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -89,9 +90,11 @@ public Exception start() {
ModuleClassLoader classLoader = module.getClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);

if (counter == 0) {
// main class must exists
module.getClassLoader().loadClass(conf.getMainClass());
// main class must exists
var mainClass = module.getClassLoader().loadClass(conf.getMainClass());
var projectdir = baseDir(conf.getBasedir(), mainClass);
if (projectdir != null) {
System.setProperty("jooby.dir", projectdir.toString());
}

System.setProperty("___jooby_run_hook__", SERVER_REF);
Expand Down Expand Up @@ -458,6 +461,30 @@ public void shutdown() {
}
}

static Path baseDir(Path root, Class clazz) {
var resource = clazz.getResource(".");
if (resource != null) {
if ("file".equals(resource.getProtocol())) {
var buildFiles = new String[] {"pom.xml", "build.gradle", "build.gradle.kts"};
var path = Paths.get(resource.getFile());
while (path.startsWith(root)) {

var buildFile =
Stream.of(buildFiles)
.map(path::resolve)
.filter(Files::exists)
.findFirst()
.orElse(null);
if (buildFile != null) {
return buildFile.getParent().toAbsolutePath();
}
path = path.getParent();
}
}
}
return null;
}

private DirectoryWatcher newWatcher() throws IOException {
List<Path> paths = new ArrayList<>(watchDirs.size());
paths.addAll(watchDirs.keySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class JoobyRunOptions {
private List<String> compileExtensions = Arrays.asList("java", "kt");

private Integer port = null;
private Path basedir;

private Long waitTimeBeforeRestart = DEFAULT_WAIT_TIME_BEFORE_RESTART;

Expand Down Expand Up @@ -180,6 +181,14 @@ public boolean isRestartExtension(Path path) {
return containsExtension(restartExtensions, path);
}

public Path getBasedir() {
return basedir;
}

public void setBasedir(Path basedir) {
this.basedir = basedir;
}

private boolean containsExtension(List<String> extensions, Path path) {
String filename = path.getFileName().toString();
return extensions.stream().anyMatch(ext -> filename.endsWith("." + ext));
Expand Down
21 changes: 21 additions & 0 deletions modules/jooby-run/src/test/java/io/jooby/run/BaseDirTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.run;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

public class BaseDirTest {
@Test
public void findBaseDir() {
var basedir = Paths.get(System.getProperty("user.dir"));
var path = JoobyRun.baseDir(basedir, JoobyRun.class);
assertEquals(basedir, path);
}
}

0 comments on commit 4800a5a

Please sign in to comment.