Skip to content

Commit

Permalink
Improve handling of JMXServiceURLs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Nov 1, 2019
1 parent d93e50f commit 6aee490
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies {
}

publishing {
def coreVersion = '0.4.0';
def coreVersion = '0.4.1';
publications {
maven(MavenPublication) {
groupId = 'com.redhat.rhjmc'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
package com.redhat.rhjmc.containerjfr.core.net;

import javax.management.remote.JMXServiceURL;
import java.io.IOException;

import com.redhat.rhjmc.containerjfr.core.sys.Clock;
import com.redhat.rhjmc.containerjfr.core.tui.ClientWriter;

import org.openjdk.jmc.rjmx.ConnectionToolkit;
import org.openjdk.jmc.rjmx.IConnectionDescriptor;
import org.openjdk.jmc.rjmx.IConnectionHandle;
import org.openjdk.jmc.rjmx.IConnectionListener;
import org.openjdk.jmc.rjmx.internal.DefaultConnectionHandle;
import org.openjdk.jmc.rjmx.internal.JMXConnectionDescriptor;
import org.openjdk.jmc.rjmx.internal.RJMXConnection;
import org.openjdk.jmc.rjmx.internal.ServerDescriptor;
import org.openjdk.jmc.rjmx.services.jfr.IFlightRecorderService;
import org.openjdk.jmc.rjmx.services.jfr.internal.FlightRecorderServiceFactory;
import org.openjdk.jmc.ui.common.security.InMemoryCredentials;

public class JFRConnection implements AutoCloseable {

static final String URL_FORMAT = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi";
public static final int DEFAULT_PORT = 9091;

private final ClientWriter cw;
private final JMXServiceURL url;
private final RJMXConnection rjmxConnection;
private final IConnectionHandle handle;
private final IFlightRecorderService service;

JFRConnection(ClientWriter cw, String host, int port) throws Exception {
this(cw, new JMXServiceURL(String.format(URL_FORMAT, host, port)));
}

JFRConnection(ClientWriter cw, JMXServiceURL url) throws Exception {
JFRConnection(ClientWriter cw, IConnectionDescriptor cd) throws Exception {
this.cw = cw;
this.url = url;
this.rjmxConnection = attemptConnect(url);
this.rjmxConnection = attemptConnect(cd);
this.handle = new DefaultConnectionHandle(rjmxConnection, "RJMX Connection", new IConnectionListener[0]);
this.service = new FlightRecorderServiceFactory().getServiceInstance(handle);
}
Expand All @@ -47,15 +40,25 @@ public IFlightRecorderService getService() {
}

public long getApproximateServerTime(Clock clock) {
return rjmxConnection.getApproximateServerTime(clock.getWallTime());
return this.rjmxConnection.getApproximateServerTime(clock.getWallTime());
}

public String getHost() {
return this.url.getHost();
try {
return ConnectionToolkit.getHostName(this.rjmxConnection.getConnectionDescriptor().createJMXServiceURL());
} catch (IOException e) {
cw.println(e);
return "unknown";
}
}

public int getPort() {
return this.url.getPort();
try {
return ConnectionToolkit.getPort(this.rjmxConnection.getConnectionDescriptor().createJMXServiceURL());
} catch (IOException e) {
cw.println(e);
return 0;
}
}

public void disconnect() {
Expand All @@ -67,14 +70,9 @@ public void close() {
this.disconnect();
}

private RJMXConnection attemptConnect(JMXServiceURL url) throws Exception {
JMXConnectionDescriptor cd = new JMXConnectionDescriptor(
url,
new InMemoryCredentials(null, null));
ServerDescriptor sd = new ServerDescriptor(null, "Container", null);

private RJMXConnection attemptConnect(IConnectionDescriptor cd) throws Exception {
try {
RJMXConnection conn = new RJMXConnection(cd, sd, JFRConnection::failConnection);
RJMXConnection conn = new RJMXConnection(cd, new ServerDescriptor(), JFRConnection::failConnection);
if (!conn.connect()) {
failConnection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import com.redhat.rhjmc.containerjfr.core.tui.ClientWriter;

import org.openjdk.jmc.rjmx.ConnectionDescriptorBuilder;

public class JFRConnectionToolkit {

private final ClientWriter cw;
Expand All @@ -13,14 +15,14 @@ public JFRConnectionToolkit(ClientWriter cw) {
}

public JFRConnection connect(JMXServiceURL url) throws Exception {
return new JFRConnection(cw, url);
return new JFRConnection(cw, new ConnectionDescriptorBuilder().url(url).build());
}

public JFRConnection connect(String host) throws Exception {
return connect(host, JFRConnection.DEFAULT_PORT);
}

public JFRConnection connect(String host, int port) throws Exception {
return new JFRConnection(cw, host, port);
return new JFRConnection(cw, new ConnectionDescriptorBuilder().hostName(host).port(port).build());
}
}

0 comments on commit 6aee490

Please sign in to comment.