Skip to content

Commit

Permalink
add logic to parse multiple yaml defs and read from boot inf
Browse files Browse the repository at this point in the history
  • Loading branch information
adamleantech committed Mar 9, 2023
1 parent 3cc0ab8 commit cd32a9b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public Resource createResource(ConfigProperties config) {
this::findByCurrentDirectoryApplicationProperties,
this::findByCurrentDirectoryApplicationYaml,
this::findByClasspathApplicationProperties,
this::findByClasspathApplicationYaml);
this::findByClasspathApplicationYaml,
this::findByBootInfApplicationYml);
return finders
.map(Supplier::get)
.filter(Objects::nonNull)
Expand Down Expand Up @@ -160,6 +161,14 @@ private String findByClasspathApplicationYaml() {
return result;
}

@Nullable
private String findByBootInfApplicationYml() {
String result =
loadFromBootInf("application.yml", SpringBootServiceNameDetector::parseNameFromYaml);
logger.log(Level.FINER, "Checking application.yml in classpath: {0}", result);
return result;
}

@Nullable
private String findByCurrentDirectoryApplicationYaml() {
String result = null;
Expand All @@ -178,14 +187,16 @@ private static String parseNameFromYaml(InputStream in) {
try {
LoadSettings settings = LoadSettings.builder().build();
Load yaml = new Load(settings);
Map<String, Object> data = (Map<String, Object>) yaml.loadFromInputStream(in);
Map<String, Map<String, Object>> spring =
(Map<String, Map<String, Object>>) data.get("spring");
if (spring != null) {
Map<String, Object> app = spring.get("application");
if (app != null) {
Object name = app.get("name");
return (String) name;
for (Object o : yaml.loadAllFromInputStream(in)) {
Map<String, Object> data = (Map<String, Object>) o;
Map<String, Map<String, Object>> spring =
(Map<String, Map<String, Object>>) data.get("spring");
if (spring != null) {
Map<String, Object> app = spring.get("application");
if (app != null) {
Object name = app.get("name");
return (String) name;
}
}
}
} catch (RuntimeException e) {
Expand Down Expand Up @@ -263,6 +274,15 @@ private String loadFromClasspath(String filename, Function<InputStream, String>
}
}

@Nullable
private String loadFromBootInf(String filename, Function<InputStream, String> parser) {
try (InputStream in = system.openBootInfClassesResource(filename)) {
return parser.apply(in);
} catch (Exception e) {
return null;
}
}

// Exists for testing
static class SystemHelper {

Expand All @@ -278,6 +298,12 @@ InputStream openClasspathResource(String filename) {
return ClassLoader.getSystemClassLoader().getResourceAsStream(filename);
}

InputStream openBootInfClassesResource(String filename) {
return Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream(Paths.get("BOOT-INF", "classes", filename).toString());
}

InputStream openFile(String filename) throws Exception {
return Files.newInputStream(Paths.get(filename));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ void classpathApplicationYaml() {
expectServiceName(result, "cat-store");
}

@Test
void classpathApplicationYamlContainingMultipleYamlDefinitions() {
when(system.openClasspathResource(APPLICATION_YML))
.thenReturn(
ClassLoader.getSystemClassLoader().getResourceAsStream("application-multi.yml"));
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "cat-store");
}

@Test
void bootInfApplicationYaml() {
when(system.openBootInfClassesResource(APPLICATION_YML)).thenCallRealMethod();
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "cat-store");
}

@Test
void yamlFileInCurrentDir() throws Exception {
Path yamlPath = Paths.get(APPLICATION_YML);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
flib:
something:
12

section:
two: 2

server:
port: 777
context-path: /meow

spring:
application:
name: cat-store
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
flib:
something:
12

section:
two: 2

server:
port: 777
context-path: /meow

spring:
application:
name: cat-store

---
some:
other:
property: value

0 comments on commit cd32a9b

Please sign in to comment.