Search in sources :

Example 41 with Attribute

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();
    }
}
Also used : Attribute(javax.management.Attribute) ObjectName(javax.management.ObjectName)

Example 42 with Attribute

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;
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList)

Example 43 with Attribute

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;
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) ReflectionException(javax.management.ReflectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OpenDataException(javax.management.openmbean.OpenDataException) AttributeNotFoundException(javax.management.AttributeNotFoundException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException)

Example 44 with Attribute

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);
        }
    }
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) Attribute(javax.management.Attribute) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) AttributeNotFoundException(javax.management.AttributeNotFoundException) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ListenerNotFoundException(javax.management.ListenerNotFoundException) ImplementationException(mx4j.ImplementationException) RuntimeOperationsException(javax.management.RuntimeOperationsException) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) Descriptor(javax.management.Descriptor) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 45 with Attribute

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"));
        }
    }
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) MBeanOperationInfo(javax.management.MBeanOperationInfo) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) TabularData(javax.management.openmbean.TabularData) Iterator(java.util.Iterator) NotificationListener(javax.management.NotificationListener) MBeanParameterInfo(javax.management.MBeanParameterInfo) Test(org.junit.Test)

Aggregations

Attribute (javax.management.Attribute)157 ObjectName (javax.management.ObjectName)94 MBeanServer (javax.management.MBeanServer)56 AttributeList (javax.management.AttributeList)46 Test (org.junit.Test)38 ReflectionException (javax.management.ReflectionException)29 MBeanException (javax.management.MBeanException)25 HashMap (java.util.HashMap)23 InstanceNotFoundException (javax.management.InstanceNotFoundException)22 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)21 AttributeNotFoundException (javax.management.AttributeNotFoundException)20 MBeanServerConnection (javax.management.MBeanServerConnection)15 MBeanInfo (javax.management.MBeanInfo)14 JMXConnector (javax.management.remote.JMXConnector)13 RuntimeOperationsException (javax.management.RuntimeOperationsException)12 List (java.util.List)10 BacklogTracerEventMessage (org.apache.camel.api.management.mbean.BacklogTracerEventMessage)10 IOException (java.io.IOException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 ArrayList (java.util.ArrayList)9