From 3309f18b5089a38f1791506952b126cc5f4386eb Mon Sep 17 00:00:00 2001 From: Kris De Volder Date: Tue, 16 Jan 2018 15:27:58 -0800 Subject: [PATCH] Fix more breakage caused by changes in boot 2.0 actuator data --- .../boot/app/cli/LiveConditionalParser.java | 73 ++++++------------- .../commons/boot/app/cli/SpringBootApp.java | 2 +- .../boot/app/cli/SpringBootAppCLI.java | 2 +- .../boot/app/cli/SpringBootAppTest.java | 8 +- 4 files changed, 30 insertions(+), 55 deletions(-) diff --git a/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/LiveConditionalParser.java b/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/LiveConditionalParser.java index af45b2956..c0ab45c45 100644 --- a/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/LiveConditionalParser.java +++ b/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/LiveConditionalParser.java @@ -11,7 +11,6 @@ package org.springframework.ide.vscode.commons.boot.app.cli; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.Optional; @@ -43,15 +42,20 @@ public class LiveConditionalParser { } public Optional> parse() { - try { List allConditionals = new ArrayList<>(); - + JSONObject autoConfigReport = new JSONObject(autoConfigRecord); + if (autoConfigReport.has("contexts")) { + //more recently the report is nested inside the 'application' context. + autoConfigReport = autoConfigReport.getJSONObject("contexts").getJSONObject("application"); + } if (StringUtil.hasText(autoConfigRecord)) { - getConditionalsFromPositiveMatches(autoConfigRecord).stream() - .forEach(conditional -> allConditionals.add(conditional)); - getConditionalsFromNegativeMatches(autoConfigRecord).stream() - .forEach(conditional -> allConditionals.add(conditional)); + for (LiveConditional c : getConditionalsFromPositiveMatches(autoConfigReport)) { + allConditionals.add(c); + } + for (LiveConditional c : getConditionalsFromNegativeMatches(autoConfigReport)) { + allConditionals.add(c); + } } if (!allConditionals.isEmpty()) { return Optional.of(allConditionals); @@ -65,67 +69,38 @@ public class LiveConditionalParser { /** * Fetches the "positiveMatches" element in the autoconfig report JSON that contains conditional information. */ - private Optional getPositiveMatchesJson(String autoConfigReport) { - JSONObject autoConfigJson = new JSONObject(autoConfigReport); - - Iterator keys = autoConfigJson.keys(); - - while (keys.hasNext()) { - String key = keys.next(); - if ("positiveMatches".equals(key)) { - Object obj = autoConfigJson.get(key); - if (obj instanceof JSONObject) { - return Optional.of((JSONObject) obj); - } - } - } - - return Optional.empty(); + private Optional getPositiveMatchesJson(JSONObject autoConfigReport) { + return Optional.ofNullable(autoConfigReport.optJSONObject("positiveMatches")); } /** * Fetches the "negativeMatches" element in the autoconfig report JSON that contains conditional information. */ - private Optional getNegativeMatchesJson(String autoConfigReport) { - JSONObject autoConfigJson = new JSONObject(autoConfigReport); - - Iterator keys = autoConfigJson.keys(); - - while (keys.hasNext()) { - String key = keys.next(); - if ("negativeMatches".equals(key)) { - Object obj = autoConfigJson.get(key); - if (obj instanceof JSONObject) { - return Optional.of((JSONObject) obj); - } - } - } - - return Optional.empty(); + private Optional getNegativeMatchesJson(JSONObject autoConfigReport) { + return Optional.ofNullable(autoConfigReport.optJSONObject("negativeMatches")); } /** * Fetches all the conditionals listed in the the "positiveMatches" element in the autoconfig report. * */ - private List getConditionalsFromPositiveMatches(String autoConfigReport) { + private List getConditionalsFromPositiveMatches(JSONObject autoConfigReport) { List conditions = new ArrayList<>(); getPositiveMatchesJson(autoConfigReport).ifPresent((matches) -> { - matches.keySet().stream().forEach(typeInfo -> { + for (String typeInfo : matches.keySet()) { // The positive match key contains the bean method information where conditional // was applied to Object val = matches.get(typeInfo); - if (val instanceof JSONArray) { + if (val instanceof JSONArray) { JSONArray contentList = (JSONArray) val; parseConditionalsFromContentList(conditions, typeInfo, contentList); } - }); - + } }); return conditions; } - private List getConditionalsFromNegativeMatches(String autoConfigReport) { + private List getConditionalsFromNegativeMatches(JSONObject autoConfigReport) { List conditions = new ArrayList<>(); // The JSON structure being parsed is: // "negativeMatches": { @@ -140,7 +115,7 @@ public class LiveConditionalParser { // } getNegativeMatchesJson(autoConfigReport).ifPresent((matches) -> { // The key in the "matches" JSON contains the live type information where the conditional was applied to - matches.keySet().stream().forEach(typeInfo -> { + for (String typeInfo : matches.keySet()) { // The positive match key contains the bean method information where conditional // was applied to Object val = matches.get(typeInfo); @@ -151,15 +126,15 @@ public class LiveConditionalParser { parseConditionalsFromContentList(conditions, typeInfo, contentList); }); } - }); + } }); return conditions; } private void parseConditionalsFromContentList(List conditionals, String typeInfo, JSONArray contentList) { - contentList.forEach((content) -> { + for (Object content : contentList) { if (content instanceof JSONObject) { JSONObject conditionalJson = (JSONObject) content; String condition = (String) conditionalJson.get("condition"); @@ -173,7 +148,7 @@ public class LiveConditionalParser { conditionals.add(conditional); } } - }); + } } diff --git a/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootApp.java b/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootApp.java index 0cd94c956..5ee7e4490 100644 --- a/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootApp.java +++ b/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootApp.java @@ -241,7 +241,7 @@ public class SpringBootApp { return LiveConditionalParser.parse(autoConfigReport, processId, processName); } - public String getAutoConfigReport() throws Exception { + private String getAutoConfigReport() throws Exception { //Boot 1.x Object result = getActuatorDataFromAttribute("org.springframework.boot:type=Endpoint,name=autoConfigurationReportEndpoint", "Data"); if (result != null) { diff --git a/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootAppCLI.java b/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootAppCLI.java index 71b4704c5..acfaebd27 100644 --- a/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootAppCLI.java +++ b/headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootAppCLI.java @@ -32,7 +32,7 @@ public class SpringBootAppCLI { System.out.println("Port: " + app.getPort()); System.out.println("Beans: " + app.getBeans()); System.out.println("Mappings: " + app.getRequestMappings()); - System.out.println("ConfigReport: " + app.getAutoConfigReport()); + System.out.println("ConfigReport: " + app.getLiveConditionals()); System.out.println(); } diff --git a/headless-services/commons/commons-boot-app-cli/src/test/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootAppTest.java b/headless-services/commons/commons-boot-app-cli/src/test/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootAppTest.java index 44c3b817b..affa11b76 100644 --- a/headless-services/commons/commons-boot-app-cli/src/test/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootAppTest.java +++ b/headless-services/commons/commons-boot-app-cli/src/test/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootAppTest.java @@ -190,13 +190,13 @@ public class SpringBootAppTest { } @Test - public void getAutoConfigReport() throws Exception { + public void getLiveConditionals() throws Exception { for (SpringBootApp testApp : getTestApps()) { try { ACondition.waitFor(TIMEOUT, () -> { - String result = testApp.getAutoConfigReport(); - assertNonEmptyJsonObject(result); - // System.out.println("autoconfreport = "+result); + Optional> result = testApp.getLiveConditionals(); + assertTrue(result.isPresent()); + assertFalse(result.get().isEmpty()); }); } catch (Exception e) { throw new RuntimeException("Failed for: "+testApp, e);