Skip to content

Commit b4292f6

Browse files
committed
GH-1410: adopt new jmx method from boot 3 apps to fetch the condition report and deal correctly with conditions embedded in app node
1 parent 48fc472 commit b4292f6

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/livehover/v2/LiveConditionalParser.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017 Pivotal, Inc.
2+
* Copyright (c) 2017, 2024 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -11,6 +11,7 @@
1111
package org.springframework.ide.vscode.boot.java.livehover.v2;
1212

1313
import java.util.ArrayList;
14+
import java.util.Iterator;
1415
import java.util.List;
1516
import java.util.Optional;
1617

@@ -50,12 +51,19 @@ public LiveConditional[] parse() {
5051
if (StringUtil.hasText(autoConfigRecord)) {
5152
JSONObject autoConfigReport = new JSONObject(autoConfigRecord);
5253
if (autoConfigReport.has("contexts")) {
53-
//more recently the report is nested inside the 'application' context.
54-
autoConfigReport = autoConfigReport.getJSONObject("contexts").getJSONObject("application");
54+
//more recently the report is nested inside an 'application' context, but the name of the node depends on the app
55+
56+
JSONObject contexts = autoConfigReport.getJSONObject("contexts");
57+
Iterator<String> keys = contexts.keys();
58+
if (keys.hasNext()) {
59+
autoConfigReport = contexts.getJSONObject(keys.next());
60+
}
5561
}
62+
5663
for (LiveConditional c : getConditionalsFromPositiveMatches(autoConfigReport)) {
5764
allConditionals.add(c);
5865
}
66+
5967
for (LiveConditional c : getConditionalsFromNegativeMatches(autoConfigReport)) {
6068
allConditionals.add(c);
6169
}
@@ -135,8 +143,7 @@ private List<LiveConditional> getConditionalsFromNegativeMatches(JSONObject auto
135143
return conditions;
136144
}
137145

138-
private void parseConditionalsFromContentList(List<LiveConditional> conditionals, String typeInfo,
139-
JSONArray contentList) {
146+
private void parseConditionalsFromContentList(List<LiveConditional> conditionals, String typeInfo, JSONArray contentList) {
140147
for (Object content : contentList) {
141148
if (content instanceof JSONObject) {
142149
JSONObject conditionalJson = (JSONObject) content;
@@ -154,9 +161,7 @@ private void parseConditionalsFromContentList(List<LiveConditional> conditionals
154161
}
155162
}
156163

157-
158-
public static LiveConditional[] parse(String autoConfigRecord, String appProcessId,
159-
String appProcessName) {
164+
public static LiveConditional[] parse(String autoConfigRecord, String appProcessId, String appProcessName) {
160165
return new LiveConditionalParser(autoConfigRecord, appProcessId, appProcessName).parse();
161166
}
162167
}

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/livehover/v2/SpringProcessLiveDataExtractorOverJMX.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019, 2020 Pivotal, Inc.
2+
* Copyright (c) 2019, 2024 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -29,6 +29,7 @@
2929
import javax.management.ObjectName;
3030
import javax.management.Query;
3131
import javax.management.QueryExp;
32+
import javax.management.ReflectionException;
3233
import javax.management.remote.JMXConnector;
3334

3435
import org.json.JSONArray;
@@ -604,8 +605,15 @@ private LiveRequestMapping[] parseRequestMappingsJson(String json, String bootVe
604605

605606
public LiveConditional[] getConditionals(MBeanServerConnection connection, String domain, String processId, String processName) {
606607
try {
608+
//Boot 3.x
609+
Object result = getActuatorDataFromOperation(connection, getObjectName(domain, "type=Endpoint,name=Conditions"), "conditions");
610+
if (result != null) {
611+
String report = gson.toJson(result);
612+
return LiveConditionalParser.parse(report, processId, processName);
613+
}
614+
607615
//Boot 2.x
608-
Object result = getActuatorDataFromOperation(connection, getObjectName(domain, "type=Endpoint,name=Conditions"), "applicationConditionEvaluation");
616+
result = getActuatorDataFromOperation(connection, getObjectName(domain, "type=Endpoint,name=Conditions"), "applicationConditionEvaluation");
609617
if (result != null) {
610618
String report = gson.toJson(result);
611619
return LiveConditionalParser.parse(report, processId, processName);
@@ -621,7 +629,7 @@ public LiveConditional[] getConditionals(MBeanServerConnection connection, Strin
621629
} catch (IOException e) {
622630
//ignore. Happens a lot when apps are stopped while we try to talk to them.
623631
} catch (Exception e) {
624-
// TODO Auto-generated catch block
632+
// ignore, might happen when communication with the running app is difficult
625633
}
626634

627635
return null;
@@ -655,7 +663,6 @@ public String[] getActiveProfiles(MBeanServerConnection connection, String envir
655663
}
656664

657665
public LiveProperties getProperties(MBeanServerConnection connection, String environment) throws Exception {
658-
659666
try {
660667
if (environment != null) {
661668
return LivePropertiesJsonParser.parseProperties(environment);
@@ -666,21 +673,20 @@ public LiveProperties getProperties(MBeanServerConnection connection, String env
666673
return null;
667674
}
668675

669-
670-
671676
public String getEnvironment(MBeanServerConnection connection, String domain) throws Exception {
672677
try {
673-
Object result = getActuatorDataFromAttribute(connection, getObjectName(domain, "type=Endpoint,name=environmentEndpoint"), "Data");
678+
Object result = getActuatorDataFromOperation(connection, getObjectName(domain, "type=Endpoint,name=Env"), "environment");
674679
if (result != null) {
675680
String environment = gson.toJson(result);
676681
return environment;
677682
}
678683

679-
result = getActuatorDataFromOperation(connection, getObjectName(domain, "type=Endpoint,name=Env"), "environment");
684+
result = getActuatorDataFromAttribute(connection, getObjectName(domain, "type=Endpoint,name=environmentEndpoint"), "Data");
680685
if (result != null) {
681686
String environment = gson.toJson(result);
682687
return environment;
683688
}
689+
684690
} catch (IOException e) {
685691
//ignore... probably just because app is stopped
686692
} catch (ExecutionException e) {
@@ -700,6 +706,9 @@ private Object getActuatorDataFromAttribute(MBeanServerConnection connection, Ob
700706
catch (InstanceNotFoundException|IOException e) {
701707
return null;
702708
}
709+
catch (ReflectionException e) {
710+
return null;
711+
}
703712
}
704713
return null;
705714
}
@@ -712,6 +721,9 @@ private Object getActuatorDataFromOperation(MBeanServerConnection connection, Ob
712721
catch (InstanceNotFoundException|IOException e) {
713722
return null;
714723
}
724+
catch (ReflectionException e) {
725+
return null;
726+
}
715727
}
716728
return null;
717729
}
@@ -724,6 +736,9 @@ private Object getActuatorDataFromOperation(MBeanServerConnection connection, Ob
724736
catch (InstanceNotFoundException|IOException e) {
725737
return null;
726738
}
739+
catch (ReflectionException e) {
740+
return null;
741+
}
727742
}
728743
return null;
729744
}

0 commit comments

Comments
 (0)