refactored boot app cli code slightly and fixed a problem with getting port from tomcat bean

This commit is contained in:
Martin Lippert
2017-05-15 16:58:49 +02:00
parent 5b3dbdb6c0
commit e7870ef2c1
2 changed files with 176 additions and 150 deletions

View File

@@ -0,0 +1,169 @@
/*******************************************************************************
* Copyright (c) 2017 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.boot.app.cli;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
/**
* @author Martin Lippert
*/
@SuppressWarnings("restriction")
public class SpringBootApp {
private VirtualMachine vm;
private VirtualMachineDescriptor vmd;
public SpringBootApp(VirtualMachineDescriptor vmd) throws Exception {
this.vmd = vmd;
this.vm = VirtualMachine.attach(vmd);
}
public String getProcessID() {
return vmd.id();
}
public String getProcessName() {
return vmd.displayName();
}
public boolean isSpringBootApp() throws Exception {
Properties props = this.vm.getSystemProperties();
String classpath = (String) props.get("java.class.path");
String[] cpElements = getClasspath(classpath);
return contains(cpElements, "spring-boot");
}
public String getPort() throws Exception {
String jmxConnect = this.vm.startLocalManagementAgent();
JMXConnector jmxConnector = null;
try {
JMXServiceURL serviceUrl = new JMXServiceURL(jmxConnect);
jmxConnector = JMXConnectorFactory.connect(serviceUrl, null);
return getPort(jmxConnector);
}
finally {
if (jmxConnector != null) jmxConnector.close();
}
}
protected boolean contains(String[] cpElements, String element) {
for (String cpElement : cpElements) {
if (cpElement.contains(element)) {
return true;
}
}
return false;
}
protected String[] getClasspath(String classpath) {
List<String> classpathElements = new ArrayList<>();
if (classpath != null) {
StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
while (tokenizer.hasMoreTokens()) {
String classpathElement = tokenizer.nextToken();
classpathElements.add(classpathElement);
}
}
return (String[]) classpathElements.toArray(new String[classpathElements.size()]);
}
protected String getPort(JMXConnector jmxConnector) throws Exception {
MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();
String port = getPortViaAdmin(connection);
if (port != null) {
return port;
}
port = getPortViaActuator(connection);
if (port != null) {
return port;
}
port = getPortViaTomcatBean(connection);
return port;
}
protected String getPortViaAdmin(MBeanServerConnection connection) throws Exception {
try {
String DEFAULT_OBJECT_NAME = "org.springframework.boot:type=Admin,name=SpringApplication";
ObjectName objectName = new ObjectName(DEFAULT_OBJECT_NAME);
Object o = connection.invoke(objectName,"getProperty", new String[] {"local.server.port"}, new String[] {String.class.getName()});
return o.toString();
}
catch (InstanceNotFoundException e) {
return null;
}
}
protected String getPortViaActuator(MBeanServerConnection connection) throws Exception {
try {
String DEFAULT_OBJECT_NAME = "org.springframework.boot:type=Endpoint,name=environmentEndpoint";
ObjectName objectName = new ObjectName(DEFAULT_OBJECT_NAME);
Object result = connection.getAttribute(objectName, "Data");
String environment = new ObjectMapper().writeValueAsString(result);
JSONObject env = new JSONObject(environment);
if (env != null) {
JSONObject portsObject = env.getJSONObject("server.ports");
if (portsObject != null) {
String portValue = portsObject.getString("local.server.port");
return portValue;
}
}
}
catch (InstanceNotFoundException e) {
}
return null;
}
protected String getPortViaTomcatBean(MBeanServerConnection connection) throws Exception {
try {
Set<ObjectName> queryNames = connection.queryNames(null, null);
for (ObjectName objectName : queryNames) {
if (objectName.toString().startsWith("Tomcat:type=Connector")) {
Object result = connection.getAttribute(objectName, "localPort");
if (result != null) {
return result.toString();
}
}
}
}
catch (InstanceNotFoundException e) {
}
return null;
}
}

View File

