use of javax.management.remote.JMXServiceURL in project jdk8u_jdk by JetBrains.
the class UnserializableTargetObjectTest method main.
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "targetObject" }, new Object[] { "operation", "operation", resource1 });
Descriptor getCountDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "targetObject" }, new Object[] { "operation", "getCount", resource2 });
Descriptor setCountDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "targetObject" }, new Object[] { "operation", "setCount", resource2 });
Descriptor countDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "getMethod", "setMethod" }, new Object[] { "attribute", "Count", "getCount", "setCount" });
ModelMBeanOperationInfo operationInfo = new ModelMBeanOperationInfo("operation description", operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo = new ModelMBeanOperationInfo("getCount description", getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo = new ModelMBeanOperationInfo("setCount description", setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo = new ModelMBeanAttributeInfo("Count", "Count description", getCountMethod, setCountMethod, countDescriptor);
ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(Resource.class.getName(), "ModelMBean to test targetObject", new ModelMBeanAttributeInfo[] { countInfo }, // no constructors
null, new ModelMBeanOperationInfo[] { operationInfo, getCountInfo, setCountInfo }, // no notifications
null);
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
use of javax.management.remote.JMXServiceURL in project geode by apache.
the class MBeanProcessController method connect.
/**
* Connects to the JMX agent in the local process.
*
* @throws ConnectionFailedException if there was a failure to connect to the local JMX connector
* in the process
* @throws IOException if the JDK management agent cannot be found and loaded
*/
private void connect() throws ConnectionFailedException, IOException {
try {
final JMXServiceURL jmxUrl = getJMXServiceURL();
this.jmxc = JMXConnectorFactory.connect(jmxUrl);
this.server = this.jmxc.getMBeanServerConnection();
} catch (AttachNotSupportedException e) {
throw new ConnectionFailedException("Failed to connect to process '" + this.pid + "'", e);
}
}
use of javax.management.remote.JMXServiceURL in project geode by apache.
the class MBeanProcessController method getJMXServiceURL.
/**
* Uses the Attach API to connect to the local process and ensures that it has loaded the JMX
* management agent. The JMXServiceURL identifying the local connector address for the JMX agent
* in the process is returned.
*
* @return the address of the JMX API connector server for connecting to the local process
*
* @throws AttachNotSupportedException if unable to use the Attach API to connect to the process
* @throws IOException if the JDK management agent cannot be found and loaded
*/
private JMXServiceURL getJMXServiceURL() throws AttachNotSupportedException, IOException {
String connectorAddress = null;
final VirtualMachine vm = VirtualMachine.attach(String.valueOf(this.pid));
try {
Properties agentProps = vm.getAgentProperties();
connectorAddress = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
if (connectorAddress == null) {
// need to load the management-agent and get the address
final String javaHome = vm.getSystemProperties().getProperty("java.home");
// assume java.home is JDK and look in JRE for agent
String managementAgentPath = javaHome + File.separator + "jre" + File.separator + "lib" + File.separator + "management-agent.jar";
File managementAgent = new File(managementAgentPath);
if (!managementAgent.exists()) {
// assume java.home is JRE and look in lib for agent
managementAgentPath = javaHome + File.separator + "lib" + File.separator + "management-agent.jar";
managementAgent = new File(managementAgentPath);
if (!managementAgent.exists()) {
throw new IOException("JDK management agent not found");
}
}
// attempt to load the management agent
managementAgentPath = managementAgent.getCanonicalPath();
try {
vm.loadAgent(managementAgentPath, "com.sun.management.jmxremote");
} catch (AgentLoadException e) {
IOException ioe = new IOException(e.getMessage());
ioe.initCause(e);
throw ioe;
} catch (AgentInitializationException e) {
IOException ioe = new IOException(e.getMessage());
ioe.initCause(e);
throw ioe;
}
// get the connector address
agentProps = vm.getAgentProperties();
connectorAddress = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
}
} finally {
vm.detach();
}
if (connectorAddress == null) {
// should never reach here
throw new IOException("Failed to find address to attach to process");
}
return new JMXServiceURL(connectorAddress);
}
use of javax.management.remote.JMXServiceURL in project geode by apache.
the class LocalProcessController method connect.
/**
* Connects to the JMX agent in the local process.
*
* @throws ConnectionFailedException if there was a failure to connect to the local JMX connector
* in the process
* @throws IOException if the JDK management agent cannot be found and loaded
*/
void connect() throws ConnectionFailedException, IOException {
try {
final JMXServiceURL jmxUrl = getJMXServiceURL();
this.jmxc = JMXConnectorFactory.connect(jmxUrl);
this.server = this.jmxc.getMBeanServerConnection();
} catch (AttachNotSupportedException e) {
throw new ConnectionFailedException("Failed to connect to process '" + this.pid + "'", e);
}
}
use of javax.management.remote.JMXServiceURL in project geode by apache.
the class LocalProcessController method getJMXServiceURL.
/**
* Uses the Attach API to connect to the local process and ensures that it has loaded the JMX
* management agent. The JMXServiceURL identifying the local connector address for the JMX agent
* in the process is returned.
*
* @return the address of the JMX API connector server for connecting to the local process
*
* @throws AttachNotSupportedException if unable to use the Attach API to connect to the process
* @throws IOException if the JDK management agent cannot be found and loaded
*/
private JMXServiceURL getJMXServiceURL() throws AttachNotSupportedException, IOException {
String connectorAddress = null;
final VirtualMachine vm = VirtualMachine.attach(String.valueOf(this.pid));
try {
Properties agentProps = vm.getAgentProperties();
connectorAddress = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
if (connectorAddress == null) {
// need to load the management-agent and get the address
final String javaHome = vm.getSystemProperties().getProperty("java.home");
// assume java.home is JDK and look in JRE for agent
String managementAgentPath = javaHome + File.separator + "jre" + File.separator + "lib" + File.separator + "management-agent.jar";
File managementAgent = new File(managementAgentPath);
if (!managementAgent.exists()) {
// assume java.home is JRE and look in lib for agent
managementAgentPath = javaHome + File.separator + "lib" + File.separator + "management-agent.jar";
managementAgent = new File(managementAgentPath);
if (!managementAgent.exists()) {
throw new IOException("JDK management agent not found");
}
}
// attempt to load the management agent
managementAgentPath = managementAgent.getCanonicalPath();
try {
vm.loadAgent(managementAgentPath, "com.sun.management.jmxremote");
} catch (AgentLoadException e) {
IOException ioe = new IOException(e.getMessage());
ioe.initCause(e);
throw ioe;
} catch (AgentInitializationException e) {
IOException ioe = new IOException(e.getMessage());
ioe.initCause(e);
throw ioe;
}
// get the connector address
agentProps = vm.getAgentProperties();
connectorAddress = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
}
} finally {
vm.detach();
}
if (connectorAddress == null) {
// should never reach here
throw new IOException("Failed to find address to attach to process");
}
return new JMXServiceURL(connectorAddress);
}
Aggregations