Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify main jar holder #2

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import io.opentelemetry.semconv.ResourceAttributes;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.logging.Logger;

/**
Expand All @@ -26,21 +28,26 @@ public final class JarServiceNameDetector implements ConditionalResourceProvider

private static final Logger logger = Logger.getLogger(JarServiceNameDetector.class.getName());

private final MainJarPathFinder jarPathFinder;
private final Supplier<Optional<Path>> jarPathSupplier;

@SuppressWarnings("unused") // SPI
public JarServiceNameDetector() {
this(new MainJarPathFinder());
this(MainJarPathHolder::getJarPath);
}

private JarServiceNameDetector(Supplier<Optional<Path>> jarPathSupplier) {
this.jarPathSupplier = jarPathSupplier;
}

// visible for tests
JarServiceNameDetector(MainJarPathFinder jarPathFinder) {
this.jarPathFinder = jarPathFinder;
this(() -> Optional.ofNullable(jarPathFinder.detectJarPath()));
}

@Override
public Resource createResource(ConfigProperties config) {
return MainJarPathHolder.getJarPath(jarPathFinder)
return jarPathSupplier
.get()
.map(
jarPath -> {
String serviceName = getServiceName(jarPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,13 @@
import java.nio.file.Path;
import java.util.Optional;

public class MainJarPathHolder {
class MainJarPathHolder {
private static final Optional<Path> jarPath =
Optional.ofNullable(new MainJarPathFinder().detectJarPath());

private MainJarPathHolder() {}

// visible for testing
static void resetForTest() {
detectionResult = Optional.empty();
}

private static class DetectionResult {
private final Optional<Path> jarPath;

private DetectionResult(Optional<Path> jarPath) {
this.jarPath = jarPath;
}
static Optional<Path> getJarPath() {
return jarPath;
}

private static Optional<DetectionResult> detectionResult = Optional.empty();

static Optional<Path> getJarPath(MainJarPathFinder finder) {
if (!detectionResult.isPresent()) {
detectionResult =
Optional.of(new DetectionResult(Optional.ofNullable(finder.detectJarPath())));
}
return detectionResult.get().jarPath;
}
private MainJarPathHolder() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.logging.Logger;
Expand All @@ -29,17 +30,16 @@ public final class ManifestResourceProvider extends AttributeResourceProvider<Ma

@SuppressWarnings("unused") // SPI
public ManifestResourceProvider() {
this(new MainJarPathFinder(), ManifestResourceProvider::readManifest);
this(MainJarPathHolder::getJarPath, ManifestResourceProvider::readManifest);
}

// Visible for testing
ManifestResourceProvider(
MainJarPathFinder jarPathFinder, Function<Path, Optional<Manifest>> manifestReader) {
private ManifestResourceProvider(
Supplier<Optional<Path>> jarPathSupplier, Function<Path, Optional<Manifest>> manifestReader) {
super(
new AttributeProvider<Manifest>() {
@Override
public Optional<Manifest> readData() {
return MainJarPathHolder.getJarPath(jarPathFinder).flatMap(manifestReader);
return jarPathSupplier.get().flatMap(manifestReader);
}

@Override
Expand All @@ -63,6 +63,12 @@ public void registerAttributes(Builder<Manifest> builder) {
});
}

// Visible for testing
ManifestResourceProvider(
MainJarPathFinder jarPathFinder, Function<Path, Optional<Manifest>> manifestReader) {
this(() -> Optional.ofNullable(jarPathFinder.detectJarPath()), manifestReader);
}

private static Optional<Manifest> readManifest(Path jarPath) {
try (JarFile jarFile = new JarFile(jarPath.toFile(), false)) {
return Optional.of(jarFile.getManifest());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand All @@ -33,11 +32,6 @@ class JarServiceNameDetectorTest {

@Mock ConfigProperties config;

@BeforeEach
void setUp() {
MainJarPathHolder.resetForTest();
}

@Test
void createResource_empty() {
String[] processArgs = new String[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,11 @@
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;

class ManifestResourceProviderTest {

@BeforeEach
void setUp() {
MainJarPathHolder.resetForTest();
}

private static class TestCase {
private final String name;
private final String expectedName;
Expand Down
Loading