@@ -10,24 +10,8 @@
*******************************************************************************/
package org.springframework.ide.vscode.commons.boot.app.cli;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
@@ -40,145 +24,18 @@ public class SpringBootAppCLI {
public static void main(String[] args) throws Exception {
List<VirtualMachineDescriptor> list = VirtualMachine.list();
for (VirtualMachineDescriptor vmd : list) {
VirtualMachine vm = VirtualMachine.attach(vmd);
Properties props = vm.getSystemProperties();
String classpath = (String) props.get("java.class.path");
String[] cpElements = getClasspath(classpath);
boolean isSpringBoot = contains(cpElements, "spring-boot");
boolean isSpringBootActuator = contains(cpElements, "spring-boot-actuator");
if (isSpringBoot) {
printBootAppDetails(vmd, vm, isSpringBootActuator);
SpringBootApp app = new SpringBootApp(vmd);
if (app.isSpringBootApp()) {
printBootAppDetails(app);
}
}
}
private static void printBootAppDetails(VirtualMachineDescriptor vmd, VirtualMachine vm,
boolean isSpringBootActuator) throws IOException {
System.out.println("Spring Boot App: " + vmd.id());
System.out.println("Name: " + vmd.displayName());
System.out.println("Actuators available: " + isSpringBootActuator);
String jmxConnect = vm.startLocalManagementAgent();
System.out.println("JMX Connection: " + jmxConnect);
JMXConnector jmxConnector = null;
try {
JMXServiceURL serviceUrl = new JMXServiceURL(jmxConnect);
jmxConnector = JMXConnectorFactory.connect(serviceUrl, null);
try {
String port = getPort(jmxConnector);
System.out.println("Runs on port: " + port);
}
catch (InstanceNotFoundException e) {
System.out.println("Port not available");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (jmxConnector != null) jmxConnector.close();
}
private static void printBootAppDetails(SpringBootApp app) throws Exception {
System.out.println("Spring Boot App: " + app.getProcessID());
System.out.println("Name: " + app.getProcessName());
System.out.println("Port: " + app.getPort());
System.out.println();
}
private static boolean contains(String[] cpElements, String element) {
for (String cpElement : cpElements) {
if (cpElement.contains(element)) {
return true;
}
}
return false;
}
private static String[] getClasspath(String classpath) {
List<String> classpathElements = new ArrayList<>();
if (classpath != null) {
StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
while (tokenizer.hasMoreTokens()) {
String classpathElement = tokenizer.nextToken();
classpathElements.add(classpathElement);
}
}
return (String[]) classpathElements.toArray(new String[classpathElements.size()]);
}
private static String getPort(JMXConnector jmxConnector) throws Exception {
MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();
String port = getPortViaAdmin(connection);
if (port != null) {
return port;
}
port = getPortViaActuator(connection);
if (port != null) {
return port;
}
port = getPortViaTomcatBean(connection);
return port;
}
private static String getPortViaAdmin(MBeanServerConnection connection) throws Exception {
try {
String DEFAULT_OBJECT_NAME = "org.springframework.boot:type=Admin,name=SpringApplication";
ObjectName objectName = new ObjectName(DEFAULT_OBJECT_NAME);
Object o = connection.invoke(objectName,"getProperty", new String[] {"local.server.port"}, new String[] {String.class.getName()});
return o.toString();
}
catch (InstanceNotFoundException e) {
System.out.println("Admin management bean not available");
return null;
}
}
private static String getPortViaActuator(MBeanServerConnection connection) throws Exception {
try {
String DEFAULT_OBJECT_NAME = "org.springframework.boot:type=Endpoint,name=environmentEndpoint";
ObjectName objectName = new ObjectName(DEFAULT_OBJECT_NAME);
Object result = connection.getAttribute(objectName, "Data");
String environment = new ObjectMapper().writeValueAsString(result);
System.out.println("Environment: " + environment);
JSONObject env = new JSONObject(environment);
if (env != null) {
JSONObject portsObject = env.getJSONObject("server.ports");
if (portsObject != null) {
String portValue = portsObject.getString("local.server.port");
return portValue;
}
}
}
catch (InstanceNotFoundException e) {
System.out.println("Actuator not available: Spring Boot actuators not enabled");
}
return null;
}
private static String getPortViaTomcatBean(MBeanServerConnection connection) throws Exception {
try {
Set<ObjectName> queryNames = connection.queryNames(null, null);
for (ObjectName objectName : queryNames) {
if (objectName.toString().startsWith("Tomcat:type=Connector")) {
Object result = connection.getAttribute(objectName, "localPort");
System.out.println("localpost: " + result.toString());
}
}
}
catch (InstanceNotFoundException e) {
System.out.println("another bean not found");
}
return null;
}
}