Skip to content

Commit

Permalink
GH-1410: adopt new jmx method from boot 3 apps to fetch the condition…
Browse files Browse the repository at this point in the history
… report and deal correctly with conditions embedded in app node
  • Loading branch information
martinlippert committed Nov 17, 2024
1 parent 48fc472 commit b4292f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* Copyright (c) 2017, 2024 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,6 +11,7 @@
package org.springframework.ide.vscode.boot.java.livehover.v2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -50,12 +51,19 @@ public LiveConditional[] parse() {
if (StringUtil.hasText(autoConfigRecord)) {
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");
//more recently the report is nested inside an 'application' context, but the name of the node depends on the app

JSONObject contexts = autoConfigReport.getJSONObject("contexts");
Iterator<String> keys = contexts.keys();
if (keys.hasNext()) {
autoConfigReport = contexts.getJSONObject(keys.next());
}
}

for (LiveConditional c : getConditionalsFromPositiveMatches(autoConfigReport)) {
allConditionals.add(c);
}

for (LiveConditional c : getConditionalsFromNegativeMatches(autoConfigReport)) {
allConditionals.add(c);
}
Expand Down Expand Up @@ -135,8 +143,7 @@ private List<LiveConditional> getConditionalsFromNegativeMatches(JSONObject auto
return conditions;
}

private void parseConditionalsFromContentList(List<LiveConditional> conditionals, String typeInfo,
JSONArray contentList) {
private void parseConditionalsFromContentList(List<LiveConditional> conditionals, String typeInfo, JSONArray contentList) {
for (Object content : contentList) {
if (content instanceof JSONObject) {
JSONObject conditionalJson = (JSONObject) content;
Expand All @@ -154,9 +161,7 @@ private void parseConditionalsFromContentList(List<LiveConditional> conditionals
}
}


public static LiveConditional[] parse(String autoConfigRecord, String appProcessId,
String appProcessName) {
public static LiveConditional[] parse(String autoConfigRecord, String appProcessId, String appProcessName) {
return new LiveConditionalParser(autoConfigRecord, appProcessId, appProcessName).parse();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2020 Pivotal, Inc.
* Copyright (c) 2019, 2024 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -29,6 +29,7 @@
import javax.management.ObjectName;
import javax.management.Query;
import javax.management.QueryExp;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;

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

public LiveConditional[] getConditionals(MBeanServerConnection connection, String domain, String processId, String processName) {
try {
//Boot 3.x
Object result = getActuatorDataFromOperation(connection, getObjectName(domain, "type=Endpoint,name=Conditions"), "conditions");
if (result != null) {
String report = gson.toJson(result);
return LiveConditionalParser.parse(report, processId, processName);
}

//Boot 2.x
Object result = getActuatorDataFromOperation(connection, getObjectName(domain, "type=Endpoint,name=Conditions"), "applicationConditionEvaluation");
result = getActuatorDataFromOperation(connection, getObjectName(domain, "type=Endpoint,name=Conditions"), "applicationConditionEvaluation");
if (result != null) {
String report = gson.toJson(result);
return LiveConditionalParser.parse(report, processId, processName);
Expand All @@ -621,7 +629,7 @@ public LiveConditional[] getConditionals(MBeanServerConnection connection, Strin
} catch (IOException e) {
//ignore. Happens a lot when apps are stopped while we try to talk to them.
} catch (Exception e) {
// TODO Auto-generated catch block
// ignore, might happen when communication with the running app is difficult
}

return null;
Expand Down Expand Up @@ -655,7 +663,6 @@ public String[] getActiveProfiles(MBeanServerConnection connection, String envir
}

public LiveProperties getProperties(MBeanServerConnection connection, String environment) throws Exception {

try {
if (environment != null) {
return LivePropertiesJsonParser.parseProperties(environment);
Expand All @@ -666,21 +673,20 @@ public LiveProperties getProperties(MBeanServerConnection connection, String env
return null;
}



public String getEnvironment(MBeanServerConnection connection, String domain) throws Exception {
try {
Object result = getActuatorDataFromAttribute(connection, getObjectName(domain, "type=Endpoint,name=environmentEndpoint"), "Data");
Object result = getActuatorDataFromOperation(connection, getObjectName(domain, "type=Endpoint,name=Env"), "environment");
if (result != null) {
String environment = gson.toJson(result);
return environment;
}

result = getActuatorDataFromOperation(connection, getObjectName(domain, "type=Endpoint,name=Env"), "environment");
result = getActuatorDataFromAttribute(connection, getObjectName(domain, "type=Endpoint,name=environmentEndpoint"), "Data");
if (result != null) {
String environment = gson.toJson(result);
return environment;
}

} catch (IOException e) {
//ignore... probably just because app is stopped
} catch (ExecutionException e) {
Expand All @@ -700,6 +706,9 @@ private Object getActuatorDataFromAttribute(MBeanServerConnection connection, Ob
catch (InstanceNotFoundException|IOException e) {
return null;
}
catch (ReflectionException e) {
return null;
}
}
return null;
}
Expand All @@ -712,6 +721,9 @@ private Object getActuatorDataFromOperation(MBeanServerConnection connection, Ob
catch (InstanceNotFoundException|IOException e) {
return null;
}
catch (ReflectionException e) {
return null;
}
}
return null;
}
Expand All @@ -724,6 +736,9 @@ private Object getActuatorDataFromOperation(MBeanServerConnection connection, Ob
catch (InstanceNotFoundException|IOException e) {
return null;
}
catch (ReflectionException e) {
return null;
}
}
return null;
}
Expand Down

0 comments on commit b4292f6

Please sign in to comment.