Search in sources :

Example 76 with Attribute

use of javax.management.Attribute in project tomcat by apache.

the class JMXAccessorSetTask method jmxSet.

/**
     * Set property value.
     *
     * @param jmxServerConnection Connection to the JMX server
     * @param name The MBean name
     * @return null (no error message to report other than exception)
     * @throws Exception An error occurred
     */
protected String jmxSet(MBeanServerConnection jmxServerConnection, String name) throws Exception {
    Object realValue;
    if (type != null) {
        realValue = convertStringToType(value, type);
    } else {
        if (isConvert()) {
            String mType = getMBeanAttributeType(jmxServerConnection, name, attribute);
            realValue = convertStringToType(value, mType);
        } else
            realValue = value;
    }
    jmxServerConnection.setAttribute(new ObjectName(name), new Attribute(attribute, realValue));
    return null;
}
Also used : Attribute(javax.management.Attribute) ObjectName(javax.management.ObjectName)

Example 77 with Attribute

use of javax.management.Attribute in project jvm-tools by aragozin.

the class MBeanHelper method set.

public void set(ObjectName bean, String attr, String value) throws Exception {
    MBeanInfo mbinfo = mserver.getMBeanInfo(bean);
    MBeanAttributeInfo ai = attrInfo(mbinfo, attr);
    if (ai == null) {
        throw new IllegalArgumentException("No such attribute '" + attr + "'");
    }
    if (!ai.isWritable()) {
        throw new IllegalArgumentException("Attribute '" + attr + "' is not writeable");
    }
    String type = ai.getType();
    Object ov = convert(value, type);
    mserver.setAttribute(bean, new Attribute(attr, ov));
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 78 with Attribute

use of javax.management.Attribute in project jetty.project by eclipse.

the class ObjectMBeanUtilTest method getAttributes.

private AttributeList getAttributes(String name, String value) {
    Attribute attribute = new Attribute(name, value);
    AttributeList attributes = new AttributeList();
    attributes.add(attribute);
    return attributes;
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList)

Example 79 with Attribute

use of javax.management.Attribute in project jetty.project by eclipse.

the class ObjectMBeanUtilTest method setUpGetAttribute.

private void setUpGetAttribute(String property, String value) throws Exception {
    Attribute attribute = new Attribute(property, value);
    objectMBean.setAttribute(attribute);
}
Also used : Attribute(javax.management.Attribute)

Example 80 with Attribute

use of javax.management.Attribute in project jetty.project by eclipse.

the class DoSFilterJMXTest method testDoSFilterJMX.

@Test
public void testDoSFilterJMX() throws Exception {
    Server server = new Server();
    Connector connector = new ServerConnector(server);
    server.addConnector(connector);
    ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    DoSFilter filter = new DoSFilter();
    FilterHolder holder = new FilterHolder(filter);
    String name = "dos";
    holder.setName(name);
    holder.setInitParameter(DoSFilter.MANAGED_ATTR_INIT_PARAM, "true");
    context.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));
    context.setInitParameter(ServletContextHandler.MANAGED_ATTRIBUTES, name);
    MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
    MBeanContainer mbeanContainer = new MBeanContainer(mbeanServer);
    server.addBean(mbeanContainer);
    server.start();
    String domain = DoSFilter.class.getPackage().getName();
    Set<ObjectName> mbeanNames = mbeanServer.queryNames(ObjectName.getInstance(domain + ":*"), null);
    Assert.assertEquals(1, mbeanNames.size());
    ObjectName objectName = mbeanNames.iterator().next();
    boolean value = (Boolean) mbeanServer.getAttribute(objectName, "enabled");
    mbeanServer.setAttribute(objectName, new Attribute("enabled", !value));
    Assert.assertEquals(!value, filter.isEnabled());
    String whitelist = (String) mbeanServer.getAttribute(objectName, "whitelist");
    String address = "127.0.0.1";
    Assert.assertFalse(whitelist.contains(address));
    boolean result = (Boolean) mbeanServer.invoke(objectName, "addWhitelistAddress", new Object[] { address }, new String[] { String.class.getName() });
    Assert.assertTrue(result);
    whitelist = (String) mbeanServer.getAttribute(objectName, "whitelist");
    Assert.assertTrue(whitelist.contains(address));
    result = (Boolean) mbeanServer.invoke(objectName, "removeWhitelistAddress", new Object[] { address }, new String[] { String.class.getName() });
    Assert.assertTrue(result);
    whitelist = (String) mbeanServer.getAttribute(objectName, "whitelist");
    Assert.assertFalse(whitelist.contains(address));
    server.stop();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) MBeanServer(javax.management.MBeanServer) Server(org.eclipse.jetty.server.Server) Attribute(javax.management.Attribute) ObjectName(javax.management.ObjectName) ServerConnector(org.eclipse.jetty.server.ServerConnector) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) MBeanServer(javax.management.MBeanServer) 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