下面列出了javax.management.Query#and ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Builds the QueryExp used to identify the target MBean.
*
* @param pidAttribute the name of the MBean attribute with the process id to compare against
* @param attributes the names of additional MBean attributes to compare with expected values
* @param values the expected values of the specified MBean attributes
*
* @return the main QueryExp for matching the target MBean
*/
private QueryExp buildQueryExp(final String pidAttribute, final String[] attributes, final Object[] values) {
final QueryExp optionalAttributes = buildOptionalQueryExp(attributes, values);
QueryExp constraint;
if (optionalAttributes != null) {
constraint = Query.and(optionalAttributes, Query.eq(
Query.attr(pidAttribute),
Query.value(this.pid)));
} else {
constraint = Query.eq(
Query.attr(pidAttribute),
Query.value(this.pid));
}
return constraint;
}
/**
* Builds an optional QueryExp to aid in matching the correct MBean using
* additional attributes with the specified values. Returns null if no
* attributes and values were specified during construction.
*
* @param attributes the names of additional MBean attributes to compare with expected values
* @param values the expected values of the specified MBean attributes
*
* @return optional QueryExp to aid in matching the correct MBean
*/
private QueryExp buildOptionalQueryExp(final String[] attributes, final Object[] values) {
QueryExp queryExp = null;
for (int i = 0; i < attributes.length; i++) {
if (values[i] instanceof Boolean) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value(((Boolean) values[i])));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value(((Boolean) values[i]))));
}
} else if (values[i] instanceof Number) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value((Number)values[i]));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value((Number)values[i])));
}
} else if (values[i] instanceof String) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value((String)values[i]));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value((String)values[i])));
}
}
}
return queryExp;
}
/**
* Builds the QueryExp used to identify the target MBean.
*
* @param pidAttribute the name of the MBean attribute with the process id to compare against
* @param attributes the names of additional MBean attributes to compare with expected values
* @param values the expected values of the specified MBean attributes
*
* @return the main QueryExp for matching the target MBean
*/
private QueryExp buildQueryExp(final String pidAttribute, final String[] attributes, final Object[] values) {
final QueryExp optionalAttributes = buildOptionalQueryExp(attributes, values);
QueryExp constraint;
if (optionalAttributes != null) {
constraint = Query.and(optionalAttributes, Query.eq(
Query.attr(pidAttribute),
Query.value(this.pid)));
} else {
constraint = Query.eq(
Query.attr(pidAttribute),
Query.value(this.pid));
}
return constraint;
}
/**
* Builds an optional QueryExp to aid in matching the correct MBean using
* additional attributes with the specified values. Returns null if no
* attributes and values were specified during construction.
*
* @param attributes the names of additional MBean attributes to compare with expected values
* @param values the expected values of the specified MBean attributes
*
* @return optional QueryExp to aid in matching the correct MBean
*/
private QueryExp buildOptionalQueryExp(final String[] attributes, final Object[] values) {
QueryExp queryExp = null;
for (int i = 0; i < attributes.length; i++) {
if (values[i] instanceof Boolean) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value(((Boolean) values[i])));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value(((Boolean) values[i]))));
}
} else if (values[i] instanceof Number) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value((Number)values[i]));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value((Number)values[i])));
}
} else if (values[i] instanceof String) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value((String)values[i]));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value((String)values[i])));
}
}
}
return queryExp;
}
/**
* Builds the QueryExp used to identify the target MBean.
*
* @param pidAttribute the name of the MBean attribute with the process id to compare against
* @param attributes the names of additional MBean attributes to compare with expected values
* @param values the expected values of the specified MBean attributes
*
* @return the main QueryExp for matching the target MBean
*/
private QueryExp buildQueryExp(final String pidAttribute, final String[] attributes, final Object[] values) {
final QueryExp optionalAttributes = buildOptionalQueryExp(attributes, values);
QueryExp constraint;
if (optionalAttributes != null) {
constraint = Query.and(optionalAttributes, Query.eq(
Query.attr(pidAttribute),
Query.value(this.pid)));
} else {
constraint = Query.eq(
Query.attr(pidAttribute),
Query.value(this.pid));
}
return constraint;
}
/**
* Builds an optional QueryExp to aid in matching the correct MBean using
* additional attributes with the specified values. Returns null if no
* attributes and values were specified during construction.
*
* @param attributes the names of additional MBean attributes to compare with expected values
* @param values the expected values of the specified MBean attributes
*
* @return optional QueryExp to aid in matching the correct MBean
*/
private QueryExp buildOptionalQueryExp(final String[] attributes, final Object[] values) {
QueryExp queryExp = null;
for (int i = 0; i < attributes.length; i++) {
if (values[i] instanceof Boolean) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value(((Boolean) values[i])));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value(((Boolean) values[i]))));
}
} else if (values[i] instanceof Number) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value((Number)values[i]));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value((Number)values[i])));
}
} else if (values[i] instanceof String) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value((String)values[i]));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value((String)values[i])));
}
}
}
return queryExp;
}
/**
* Builds the QueryExp used to identify the target MBean.
*
* @param pidAttribute the name of the MBean attribute with the process id to compare against
* @param attributes the names of additional MBean attributes to compare with expected values
* @param values the expected values of the specified MBean attributes
*
* @return the main QueryExp for matching the target MBean
*/
private QueryExp buildQueryExp(final String pidAttribute, final String[] attributes, final Object[] values) {
final QueryExp optionalAttributes = buildOptionalQueryExp(attributes, values);
QueryExp constraint;
if (optionalAttributes != null) {
constraint = Query.and(optionalAttributes, Query.eq(
Query.attr(pidAttribute),
Query.value(this.pid)));
} else {
constraint = Query.eq(
Query.attr(pidAttribute),
Query.value(this.pid));
}
return constraint;
}
/**
* Builds an optional QueryExp to aid in matching the correct MBean using
* additional attributes with the specified values. Returns null if no
* attributes and values were specified during construction.
*
* @param attributes the names of additional MBean attributes to compare with expected values
* @param values the expected values of the specified MBean attributes
*
* @return optional QueryExp to aid in matching the correct MBean
*/
private QueryExp buildOptionalQueryExp(final String[] attributes, final Object[] values) {
QueryExp queryExp = null;
for (int i = 0; i < attributes.length; i++) {
if (values[i] instanceof Boolean) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value(((Boolean) values[i])));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value(((Boolean) values[i]))));
}
} else if (values[i] instanceof Number) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value((Number)values[i]));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value((Number)values[i])));
}
} else if (values[i] instanceof String) {
if (queryExp == null) {
queryExp = Query.eq(
Query.attr(attributes[i]),
Query.value((String)values[i]));
} else {
queryExp = Query.and(queryExp,
Query.eq(Query.attr(attributes[i]),
Query.value((String)values[i])));
}
}
}
return queryExp;
}
public void testProcessMBean() throws Exception {
final String testName = "testProcessMBean";
final int pid = ProcessUtils.identifyPid();
final Process process = new Process(pid, true);
final ObjectName objectName = ObjectName.getInstance(
getClass().getSimpleName() + ":testName=" + testName);
final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
final ObjectInstance instance = server.registerMBean(process, objectName);
assertNotNull(instance);
try {
// validate basics of the ProcessMBean
Set<ObjectName> mbeanNames = server.queryNames(objectName, null);
assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
assertEquals(1, mbeanNames.size());
final ObjectName name = mbeanNames.iterator().next();
final MBeanInfo info = server.getMBeanInfo(name);
final MBeanOperationInfo[] operInfo = info.getOperations();
assertEquals(1, operInfo.length);
assertEquals("stop", operInfo[0].getName());
final MBeanAttributeInfo[] attrInfo = info.getAttributes();
assertEquals(2, attrInfo.length);
// The order of these attributes is indeterminate
// assertEquals("Pid", attrInfo[0].getName());
// assertEquals("Process", attrInfo[1].getName());
assertNotNull(server.getAttribute(name, "Pid"));
assertNotNull(server.getAttribute(name, "Process"));
assertEquals(pid, server.getAttribute(name, "Pid"));
assertEquals(true, server.getAttribute(name, "Process"));
// validate query using only Pid attribute
QueryExp constraint = Query.eq(
Query.attr("Pid"),
Query.value(pid));
mbeanNames = server.queryNames(objectName, constraint);
assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
// validate query with wrong Pid finds nothing
constraint = Query.eq(
Query.attr("Pid"),
Query.value(pid+1));
mbeanNames = server.queryNames(objectName, constraint);
assertTrue("Found matching mbeans", mbeanNames.isEmpty());
// validate query using both attributes
constraint = Query.and(
Query.eq(Query.attr("Process"),Query.value(true)),
Query.eq(Query.attr("Pid"),Query.value(pid)));
mbeanNames = server.queryNames(objectName, constraint);
assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
// validate query with wrong attribute finds nothing
constraint = Query.and(
Query.eq(Query.attr("Process"),Query.value(false)),
Query.eq(Query.attr("Pid"),Query.value(pid)));
mbeanNames = server.queryNames(objectName, constraint);
assertTrue("Found matching mbeans", mbeanNames.isEmpty());
} finally {
try {
server.unregisterMBean(objectName);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
public void testProcessMBean() throws Exception {
final String testName = "testProcessMBean";
final int pid = ProcessUtils.identifyPid();
final Process process = new Process(pid, true);
final ObjectName objectName = ObjectName.getInstance(
getClass().getSimpleName() + ":testName=" + testName);
final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
final ObjectInstance instance = server.registerMBean(process, objectName);
assertNotNull(instance);
try {
// validate basics of the ProcessMBean
Set<ObjectName> mbeanNames = server.queryNames(objectName, null);
assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
assertEquals(1, mbeanNames.size());
final ObjectName name = mbeanNames.iterator().next();
final MBeanInfo info = server.getMBeanInfo(name);
final MBeanOperationInfo[] operInfo = info.getOperations();
assertEquals(1, operInfo.length);
assertEquals("stop", operInfo[0].getName());
final MBeanAttributeInfo[] attrInfo = info.getAttributes();
assertEquals(2, attrInfo.length);
// The order of these attributes is indeterminate
// assertEquals("Pid", attrInfo[0].getName());
// assertEquals("Process", attrInfo[1].getName());
assertNotNull(server.getAttribute(name, "Pid"));
assertNotNull(server.getAttribute(name, "Process"));
assertEquals(pid, server.getAttribute(name, "Pid"));
assertEquals(true, server.getAttribute(name, "Process"));
// validate query using only Pid attribute
QueryExp constraint = Query.eq(
Query.attr("Pid"),
Query.value(pid));
mbeanNames = server.queryNames(objectName, constraint);
assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
// validate query with wrong Pid finds nothing
constraint = Query.eq(
Query.attr("Pid"),
Query.value(pid+1));
mbeanNames = server.queryNames(objectName, constraint);
assertTrue("Found matching mbeans", mbeanNames.isEmpty());
// validate query using both attributes
constraint = Query.and(
Query.eq(Query.attr("Process"),Query.value(true)),
Query.eq(Query.attr("Pid"),Query.value(pid)));
mbeanNames = server.queryNames(objectName, constraint);
assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
// validate query with wrong attribute finds nothing
constraint = Query.and(
Query.eq(Query.attr("Process"),Query.value(false)),
Query.eq(Query.attr("Pid"),Query.value(pid)));
mbeanNames = server.queryNames(objectName, constraint);
assertTrue("Found matching mbeans", mbeanNames.isEmpty());
} finally {
try {
server.unregisterMBean(objectName);
} catch (Throwable t) {
t.printStackTrace();
}
}
}