From 83d7ddecd723ad0f417910cf220f708357efeb07 Mon Sep 17 00:00:00 2001 From: Kris De Volder Date: Tue, 4 Jun 2019 12:27:04 -0700 Subject: [PATCH] Add Remote apps prefs page This was moved from boot dash. See: https://www.pivotaltracker.com/story/show/163360865 --- .../META-INF/MANIFEST.MF | 3 +- .../plugin.xml | 8 +- .../boot/ls/BootLanguageServerPlugin.java | 8 +- .../DelegatingStreamConnectionProvider.java | 34 ++++++- .../boot/ls/prefs/RemoteAppsPrefs.java | 99 +++++++++++++++++++ .../boot/ls/prefs/RemoteAppsPrefsPage.java | 43 ++++++++ .../boot/ls/prefs/RemoteAppsSection.java | 70 +++++++++++++ .../.classpath | 4 +- 8 files changed, 259 insertions(+), 10 deletions(-) create mode 100644 eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsPrefs.java create mode 100644 eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsPrefsPage.java create mode 100644 eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsSection.java diff --git a/eclipse-language-servers/org.springframework.tooling.boot.ls/META-INF/MANIFEST.MF b/eclipse-language-servers/org.springframework.tooling.boot.ls/META-INF/MANIFEST.MF index f540ab4e2..797ce257c 100644 --- a/eclipse-language-servers/org.springframework.tooling.boot.ls/META-INF/MANIFEST.MF +++ b/eclipse-language-servers/org.springframework.tooling.boot.ls/META-INF/MANIFEST.MF @@ -26,7 +26,8 @@ Require-Bundle: org.eclipse.jdt.launching;bundle-version="3.9.0", org.eclipse.ui.editors;bundle-version="3.11.100", org.springsource.ide.eclipse.commons.livexp, org.apache.commons.lang3, - org.eclipse.wst.sse.ui;bundle-version="1.5.0";resolution:=optional + org.eclipse.wst.sse.ui;bundle-version="1.5.0";resolution:=optional, + org.json;bundle-version="1.0.0" Import-Package: com.google.gson;version="2.7.0", org.eclipse.jface.preference, org.osgi.framework diff --git a/eclipse-language-servers/org.springframework.tooling.boot.ls/plugin.xml b/eclipse-language-servers/org.springframework.tooling.boot.ls/plugin.xml index 02e917d2a..23360eb59 100644 --- a/eclipse-language-servers/org.springframework.tooling.boot.ls/plugin.xml +++ b/eclipse-language-servers/org.springframework.tooling.boot.ls/plugin.xml @@ -105,9 +105,15 @@ contentTypeId="org.eclipse.jdt.core.javaSource"> - + + + getAllRemoteApps() { + ImmutableSet fromBootDash = BootLanguageServerPlugin.getRemoteBootApps().getValues(); + List> fromUserPrefs = new RemoteAppsPrefs().getRemoteAppData(); + + Set combined = new LinkedHashSet<>(); + combined.addAll(fromBootDash); + combined.addAll(fromUserPrefs); + return combined + .stream() + .map(this::parseData) + .collect(Collectors.toList()); + } @SuppressWarnings("unchecked") private RemoteBootAppData parseData(Object incomingData) { diff --git a/eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsPrefs.java b/eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsPrefs.java new file mode 100644 index 000000000..b2bf5215e --- /dev/null +++ b/eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsPrefs.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2018 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 + * https://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Pivotal, Inc. - initial API and implementation + *******************************************************************************/ + +package org.springframework.tooling.boot.ls.prefs; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.springframework.tooling.boot.ls.BootLanguageServerPlugin; +import org.springsource.ide.eclipse.commons.livexp.ui.Disposable; +import org.springsource.ide.eclipse.commons.livexp.util.ExceptionUtil; +import org.springsource.ide.eclipse.commons.livexp.util.Log; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; + +public class RemoteAppsPrefs { + + public static final String REMOTE_APPS_KEY = "remote-apps"; + + private IEclipsePreferences prefs = BootLanguageServerPlugin.getPreferences(); + + public void setRawJson(String json) { + prefs.put(REMOTE_APPS_KEY, json); + } + + public String getRawJson() { + return prefs.get(REMOTE_APPS_KEY, ""); + } + + public List> getRemoteAppData() { + String json = getRawJson(); + try { + return parse(json); + } catch (Exception e) { + Log.warn("Problem parsing manually configured boot remote apps data: "+ExceptionUtil.getMessage(e)); + return ImmutableList.of(); + } + } + + public static List> parse(String json) throws JSONException { + Builder> buider = ImmutableList.builder(); + if (!json.trim().equals("")) { + JSONArray remoteApps = new JSONArray(json); + for (int i = 0; i < remoteApps.length(); i++) { + JSONObject app = remoteApps.getJSONObject(i); + String host = app.getString("host"); + String jmxUrl = app.getString("jmxurl"); + String port = app.optString("port"); + String urlScheme = app.optString("urlScheme"); + if (host!=null && jmxUrl!=null) { + //Not using ImmutableList because it doesn't allow null values in elements. + ArrayList remoteApp = new ArrayList<>(4); + remoteApp.add(jmxUrl); + remoteApp.add(host); + if (urlScheme!=null) { + remoteApp.add(port); //could be null! + remoteApp.add(urlScheme); + } else if (host!=null) { + remoteApp.add(port); + //don't need to add urlScheme because we know it is null + } + buider.add(remoteApp); + } + } + } + return buider.build(); + } + + public static Disposable addListener(Runnable runnable) { + IPreferenceChangeListener l = event -> { + if (event.getKey().equals(REMOTE_APPS_KEY)) { + runnable.run(); + } + }; + getPreferences().addPreferenceChangeListener(l); + return () -> { + getPreferences().removePreferenceChangeListener(l); + }; + } + + private static IEclipsePreferences getPreferences() { + return BootLanguageServerPlugin.getPreferences(); + } + +} diff --git a/eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsPrefsPage.java b/eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsPrefsPage.java new file mode 100644 index 000000000..fbb555637 --- /dev/null +++ b/eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsPrefsPage.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2018 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 + * https://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Pivotal, Inc. - initial API and implementation + *******************************************************************************/ +package org.springframework.tooling.boot.ls.prefs; + +import java.util.List; + +import org.springsource.ide.eclipse.commons.livexp.ui.CommentSection; +import org.springsource.ide.eclipse.commons.livexp.ui.PreferencePageWithSections; +import org.springsource.ide.eclipse.commons.livexp.ui.PrefsPageSection; + +import com.google.common.collect.ImmutableList; + +public class RemoteAppsPrefsPage extends PreferencePageWithSections { + + public static final String PREF_REMOTE_BOOT_APPS_JSON = "boot-java.remote-apps"; + + @Override + protected List createSections() { + return ImmutableList.of( + new CommentSection(this, "For boot live hover support. Add jmxurl and hostname information for remote boot apps below. The format is as in this example:\n\n" + + "[\n" + + " {\n" + + " \"jmxurl\" : \"service:jmx:rmi://localhost:44251/jndi/rmi://localhost:44251/jmxrmi\",\n" + + " \"host\" : \"my-remote-app.cfapps.io\"\n" + + " \"urlScheme\": \"http\", //optional, defaults to 'https'\n" + + " \"port\": 80 //optional, defaults to '443'\n" + + " }\n" + + "]\n" + ), + new RemoteAppsSection(this) + ); + } +} + + diff --git a/eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsSection.java b/eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsSection.java new file mode 100644 index 000000000..391c12926 --- /dev/null +++ b/eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/RemoteAppsSection.java @@ -0,0 +1,70 @@ +package org.springframework.tooling.boot.ls.prefs; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Text; +import org.springsource.ide.eclipse.commons.livexp.core.AsyncLiveExpression; +import org.springsource.ide.eclipse.commons.livexp.core.LiveExpression; +import org.springsource.ide.eclipse.commons.livexp.core.LiveVariable; +import org.springsource.ide.eclipse.commons.livexp.core.ValidationResult; +import org.springsource.ide.eclipse.commons.livexp.ui.IPageWithSections; +import org.springsource.ide.eclipse.commons.livexp.ui.PrefsPageSection; +import org.springsource.ide.eclipse.commons.livexp.ui.UIConstants; +import org.springsource.ide.eclipse.commons.livexp.ui.util.SwtConnect; +import org.springsource.ide.eclipse.commons.livexp.util.ExceptionUtil; + +public class RemoteAppsSection extends PrefsPageSection { + + private RemoteAppsPrefs prefs = new RemoteAppsPrefs(); + private Text text; + private static final int HEIGHT_HINT = 150; + + private LiveVariable model = new LiveVariable<>(""); + private LiveExpression validator = new AsyncLiveExpression(ValidationResult.OK) { + { + dependsOn(model); + } + @Override + protected ValidationResult compute() { + try { + RemoteAppsPrefs.parse(model.getValue()); + return ValidationResult.OK; + } catch (Exception e) { + return ValidationResult.error(ExceptionUtil.getMessage(e)); + } + } + }; + + public RemoteAppsSection(IPageWithSections owner) { + super(owner); + } + + @Override + public boolean performOK() { + prefs.setRawJson(model.getValue()); + return true; + } + + @Override + public void performDefaults() { + model.setValue(""); + } + + @Override + public void createContents(Composite parent) { + model.setValue(prefs.getRawJson()); + text = new Text(parent, SWT.BORDER|SWT.MULTI|SWT.H_SCROLL|SWT.V_SCROLL|SWT.WRAP); + GridDataFactory.fillDefaults() + .hint(UIConstants.FIELD_TEXT_AREA_WIDTH, HEIGHT_HINT) + .grab(true, false) + .applyTo(text); + SwtConnect.connect(text, model); + } + + @Override + public LiveExpression getValidator() { + return validator; + } + +} diff --git a/eclipse-language-servers/org.springframework.tooling.cloudfoundry.manifest.ls/.classpath b/eclipse-language-servers/org.springframework.tooling.cloudfoundry.manifest.ls/.classpath index 2885bebb5..286ce45fe 100644 --- a/eclipse-language-servers/org.springframework.tooling.cloudfoundry.manifest.ls/.classpath +++ b/eclipse-language-servers/org.springframework.tooling.cloudfoundry.manifest.ls/.classpath @@ -1,9 +1,9 @@ - - + +