use of javax.management.AttributeList in project jdk8u_jdk by JetBrains.
the class XMBeanAttributes method doLoadAttributes.
// Don't call this in EDT, but execute returned Runnable inside
// EDT - typically in the done() method of a SwingWorker
// This method can return null.
private Runnable doLoadAttributes(final XMBean mbean, MBeanInfo infoOrNull) throws JMException, IOException {
if (mbean == null)
return null;
final MBeanInfo curMBeanInfo = (infoOrNull == null) ? mbean.getMBeanInfo() : infoOrNull;
final MBeanAttributeInfo[] attrsInfo = curMBeanInfo.getAttributes();
final HashMap<String, Object> attrs = new HashMap<String, Object>(attrsInfo.length);
final HashMap<String, Object> unavailableAttrs = new HashMap<String, Object>(attrsInfo.length);
final HashMap<String, Object> viewableAttrs = new HashMap<String, Object>(attrsInfo.length);
AttributeList list = null;
try {
list = mbean.getAttributes(attrsInfo);
} catch (Exception e) {
if (JConsole.isDebug()) {
System.err.println("Error calling getAttributes() on MBean \"" + mbean.getObjectName() + "\". JConsole will " + "try to get them individually calling " + "getAttribute() instead. Exception:");
e.printStackTrace(System.err);
}
list = new AttributeList();
//Can't load all attributes, do it one after each other.
for (int i = 0; i < attrsInfo.length; i++) {
String name = null;
try {
name = attrsInfo[i].getName();
Object value = mbean.getMBeanServerConnection().getAttribute(mbean.getObjectName(), name);
list.add(new Attribute(name, value));
} catch (Exception ex) {
if (attrsInfo[i].isReadable()) {
unavailableAttrs.put(name, Utils.getActualException(ex).toString());
}
}
}
}
try {
int att_length = list.size();
for (int i = 0; i < att_length; i++) {
Attribute attribute = (Attribute) list.get(i);
if (isViewable(attribute)) {
viewableAttrs.put(attribute.getName(), attribute.getValue());
} else
attrs.put(attribute.getName(), attribute.getValue());
}
// check them one after the other.
if (att_length < attrsInfo.length) {
for (int i = 0; i < attrsInfo.length; i++) {
MBeanAttributeInfo attributeInfo = attrsInfo[i];
if (!attrs.containsKey(attributeInfo.getName()) && !viewableAttrs.containsKey(attributeInfo.getName()) && !unavailableAttrs.containsKey(attributeInfo.getName())) {
if (attributeInfo.isReadable()) {
// went wrong.
try {
Object v = mbean.getMBeanServerConnection().getAttribute(mbean.getObjectName(), attributeInfo.getName());
//What happens if now it is ok?
// Be pragmatic, add it to readable...
attrs.put(attributeInfo.getName(), v);
} catch (Exception e) {
//Put the exception that will be displayed
// in tooltip
unavailableAttrs.put(attributeInfo.getName(), Utils.getActualException(e).toString());
}
}
}
}
}
} catch (Exception e) {
//sets all attributes unavailable except the writable ones
for (int i = 0; i < attrsInfo.length; i++) {
MBeanAttributeInfo attributeInfo = attrsInfo[i];
if (attributeInfo.isReadable()) {
unavailableAttrs.put(attributeInfo.getName(), Utils.getActualException(e).toString());
}
}
}
//one update at a time
return new Runnable() {
public void run() {
synchronized (XMBeanAttributes.this) {
XMBeanAttributes.this.mbean = mbean;
XMBeanAttributes.this.mbeanInfo = curMBeanInfo;
XMBeanAttributes.this.attributesInfo = attrsInfo;
XMBeanAttributes.this.attributes = attrs;
XMBeanAttributes.this.unavailableAttributes = unavailableAttrs;
XMBeanAttributes.this.viewableAttributes = viewableAttrs;
DefaultTableModel tableModel = (DefaultTableModel) getModel();
// add attribute information
emptyTable(tableModel);
addTableData(tableModel, mbean, attrsInfo, attrs, unavailableAttrs, viewableAttrs);
// update the model with the new data
tableModel.newDataAvailable(new TableModelEvent(tableModel));
// re-register for change events
tableModel.addTableModelListener(attributesListener);
}
}
};
}
use of javax.management.AttributeList in project jdk8u_jdk by JetBrains.
the class AttributeListTypeSafeTest method main.
public static void main(String[] args) throws Exception {
// Test calling asList after adding non-Attribute by various means
for (Op op : Op.values()) {
AttributeList alist = new AttributeList();
alist.add(new Attribute("foo", "bar"));
doOp(alist, op);
String what = "asList() after calling " + op + " with non-Attribute";
try {
List<Attribute> lista = alist.asList();
fail(what + ": succeeded but should not have");
} catch (IllegalArgumentException e) {
System.out.println("OK: " + what + ": got IllegalArgumentException");
}
}
// Test adding non-Attribute by various means after calling asList
for (Op op : Op.values()) {
AttributeList alist = new AttributeList();
List<Attribute> lista = alist.asList();
lista.add(new Attribute("foo", "bar"));
String what = op + " with non-Attribute after calling asList()";
try {
doOp(alist, op);
fail(what + ": succeeded but should not have");
} catch (IllegalArgumentException e) {
System.out.println("OK: " + what + ": got IllegalArgumentException");
}
}
if (failure == null)
System.out.println("TEST PASSED");
else
throw new Exception("TEST FAILED: " + failure);
}
use of javax.management.AttributeList in project jdk8u_jdk by JetBrains.
the class RMIConnectorLogAttributesTest method doTest.
private void doTest(JMXConnector connector) throws IOException, MalformedObjectNameException, ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException {
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
ObjectName objName = new ObjectName("com.redhat.test.jmx:type=NameMBean");
System.out.println("DEBUG: Calling createMBean");
mbsc.createMBean(Name.class.getName(), objName);
System.out.println("DEBUG: Calling setAttributes");
AttributeList attList = new AttributeList();
attList.add(new Attribute("FirstName", ANY_NAME));
attList.add(new Attribute("LastName", ANY_NAME));
mbsc.setAttributes(objName, attList);
}
use of javax.management.AttributeList in project Ardent by adamint.
the class UsageUtils method getProcessCpuLoad.
// Credit http://stackoverflow.com/questions/18489273/how-to-get-percentage-of-cpu-usage-of-os-from-java
public static double getProcessCpuLoad() throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
AttributeList list = mbs.getAttributes(name, new String[] { "ProcessCpuLoad" });
if (list.isEmpty())
return Double.NaN;
Attribute att = (Attribute) list.get(0);
Double value = (Double) att.getValue();
// usually takes a couple of seconds before we get real values
if (value == -1.0)
return Double.NaN;
// returns a percentage value with 1 decimal point precision
return ((int) (value * 1000) / 10.0);
}
use of javax.management.AttributeList in project geode by apache.
the class MX4JModelMBean method getAttributes.
public AttributeList getAttributes(String[] attributes) {
if (attributes == null)
throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_NAMES_CANNOT_BE_NULL.toLocalizedString()));
Logger logger = getLogger();
AttributeList list = new AttributeList();
for (int i = 0; i < attributes.length; ++i) {
String attrName = attributes[i];
Attribute attribute = null;
try {
Object value = getAttribute(attrName);
attribute = new Attribute(attrName, value);
list.add(attribute);
} catch (Exception x) {
if (logger.isEnabledFor(Logger.TRACE))
logger.trace("getAttribute for attribute " + attrName + " failed", x);
// And go on with the next attribute
}
}
return list;
}
Aggregations