use of javax.management.Attribute in project tdi-studio-se by Talend.
the class CpuProfiler method suspend.
/*
* @see ICpuProfiler#suspend()
*/
@Override
public void suspend() throws JvmCoreException {
if (type == ProfilerType.BCI) {
validateAgent();
ObjectName objectName = jvm.getMBeanServer().getObjectName(PROFILER_MXBEAN_NAME);
if (objectName != null) {
jvm.getMBeanServer().setAttribute(objectName, new Attribute(RUNNING, false));
}
} else {
jvm.getMBeanServer().suspendSampling();
}
}
use of javax.management.Attribute in project cloudstack by apache.
the class PropertyMapDynamicBean method setAttributes.
@Override
public synchronized AttributeList setAttributes(AttributeList list) {
Attribute[] attrs = list.toArray(new Attribute[0]);
AttributeList retList = new AttributeList();
for (Attribute attr : attrs) {
String name = attr.getName();
Object value = attr.getValue();
_propMap.put(name, value);
retList.add(new Attribute(name, value));
}
return retList;
}
use of javax.management.Attribute in project deltaspike by apache.
the class DynamicMBeanWrapper method setAttributes.
@Override
public AttributeList setAttributes(final AttributeList attributes) {
final AttributeList list = new AttributeList();
for (Object o : attributes) {
final Attribute attr = (Attribute) o;
try {
setAttribute(attr);
list.add(attr);
} catch (Exception ignore) {
// no-op
}
}
return list;
}
use of javax.management.Attribute in project geode by apache.
the class MX4JModelMBean method setAttribute.
public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
if (attribute == null)
throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_CANNOT_BE_NULL.toLocalizedString()));
Logger logger = getLogger();
// No need to synchronize: I work mostly on clones
// I want the real info, not its clone
ModelMBeanInfo info = getModelMBeanInfo();
if (info == null)
throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_MODELMBEANINFO_IS_NULL.toLocalizedString());
if (logger.isEnabledFor(Logger.DEBUG))
logger.debug("ModelMBeanInfo is: " + info);
String attrName = attribute.getName();
Object attrValue = attribute.getValue();
// This is a clone, we use it read only
ModelMBeanAttributeInfo attrInfo = info.getAttribute(attrName);
if (attrInfo == null)
throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_CANNOT_FIND_MODELMBEANATTRIBUTEINFO_FOR_ATTRIBUTE_0.toLocalizedString(attrName));
if (logger.isEnabledFor(Logger.DEBUG))
logger.debug("Attribute info is: " + attrInfo);
if (!attrInfo.isWritable())
throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_0_IS_NOT_WRITABLE.toLocalizedString(attrName));
// This returns a clone of the mbean descriptor, we use it read only
Descriptor mbeanDescriptor = info.getMBeanDescriptor();
if (mbeanDescriptor == null)
throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_MBEAN_DESCRIPTOR_CANNOT_BE_NULL.toLocalizedString());
if (logger.isEnabledFor(Logger.DEBUG))
logger.debug("MBean descriptor is: " + mbeanDescriptor);
// This descriptor is a clone
Descriptor attributeDescriptor = attrInfo.getDescriptor();
if (attributeDescriptor == null)
throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_DESCRIPTOR_FOR_ATTRIBUTE_0_CANNOT_BE_NULL.toLocalizedString(attrName));
if (logger.isEnabledFor(Logger.DEBUG))
logger.debug("Attribute descriptor is: " + attributeDescriptor);
String lastUpdateField = "lastUpdatedTimeStamp";
Object oldValue = null;
try {
oldValue = getAttribute(attrName);
if (logger.isEnabledFor(Logger.DEBUG))
logger.debug("Previous value of attribute " + attrName + ": " + oldValue);
} catch (Exception x) {
if (logger.isEnabledFor(Logger.DEBUG))
logger.debug("Cannot get previous value of attribute " + attrName, x);
}
// Check if setMethod is present
String method = (String) attributeDescriptor.getFieldValue("setMethod");
if (logger.isEnabledFor(Logger.DEBUG))
logger.debug("setMethod field is: " + method);
if (method != null) {
Class declared = loadClassWithContextClassLoader(attrInfo.getType());
if (attrValue != null) {
Class parameter = attrValue.getClass();
checkAssignability(parameter, declared);
}
// As an extension, allow attributes to be called on target objects also
Object target = resolveTargetObject(attributeDescriptor);
invokeMethod(target, method, new Class[] { declared }, new Object[] { attrValue });
// Cache the value only if currencyTimeLimit is not 0, ie it is not always stale
int staleness = getStaleness(attributeDescriptor, mbeanDescriptor, lastUpdateField);
if (staleness != ALWAYS_STALE) {
attributeDescriptor.setField("value", attrValue);
attributeDescriptor.setField(lastUpdateField, Long.valueOf(System.currentTimeMillis()));
if (logger.isEnabledFor(Logger.TRACE))
logger.trace("Attribute's value has been cached");
} else {
if (logger.isEnabledFor(Logger.TRACE))
logger.trace("Always stale, avoiding to cache attribute's value");
}
} else {
if (attrValue != null) {
Class parameter = attrValue.getClass();
Class declared = loadClassWithContextClassLoader(attrInfo.getType());
checkAssignability(parameter, declared);
}
// Always store the value in the descriptor: no setMethod
attributeDescriptor.setField("value", attrValue);
}
// And now replace the descriptor with the updated clone
info.setDescriptor(attributeDescriptor, "attribute");
// Send notifications to listeners
if (logger.isEnabledFor(Logger.TRACE))
logger.trace("Sending attribute change notifications");
sendAttributeChangeNotification(new Attribute(attrName, oldValue), attribute);
// Persist this ModelMBean
boolean persistNow = shouldPersistNow(attributeDescriptor, mbeanDescriptor, lastUpdateField);
if (persistNow) {
if (logger.isEnabledFor(Logger.TRACE))
logger.trace("Persisting this ModelMBean...");
try {
store();
if (logger.isEnabledFor(Logger.TRACE))
logger.trace("ModelMBean persisted successfully");
} catch (Exception x) {
logger.error(LocalizedStrings.MX4JModelMBean_CANNOT_STORE_MODELMBEAN_AFTER_SETATTRIBUTE, x);
if (x instanceof MBeanException)
throw (MBeanException) x;
else
throw new MBeanException(x);
}
}
}
use of javax.management.Attribute in project deltaspike by apache.
the class SimpleRegistrationTest method checkMBean.
@Test
public void checkMBean() throws Exception {
assertEquals(0, myMBean.getCounter());
myMBean.resetTo(2);
final ObjectName on = new ObjectName("org.apache.deltaspike:type=MBeans,name=" + MyMBean.class.getName());
assertTrue(server.isRegistered(on));
assertEquals(2, server.getAttribute(on, "counter"));
assertEquals(6, server.invoke(on, "multiply", new Object[] { 3 }, new String[0]));
myMBean.resetTo(5);
assertEquals(5, server.getAttribute(on, "counter"));
assertEquals(20, server.invoke(on, "multiply", new Object[] { 4 }, new String[0]));
server.setAttribute(on, new Attribute("counter", 10));
assertEquals(10, myMBean.getCounter());
final Collection<Notification> notifications = new ArrayList<Notification>();
server.addNotificationListener(on, new NotificationListener() {
@Override
public void handleNotification(final Notification notification, final Object handback) {
notifications.add(notification);
}
}, null, null);
myMBean.broadcast();
assertEquals(1, notifications.size());
assertEquals(10L, notifications.iterator().next().getSequenceNumber());
MBeanInfo mBeanInfo = server.getMBeanInfo(on);
Assert.assertNotNull(mBeanInfo);
MBeanOperationInfo[] operations = mBeanInfo.getOperations();
Assert.assertNotNull(operations);
Assert.assertTrue(operations.length > 0);
Assert.assertTrue("Empty Signature on operation: " + operations[1], operations[1].getSignature().length > 0);
MBeanParameterInfo parameterInfo = operations[1].getSignature()[0];
assertEquals("multiplier", parameterInfo.getName());
assertEquals("the multiplier", parameterInfo.getDescription());
{
// table support - through map
Object table = server.getAttribute(on, "table");
assertTrue(TabularData.class.isInstance(table));
final TabularData data = TabularData.class.cast(table);
assertEquals(1, data.size());
final CompositeData compositeData = CompositeData.class.cast(data.values().iterator().next());
assertEquals(2, compositeData.values().size());
assertEquals("value1", compositeData.get("key1"));
assertEquals("value2", compositeData.get("key2"));
}
{
// table support - through Table
Object table = server.getAttribute(on, "table2");
assertTrue(TabularData.class.isInstance(table));
final TabularData data = TabularData.class.cast(table);
assertEquals(2, data.size());
final Iterator<?> iterator = data.values().iterator();
{
final CompositeData compositeData = CompositeData.class.cast(iterator.next());
assertEquals(3, compositeData.values().size());
assertEquals("1", compositeData.get("a"));
assertEquals("2", compositeData.get("b"));
assertEquals("3", compositeData.get("c"));
}
{
final CompositeData compositeData = CompositeData.class.cast(iterator.next());
assertEquals(3, compositeData.values().size());
assertEquals("alpha", compositeData.get("a"));
assertEquals("beta", compositeData.get("b"));
assertEquals("gamma", compositeData.get("c"));
}
}
}
Aggregations