Search in sources :

Example 26 with Attribute

use of javax.management.Attribute in project neo4j by neo4j.

the class Dbinfo method printAttribute.

private void printAttribute(JSONObject json, Object value) throws RemoteException, ShellException {
    try {
        Attribute attribute = (Attribute) value;
        Object attributeValue = attribute.getValue();
        if (attributeValue != null && attributeValue.getClass().isArray()) {
            Object[] arrayValue = (Object[]) attributeValue;
            JSONArray array = new JSONArray();
            for (Object item : (Object[]) arrayValue) {
                if (item instanceof CompositeData) {
                    array.put(compositeDataAsMap((CompositeData) item));
                } else {
                    array.put(item.toString());
                }
            }
            json.put(attribute.getName(), array);
        } else {
            json.put(attribute.getName(), attributeValue);
        }
    } catch (JSONException e) {
        throw ShellException.wrapCause(e);
    }
}
Also used : Attribute(javax.management.Attribute) CompositeData(javax.management.openmbean.CompositeData) JSONArray(org.neo4j.shell.util.json.JSONArray) JSONException(org.neo4j.shell.util.json.JSONException) JSONObject(org.neo4j.shell.util.json.JSONObject)

Example 27 with Attribute

use of javax.management.Attribute in project spring-framework by spring-projects.

the class MBeanClientInterceptor method invokeAttribute.

private Object invokeAttribute(PropertyDescriptor pd, MethodInvocation invocation) throws JMException, IOException {
    String attributeName = JmxUtils.getAttributeName(pd, this.useStrictCasing);
    MBeanAttributeInfo inf = this.allowedAttributes.get(attributeName);
    // management interface.
    if (inf == null) {
        throw new InvalidInvocationException("Attribute '" + pd.getName() + "' is not exposed on the management interface");
    }
    if (invocation.getMethod().equals(pd.getReadMethod())) {
        if (inf.isReadable()) {
            return this.serverToUse.getAttribute(this.objectName, attributeName);
        } else {
            throw new InvalidInvocationException("Attribute '" + attributeName + "' is not readable");
        }
    } else if (invocation.getMethod().equals(pd.getWriteMethod())) {
        if (inf.isWritable()) {
            this.serverToUse.setAttribute(this.objectName, new Attribute(attributeName, invocation.getArguments()[0]));
            return null;
        } else {
            throw new InvalidInvocationException("Attribute '" + attributeName + "' is not writable");
        }
    } else {
        throw new IllegalStateException("Method [" + invocation.getMethod() + "] is neither a bean property getter nor a setter");
    }
}
Also used : Attribute(javax.management.Attribute) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 28 with Attribute

use of javax.management.Attribute in project spring-framework by spring-projects.

the class MBeanExporterTests method testWithExposeClassLoader.

@Test
public void testWithExposeClassLoader() throws Exception {
    String name = "Rob Harrop";
    String otherName = "Juergen Hoeller";
    JmxTestBean bean = new JmxTestBean();
    bean.setName(name);
    ObjectName objectName = ObjectNameManager.getInstance("spring:type=Test");
    Map<String, Object> beans = new HashMap<>();
    beans.put(objectName.toString(), bean);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(getServer());
    exporter.setBeans(beans);
    exporter.setExposeManagedResourceClassLoader(true);
    start(exporter);
    assertIsRegistered("Bean instance not registered", objectName);
    Object result = server.invoke(objectName, "add", new Object[] { new Integer(2), new Integer(3) }, new String[] { int.class.getName(), int.class.getName() });
    assertEquals("Incorrect result return from add", result, new Integer(5));
    assertEquals("Incorrect attribute value", name, server.getAttribute(objectName, "Name"));
    server.setAttribute(objectName, new Attribute("Name", otherName));
    assertEquals("Incorrect updated name.", otherName, bean.getName());
}
Also used : HashMap(java.util.HashMap) Attribute(javax.management.Attribute) JmxTestBean(org.springframework.jmx.JmxTestBean) IJmxTestBean(org.springframework.jmx.IJmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 29 with Attribute

use of javax.management.Attribute in project spring-framework by spring-projects.

the class NotificationListenerTests method testRegisterNotificationListenerWithBeanNameBeforeObjectNameMappedToSameBeanInstance.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRegisterNotificationListenerWithBeanNameBeforeObjectNameMappedToSameBeanInstance() throws Exception {
    String beanName = "testBean";
    ObjectName objectName = ObjectName.getInstance("spring:name=Test");
    SelfNamingTestBean testBean = new SelfNamingTestBean();
    testBean.setObjectName(objectName);
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.registerSingleton(beanName, testBean);
    Map<String, Object> beans = new HashMap<>();
    beans.put(beanName, testBean);
    Map listenerMappings = new HashMap();
    CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
    listenerMappings.put(beanName, listener);
    listenerMappings.put(objectName, listener);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListenerMappings(listenerMappings);
    exporter.setBeanFactory(factory);
    start(exporter);
    assertIsRegistered("Should have registered MBean", objectName);
    server.setAttribute(objectName, new Attribute("Age", new Integer(77)));
    assertEquals("Listener should have been notified exactly once", 1, listener.getCount("Age"));
}
Also used : HashMap(java.util.HashMap) Attribute(javax.management.Attribute) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) HashMap(java.util.HashMap) Map(java.util.Map) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 30 with Attribute

use of javax.management.Attribute in project spring-framework by spring-projects.

the class NotificationListenerTests method testRegisterNotificationListenerWithHandback.

@Test
public void testRegisterNotificationListenerWithHandback() throws Exception {
    String objectName = "spring:name=Test";
    JmxTestBean bean = new JmxTestBean();
    Map<String, Object> beans = new HashMap<>();
    beans.put(objectName, bean);
    CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
    Object handback = new Object();
    NotificationListenerBean listenerBean = new NotificationListenerBean();
    listenerBean.setNotificationListener(listener);
    listenerBean.setMappedObjectName("spring:name=Test");
    listenerBean.setHandback(handback);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean });
    start(exporter);
    // update the attribute
    String attributeName = "Name";
    server.setAttribute(ObjectNameManager.getInstance("spring:name=Test"), new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener not notified", 1, listener.getCount(attributeName));
    assertEquals("Handback object not transmitted correctly", handback, listener.getLastHandback(attributeName));
}
Also used : HashMap(java.util.HashMap) Attribute(javax.management.Attribute) JmxTestBean(org.springframework.jmx.JmxTestBean) 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