use of javax.management.remote.JMXServiceURL in project jdk8u_jdk by JetBrains.
the class EmptyDomainNotificationTest method main.
public static void main(String[] args) throws Exception {
final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
server.start();
JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);
final MBeanServerConnection mbsc = client.getMBeanServerConnection();
final ObjectName mbean = ObjectName.getInstance(":type=Simple");
mbsc.createMBean(Simple.class.getName(), mbean);
System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
final Listener li = new Listener();
mbsc.addNotificationListener(mbean, li, null, null);
System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
mbsc.invoke(mbean, "emitNotification", null, null);
System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
final long stopTime = System.currentTimeMillis() + 2000;
synchronized (li) {
long toWait = stopTime - System.currentTimeMillis();
while (li.received < 1 && toWait > 0) {
li.wait(toWait);
toWait = stopTime - System.currentTimeMillis();
}
}
if (li.received < 1) {
throw new RuntimeException("No notif received!");
} else if (li.received > 1) {
throw new RuntimeException("Wait one notif but got: " + li.received);
}
System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");
System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
mbsc.removeNotificationListener(mbean, li);
// clean
client.close();
server.stop();
System.out.println("EmptyDomainNotificationTest-main: Bye.");
}
use of javax.management.remote.JMXServiceURL in project jdk8u_jdk by JetBrains.
the class NotificationEmissionTest method testNotificationEmission.
public int testNotificationEmission(boolean prop, Object propValue, boolean sm, boolean policyPositive) throws Exception {
JMXConnectorServer server = null;
JMXConnector client = null;
// Set policy file
//
String policyFile = System.getProperty("test.src") + File.separator + (policyPositive ? "policy.positive" : "policy.negative");
echo("\nSetting policy file " + policyFile);
System.setProperty("java.security.policy", policyFile);
// Create a new MBeanServer
//
final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
try {
// Create server environment map
//
final Map<String, Object> env = new HashMap<String, Object>();
env.put("jmx.remote.authenticator", new CustomJMXAuthenticator());
if (prop)
env.put("jmx.remote.x.check.notification.emission", propValue);
// Create the JMXServiceURL
//
final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
// Create a JMXConnectorServer
//
server = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
// Start the JMXConnectorServer
//
server.start();
// Create server environment map
//
final Map<String, Object> cenv = new HashMap<String, Object>();
String[] credentials = new String[] { "role", "password" };
cenv.put("jmx.remote.credentials", credentials);
// Create JMXConnector and connect to JMXConnectorServer
//
client = JMXConnectorFactory.connect(server.getAddress(), cenv);
// Get non-secure MBeanServerConnection
//
final MBeanServerConnection mbsc = client.getMBeanServerConnection();
// Create NB MBean
//
ObjectName nb1 = ObjectName.getInstance("domain:type=NB,name=1");
ObjectName nb2 = ObjectName.getInstance("domain:type=NB,name=2");
ObjectName nb3 = ObjectName.getInstance("domain:type=NB,name=3");
mbsc.createMBean(NB.class.getName(), nb1);
mbsc.createMBean(NB.class.getName(), nb2);
mbsc.createMBean(NB.class.getName(), nb3);
// Add notification listener
//
Listener li = new Listener();
mbsc.addNotificationListener(nb1, li, null, null);
mbsc.addNotificationListener(nb2, li, null, null);
//
if (sm) {
echo("Setting SM");
System.setSecurityManager(new SecurityManager());
}
// Invoke the "sendNotification" method
//
mbsc.invoke(nb1, "emitNotification", new Object[] { 0, null }, new String[] { "int", "javax.management.ObjectName" });
mbsc.invoke(nb2, "emitNotification", new Object[] { 1, null }, new String[] { "int", "javax.management.ObjectName" });
mbsc.invoke(nb2, "emitNotification", new Object[] { 2, nb3 }, new String[] { "int", "javax.management.ObjectName" });
// If the check is effective and we're using policy.negative,
// then we should see the two notifs sent by nb2 (of which one
// has a getSource() that is nb3), but not the notif sent by nb1.
// Otherwise we should see all three notifs. The check is only
// effective if the property jmx.remote.x.check.notification.emission
// is explicitly true and there is a security manager.
int expectedNotifs = (prop && sm && !policyPositive) ? 2 : 3;
// Wait for notifications to be emitted
//
long deadline = System.currentTimeMillis() + 2000;
while (li.notifs.size() < expectedNotifs && System.currentTimeMillis() < deadline) Thread.sleep(1);
// Remove notification listener
//
mbsc.removeNotificationListener(nb1, li);
mbsc.removeNotificationListener(nb2, li);
int result = 0;
List<ObjectName> sources = new ArrayList<ObjectName>();
sources.add(nb1);
sources.add(nb2);
sources.add(nb3);
result = checkNotifs(expectedNotifs, li.notifs, sources);
if (result > 0) {
echo("...SecurityManager=" + sm + "; policy=" + policyPositive);
return result;
}
} finally {
//
if (client != null)
client.close();
//
if (server != null)
server.stop();
//
if (mbs != null)
MBeanServerFactory.releaseMBeanServer(mbs);
}
return 0;
}
use of javax.management.remote.JMXServiceURL in project jdk8u_jdk by JetBrains.
the class NotificationAccessControllerTest method runTest.
/**
* Run test
*/
public int runTest(boolean enableChecks, boolean throwException) throws Exception {
echo("\n=-=-= " + (enableChecks ? "Enable" : "Disable") + " notification access control checks " + (!enableChecks ? "" : (throwException ? ": add/remove " : ": fetch ")) + "=-=-=");
JMXConnectorServer server = null;
JMXConnector client = null;
/*
* (!enableChecks)
* - List must contain three notifs from sources nb1, nb2 and nb3
* (enableChecks && !throwException)
* - List must contain one notif from source nb1
* (enableChecks && throwException)
* - List must contain two notifs from sources nb2 and nb3
*/
final int expected_notifs = (!enableChecks ? 3 : (throwException ? 2 : 1));
// Create a new MBeanServer
//
final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
try {
// Create server environment map
//
final Map<String, Object> env = new HashMap<>();
env.put("jmx.remote.authenticator", new CustomJMXAuthenticator());
if (enableChecks) {
env.put("com.sun.jmx.remote.notification.access.controller", new NAC(throwException));
}
// Create the JMXServiceURL
//
final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
// Create a JMXConnectorServer
//
server = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
// Start the JMXConnectorServer
//
server.start();
// Create server environment map
//
final Map<String, Object> cenv = new HashMap<>();
String[] credentials = new String[] { "role", "password" };
cenv.put("jmx.remote.credentials", credentials);
// Create JMXConnector and connect to JMXConnectorServer
//
client = JMXConnectorFactory.connect(server.getAddress(), cenv);
// Get non-secure MBeanServerConnection
//
final MBeanServerConnection mbsc = client.getMBeanServerConnection();
// Create NB MBean
//
ObjectName nb1 = ObjectName.getInstance("domain:type=NB,name=1");
ObjectName nb2 = ObjectName.getInstance("domain:type=NB,name=2");
ObjectName nb3 = ObjectName.getInstance("domain:type=NB,name=3");
mbsc.createMBean(NB.class.getName(), nb1);
mbsc.createMBean(NB.class.getName(), nb2);
mbsc.createMBean(NB.class.getName(), nb3);
// Add notification listener
//
Semaphore s = new Semaphore(0);
Listener li = new Listener(s);
try {
mbsc.addNotificationListener(nb1, li, null, null);
if (enableChecks && throwException) {
echo("Didn't get expected exception");
return 1;
}
} catch (SecurityException e) {
if (enableChecks && throwException) {
echo("Got expected exception: " + e);
} else {
echo("Got unexpected exception: " + e);
return 1;
}
}
mbsc.addNotificationListener(nb2, li, null, null);
System.out.println("\n+++ Expecting to receive " + expected_notifs + " notification" + (expected_notifs > 1 ? "s" : "") + " +++");
// Invoke the "sendNotification" method
//
mbsc.invoke(nb1, "emitNotification", new Object[] { 0, null }, new String[] { "int", "javax.management.ObjectName" });
mbsc.invoke(nb2, "emitNotification", new Object[] { 1, null }, new String[] { "int", "javax.management.ObjectName" });
mbsc.invoke(nb2, "emitNotification", new Object[] { 2, nb3 }, new String[] { "int", "javax.management.ObjectName" });
// Wait for notifications to be emitted
//
s.acquire(expected_notifs);
//
if (!throwException)
mbsc.removeNotificationListener(nb1, li);
try {
mbsc.removeNotificationListener(nb2, li);
if (enableChecks && throwException) {
echo("Didn't get expected exception");
return 1;
}
} catch (SecurityException e) {
if (enableChecks && throwException) {
echo("Got expected exception: " + e);
} else {
echo("Got unexpected exception: " + e);
return 1;
}
}
int result = 0;
List<ObjectName> sources = new ArrayList();
sources.add(nb1);
sources.add(nb2);
sources.add(nb3);
result = checkNotifs(expected_notifs, li.notifs, sources);
if (result > 0) {
return result;
}
} catch (Exception e) {
echo("Failed to perform operation: " + e);
e.printStackTrace();
return 1;
} finally {
//
if (client != null)
client.close();
//
if (server != null)
server.stop();
//
if (mbs != null)
MBeanServerFactory.releaseMBeanServer(mbs);
}
return 0;
}
use of javax.management.remote.JMXServiceURL in project jdk8u_jdk by JetBrains.
the class UnexpectedNotifTest method test.
private static void test(String proto) throws Exception {
System.out.println("Unexpected notifications test for protocol " + proto);
MBeanServer mbs = null;
try {
// Create a MBeanServer
//
mbs = MBeanServerFactory.createMBeanServer();
// Create a NotificationEmitter MBean
//
mbean = new ObjectName("Default:name=NotificationEmitter");
mbs.registerMBean(new NotificationEmitter(), mbean);
// Create a connector server
//
url = new JMXServiceURL("service:jmx:" + proto + "://");
server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
mbs.registerMBean(server, new ObjectName("Default:name=ConnectorServer"));
server.start();
url = server.getAddress();
for (int j = 0; j < 2; j++) {
test();
}
} finally {
// Stop server
//
server.stop();
// Release the MBeanServer
//
MBeanServerFactory.releaseMBeanServer(mbs);
}
}
use of javax.management.remote.JMXServiceURL in project voldemort by voldemort.
the class AbstractFailureDetectorTest method assertJmxEquals.
protected void assertJmxEquals(String attributeName, Object attributeValue) throws Exception {
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(new JMXServiceURL("service:jmx:rmi://"), null, ManagementFactory.getPlatformMBeanServer());
cs.start();
JMXConnector cc = null;
try {
cc = JMXConnectorFactory.connect(cs.getAddress());
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ObjectName objectName = JmxUtils.createObjectName(JmxUtils.getPackageName(failureDetector.getClass()), failureDetector.getClass().getSimpleName());
Object availableNodes = mbsc.getAttribute(objectName, attributeName);
assertEquals(attributeValue, availableNodes);
} finally {
if (cc != null)
cc.close();
cs.stop();
}
}
Aggregations