Respect actuator on classpath. Launch config attr for auto-connect

This commit is contained in:
aboyko
2022-09-13 10:59:56 -04:00
parent 1012074a66
commit 274739ba1f
7 changed files with 60 additions and 76 deletions

View File

@@ -327,11 +327,5 @@
class="org.springframework.ide.eclipse.boot.dash.remoteapps.RemoteAppsFromBootDash">
</injection>
</extension>
<extension
point="org.eclipse.core.runtime.preferences">
<initializer
class="org.springframework.ide.eclipse.boot.dash.prefs.PrefsInitializer">
</initializer>
</extension>
</plugin>

View File

@@ -11,6 +11,7 @@
package org.springframework.ide.eclipse.boot.dash;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -21,13 +22,14 @@ import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.springframework.ide.eclipse.boot.core.BootPropertyTester;
import org.springframework.ide.eclipse.boot.dash.di.SimpleDIContext;
import org.springframework.ide.eclipse.boot.dash.liveprocess.CommandInfo;
import org.springframework.ide.eclipse.boot.dash.liveprocess.LiveProcessCommandsExecutor;
@@ -104,7 +106,7 @@ public class BootDashActivator extends AbstractUIPlugin {
super.start(context);
plugin = this;
new M2ELogbackCustomizer().schedule();
connectToLaunchedBootApps();
listenToLaunchedBootApps();
}
/*
@@ -114,6 +116,7 @@ public class BootDashActivator extends AbstractUIPlugin {
* BundleContext)
*/
public void stop(BundleContext context) throws Exception {
stopListeningToLaunchedBootApps();
plugin = null;
super.stop(context);
if (model!=null) {
@@ -195,60 +198,56 @@ public class BootDashActivator extends AbstractUIPlugin {
@Override
public void stateChanged(ILaunchConfiguration owner) {
if (BootLaunchConfigurationDelegate.getEnableJmx(owner)) {
LocalBootDashModel localModel = (LocalBootDashModel) getModel().getSectionByTargetId(RunTargets.LOCAL.getId());
LaunchConfRunStateTracker tracker = localModel.getLaunchConfRunStateTracker();
RunState state = tracker.getState(owner);
if (state == RunState.RUNNING || state == RunState.DEBUGGING) {
for (ILaunch l : DebugPlugin.getDefault().getLaunchManager().getLaunches()) {
if (l.getLaunchConfiguration() == owner) {
for (IProcess p : l.getProcesses()) {
String pid = p.getAttribute(IProcess.ATTR_PROCESS_ID);
if (pid != null) {
List<Server> servers = LiveProcessCommandsExecutor.getDefault()
.getLanguageServers();
if (BootLaunchConfigurationDelegate.getEnableJmx(owner) && BootLaunchConfigurationDelegate.getAutoConnect(owner)) {
IJavaProject project = JavaCore.create(BootLaunchConfigurationDelegate.getProject(owner));
try {
if (project != null && Arrays.stream(project.getResolvedClasspath(true)).anyMatch(BootPropertyTester::isActuatorJar)) {
LocalBootDashModel localModel = (LocalBootDashModel) getModel().getSectionByTargetId(RunTargets.LOCAL.getId());
LaunchConfRunStateTracker tracker = localModel.getLaunchConfRunStateTracker();
RunState state = tracker.getState(owner);
if (state == RunState.RUNNING || state == RunState.DEBUGGING) {
for (ILaunch l : DebugPlugin.getDefault().getLaunchManager().getLaunches()) {
if (l.getLaunchConfiguration() == owner) {
for (IProcess p : l.getProcesses()) {
String pid = p.getAttribute(IProcess.ATTR_PROCESS_ID);
if (pid != null) {
List<Server> servers = LiveProcessCommandsExecutor.getDefault()
.getLanguageServers();
CommandInfo cmd = new CommandInfo("sts/livedata/connect",
Map.of("processKey", pid));
CommandInfo cmd = new CommandInfo("sts/livedata/connect",
Map.of("processKey", pid));
// The delay seems to be necessary for the moment. Although the process is created VM attach API may not work at early stages of the process run.
// "VirtualMachine.list()" call may not list the newly created process which means the process is gone and triggers disconnect.
// If lifecycle management is enabled the ready state seem to be a great indicator of a boot process fully started.
// TODO: explore health endpoint perhaps instead of ready state under Admin endpoint.
Flux.fromIterable(servers).flatMap(s -> Mono.delay(Duration.ofMillis(500)).then(s.executeCommand(cmd))).subscribe();
// The delay seems to be necessary for the moment. Although the process is created VM attach API may not work at early stages of the process run.
// "VirtualMachine.list()" call may not list the newly created process which means the process is gone and triggers disconnect.
// If lifecycle management is enabled the ready state seem to be a great indicator of a boot process fully started.
// TODO: explore health endpoint perhaps instead of ready state under Admin endpoint.
Flux.fromIterable(servers).flatMap(s -> Mono.delay(Duration.ofMillis(500)).then(s.executeCommand(cmd))).subscribe();
}
}
}
}
}
}
}
}
} catch (Exception e) {
getLog().error("Failed to connect to Boot app", e);
}
}
}
};
private void connectToLaunchedBootApps() {
updateRunStateListening();
getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (PREF_LIVE_INFORMATION_AUTO_CONNECT.equals(event.getProperty())) {
updateRunStateListening();
}
}
});
private void listenToLaunchedBootApps() {
LocalBootDashModel localModel = (LocalBootDashModel) getModel().getSectionByTargetId(RunTargets.LOCAL.getId());
LaunchConfRunStateTracker tracker = localModel.getLaunchConfRunStateTracker();
tracker.addListener(RUN_STATE_LISTENER);
}
private void updateRunStateListening() {
private void stopListeningToLaunchedBootApps() {
LocalBootDashModel localModel = (LocalBootDashModel) getModel().getSectionByTargetId(RunTargets.LOCAL.getId());
LaunchConfRunStateTracker tracker = localModel.getLaunchConfRunStateTracker();
if (getPreferenceStore().getBoolean(PREF_LIVE_INFORMATION_AUTO_CONNECT)) {
tracker.addListener(RUN_STATE_LISTENER);
} else {
tracker.removeListener(RUN_STATE_LISTENER);
}
tracker.removeListener(RUN_STATE_LISTENER);
}
}

View File

@@ -36,7 +36,6 @@ public class BootDashPrefsPage extends FieldEditorPreferencePage implements IWor
@Override
protected void createFieldEditors() {
Composite parent = getFieldEditorParent();
addField(new BooleanFieldEditor(BootDashActivator.PREF_LIVE_INFORMATION_AUTO_CONNECT, "Auto Connect to launched Boot app to show Live Information", parent));
for (RunTargetType rtt : BootDashActivator.getDefault().getModel().getRunTargetTypes()) {
if (rtt.supportsDeletion()) {
addField(new BooleanFieldEditor(DeleteElementsAction.PREF_SKIP_CONFIRM_DELETE(rtt), "Skip Delete Element Confirmation ("+rtt.getName()+")", parent));

View File

@@ -1,28 +0,0 @@
/*******************************************************************************
* Copyright (c) 2022 VMware, 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
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* VMware, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.eclipse.boot.dash.prefs;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;
import org.springframework.ide.eclipse.boot.dash.BootDashActivator;
public class PrefsInitializer extends AbstractPreferenceInitializer {
public PrefsInitializer() {
}
@Override
public void initializeDefaultPreferences() {
IPreferenceStore preferenceStore = BootDashActivator.getDefault().getPreferenceStore();
preferenceStore.setDefault(BootDashActivator.PREF_LIVE_INFORMATION_AUTO_CONNECT, true);
}
}