use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.
the class ConnectorBootstrap method startLocalConnectorServer.
/*
* Creates and starts a RMI Connector Server for "local" monitoring
* and management.
*/
public static JMXConnectorServer startLocalConnectorServer() {
// Ensure cryptographically strong random number generater used
// to choose the object number - see java.rmi.server.ObjID
System.setProperty("java.rmi.server.randomIDs", "true");
// This RMI server should not keep the VM alive
Map<String, Object> env = new HashMap<>();
env.put(RMIExporter.EXPORTER_ATTRIBUTE, new PermanentExporter());
env.put(EnvHelp.CREDENTIAL_TYPES, new String[] { String[].class.getName(), String.class.getName() });
// The local connector server need only be available via the
// loopback connection.
String localhost = "localhost";
InetAddress lh = null;
try {
lh = InetAddress.getByName(localhost);
localhost = lh.getHostAddress();
} catch (UnknownHostException x) {
}
// a loopback address.
if (lh == null || !lh.isLoopbackAddress()) {
localhost = "127.0.0.1";
}
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
try {
JMXServiceURL url = new JMXServiceURL("rmi", localhost, 0);
// Do we accept connections from local interfaces only?
Properties props = Agent.getManagementProperties();
if (props == null) {
props = new Properties();
}
String useLocalOnlyStr = props.getProperty(PropertyNames.USE_LOCAL_ONLY, DefaultValues.USE_LOCAL_ONLY);
boolean useLocalOnly = Boolean.valueOf(useLocalOnlyStr).booleanValue();
if (useLocalOnly) {
env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, new LocalRMIServerSocketFactory());
}
JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
server.start();
return server;
} catch (Exception e) {
throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.toString());
}
}
use of javax.management.remote.JMXConnectorServer in project jvm-breakglass by matlux.
the class MBeanTest method createJMXServer.
private JMXConnectorServer createJMXServer(int port, JMXServiceURL url) throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
java.rmi.registry.LocateRegistry.createRegistry(port);
JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
return connectorServer;
}
use of javax.management.remote.JMXConnectorServer in project jvm-breakglass by matlux.
the class MBeanTest method testStartStopViaJMX.
@Test
public void testStartStopViaJMX() throws Exception {
int port = 9876;
JMXServiceURL url = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi", port));
JMXConnectorServer jmxServer = createJMXServer(port, url);
try {
jmxServer.start();
assertFalse(nreplServer.isStarted());
JMXConnector connector = JMXConnectorFactory.connect(url);
try {
MBeanServerConnection conn = connector.getMBeanServerConnection();
ObjectName objectName = MBeanRegistration.getObjectName();
NreplMBean proxy = JMX.newMBeanProxy(conn, objectName, NreplMBean.class);
assertFalse(proxy.isStarted());
proxy.start();
assertTrue(proxy.isStarted());
proxy.stop();
assertFalse(proxy.isStarted());
} finally {
connector.close();
}
} finally {
jmxServer.stop();
}
assertFalse(nreplServer.isStarted());
}
use of javax.management.remote.JMXConnectorServer in project jvm-breakglass by matlux.
the class MBeanTest method testStartViaJMX.
@Test
public void testStartViaJMX() throws Exception {
int port = 9875;
JMXServiceURL url = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi", port));
JMXConnectorServer jmxServer = createJMXServer(port, url);
try {
jmxServer.start();
assertFalse(nreplServer.isStarted());
JMXConnector connector = JMXConnectorFactory.connect(url);
try {
MBeanServerConnection conn = connector.getMBeanServerConnection();
ObjectName objectName = MBeanRegistration.getObjectName();
NreplMBean proxy = JMX.newMBeanProxy(conn, objectName, NreplMBean.class);
assertFalse(proxy.isStarted());
proxy.start();
assertTrue(proxy.isStarted());
} finally {
connector.close();
}
assertTrue(nreplServer.isStarted());
} finally {
jmxServer.stop();
}
}
use of javax.management.remote.JMXConnectorServer in project graphdb by neo4j-attic.
the class HotspotManagementSupport method getJMXServiceURL.
@Override
protected JMXServiceURL getJMXServiceURL(KernelData kernel) {
JMXServiceURL url = null;
try {
Class<?> cal = Class.forName("sun.management.ConnectorAddressLink");
try {
Method importRemoteFrom = cal.getMethod("importRemoteFrom", int.class);
@SuppressWarnings("unchecked") Map<String, String> remote = (Map<String, String>) importRemoteFrom.invoke(null, Integer.valueOf(0));
url = getUrlFrom(remote);
} catch (NoSuchMethodException ex) {
// handled by null check
}
if (url == null) {
Method importFrom = cal.getMethod("importFrom", int.class);
url = getUrlFrom((String) importFrom.invoke(null, Integer.valueOf(0)));
}
} catch (InvocationTargetException e) {
log.log(Level.CONFIG, "Failed to load local JMX connection URL.", e.getTargetException());
} catch (LinkageError e) {
log.log(Level.CONFIG, "Failed to load local JMX connection URL.", e);
} catch (Exception e) {
log.log(Level.CONFIG, "Failed to load local JMX connection URL.", e);
}
// No previous connection server -- create one!
if (url == null) {
Object portObj = kernel.getParam("jmx.port");
int port = 0;
if (portObj instanceof Integer) {
port = ((Integer) portObj).intValue();
} else if (portObj instanceof String) {
try {
port = Integer.parseInt((String) portObj);
} catch (NumberFormatException ok) {
// handled by 0-check
}
}
if (port > 0) {
Object useSslObj = kernel.getParam("jmx.use_ssl");
boolean useSSL = false;
if (useSslObj instanceof Boolean) {
useSSL = ((Boolean) useSslObj).booleanValue();
} else if (useSslObj instanceof String) {
useSSL = Boolean.parseBoolean((String) useSslObj);
}
log.log(Level.CONFIG, "Creating new MBean server on port %s%s", new Object[] { Integer.valueOf(port), useSSL ? " using ssl" : "" });
JMXConnectorServer server = createServer(port, useSSL);
if (server != null) {
try {
server.start();
} catch (IOException e) {
log.log(Level.CONFIG, "Failed to start MBean server", e);
server = null;
}
if (server != null) {
try {
server.getMBeanServer().registerMBean(server, KernelProxy.createObjectName(kernel.instanceId(), "JMX Server"));
} catch (Exception e) {
log.log(Level.CONFIG, "Failed to register MBean server as JMX bean", e);
}
url = server.getAddress();
}
}
}
}
return url;
}
Aggregations