Example usage for javax.management MBeanServerConnection setAttribute

List of usage examples for javax.management MBeanServerConnection setAttribute

Introduction

In this page you can find the example usage for javax.management MBeanServerConnection setAttribute.

Prototype

public void setAttribute(ObjectName name, Attribute attribute)
        throws InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException,
        MBeanException, ReflectionException, IOException;

Source Link

Document

Sets the value of a specific attribute of a named MBean.

Usage

From source file:org.hyperic.hq.plugin.jboss.JBossUtil.java

private static Object setAttribute(MBeanServerConnection mServer, ObjectName obj, String name, Object value)
        throws MetricUnreachableException, MetricNotFoundException, PluginException, ReflectionException,
        InstanceNotFoundException, MBeanException, IOException {

    if (name.startsWith("set")) {
        name = name.substring(3);//ww w. j  a  va2 s.c  o  m
    }

    Attribute attr = new Attribute(name, value);

    try {
        mServer.setAttribute(obj, attr);
    } catch (AttributeNotFoundException e) {
        throw new MetricNotFoundException(e.getMessage(), e);
    } catch (InvalidAttributeValueException e) {
        throw new ReflectionException(e);
    }

    return null;
}

From source file:com.custardsource.maven.plugins.jmx.SetAttribute.java

@Override
public Object execute(MBeanServerConnection connection) throws Exception {
    ObjectName name = new ObjectName(objectName);
    ConvertUtilsBean converter = new ConvertUtilsBean();

    Object attributeValue = converter.convert(value, ClassUtils.getClass(type));
    connection.setAttribute(name, new Attribute(attributeName, attributeValue));
    return connection.getAttribute(name, attributeName);
}

From source file:org.jboss.as.test.integration.domain.rbac.JmxRBACProviderHostScopedRolesTestCase.java

private void setAttribute(String userName, JmxManagementInterface jmx) throws Exception {
    boolean successExpected = isWriteAllowed(userName);

    MBeanServerConnection connection = jmx.getConnection();
    ObjectName domain = new ObjectName("java.lang:type=Memory");
    try {//from   w  w w.  j av  a 2s  .  c o m
        connection.setAttribute(domain, new Attribute("Verbose", true));
        connection.setAttribute(domain, new Attribute("Verbose", false)); // back to default to not pollute the logs
        assertTrue("Failure was expected", successExpected);
    } catch (JMRuntimeException e) {
        if (e.getMessage().contains("WFLYJMX0037")) {
            assertFalse("Success was expected but failure happened: " + e, successExpected);
        } else {
            throw e;
        }
    }
}

From source file:org.jminix.console.resource.AttributeResource.java

@Override
public void acceptRepresentation(Representation entity) throws ResourceException {
    String value = new Form(entity).getFirstValue("value");

    String domain = getRequest().getAttributes().get("domain").toString();

    String mbean = new EncoderBean().decode(getRequest().getAttributes().get("mbean").toString());

    String attributeName = new EncoderBean().decode(getRequest().getAttributes().get("attribute").toString());

    MBeanServerConnection server = getServer();

    try {// ww  w. j ava 2  s.  c  om

        String type = "java.lang.String";
        for (MBeanAttributeInfo info : server.getMBeanInfo(new ObjectName(domain + ":" + mbean))
                .getAttributes()) {
            if (info.getName().equals(attributeName)) {
                type = info.getType();
            }
        }

        Object attribute = new ValueParser().parse(value, type);

        if (attribute != null) {
            server.setAttribute(new ObjectName(domain + ":" + mbean), new Attribute(attributeName, attribute));
        }

        String queryString = getQueryString();
        if (queryString == null) {
            queryString = "?";
        }
        queryString += "ok=1";
        getResponse().redirectPermanent(new EncoderBean().encode(attributeName) + queryString);
    } catch (InstanceNotFoundException e) {
        throw new RuntimeException(e);
    } catch (AttributeNotFoundException e) {
        throw new RuntimeException(e);
    } catch (InvalidAttributeValueException e) {
        throw new RuntimeException(e);
    } catch (MalformedObjectNameException e) {
        throw new RuntimeException(e);
    } catch (MBeanException e) {
        throw new RuntimeException(e);
    } catch (ReflectionException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }

}

From source file:io.fabric8.test.smoke.JolokiaEndpointTestBase.java

@Test
public void testMXBeanEndpoint() throws Exception {

    ContainerManager cntManager = ContainerManagerLocator.getContainerManager();
    Container cnt = cntManager.getCurrentContainer();

    ServiceEndpoint sep = cnt.getServiceEndpoint(URLServiceEndpoint.JMX_SERVICE_ENDPOINT_IDENTITY);
    JMXServiceEndpoint jmxEndpoint = sep.adapt(JMXServiceEndpoint.class);
    String serviceURL = jmxEndpoint.getServiceURL();
    Assert.assertNotNull("JMX URL not null", serviceURL);

    // Get the local MBeanServer
    MBeanServer server = ServiceLocator.getRequiredService(MBeanServer.class);
    server.registerMBean(new Simple(), SimpleMXBean.OBJECT_NAME);
    try {/*from  ww  w . j  a  v a2s.  c  o m*/
        String[] userpass = RuntimeType.KARAF == RuntimeType.getRuntimeType() ? karafJmx : otherJmx;
        JMXConnector jmxConnector = jmxEndpoint.getJMXConnector(userpass[0], userpass[1], 200,
                TimeUnit.MILLISECONDS);
        MBeanServerConnection con = jmxConnector.getMBeanServerConnection();
        try {
            // Simple string echo
            Object[] params = new Object[] { "Kermit" };
            String[] signature = new String[] { String.class.getName() };
            Object result = con.invoke(SimpleMXBean.OBJECT_NAME, "echo", params, signature);
            Assert.assertEquals("Hello: Kermit", result);

            // Set Bean attribute using CompositeData
            Bean bean = new Bean("Hello", "Foo");
            CompositeData cdata = OpenTypeGenerator.toCompositeData(bean);
            con.setAttribute(SimpleMXBean.OBJECT_NAME, new Attribute("Bean", cdata));

            // Get Bean attribute using CompositeData
            cdata = (CompositeData) con.getAttribute(SimpleMXBean.OBJECT_NAME, "Bean");
            Assert.assertEquals(bean, OpenTypeGenerator.fromCompositeData(Bean.class, cdata));

            // Simple Bean echo using CompositeData
            params = new Object[] { cdata };
            signature = new String[] { CompositeData.class.getName() };
            cdata = (CompositeData) con.invoke(SimpleMXBean.OBJECT_NAME, "echoBean", params, signature);
            Assert.assertEquals(bean, OpenTypeGenerator.fromCompositeData(Bean.class, cdata));
        } finally {
            jmxConnector.close();
        }
    } finally {
        server.unregisterMBean(SimpleMXBean.OBJECT_NAME);
    }
}

From source file:org.hyperic.hq.plugin.weblogic.WeblogicServiceControlPlugin.java

protected Object invoke(MBeanServerConnection mServer, String objectName, String method, Object[] args,
        String[] sig) throws MetricUnreachableException, MetricNotFoundException, PluginException {

    ObjectName obj;//from  ww  w.  j  a v a  2s.  c  o m
    try {
        obj = new ObjectName(objectName);
    } catch (MalformedObjectNameException e1) {
        throw new PluginException("Unable to create an ObjectName from " + objectName);
    }

    MBeanInfo info;
    try {
        info = mServer.getMBeanInfo(obj);
    } catch (Exception e1) {
        throw new PluginException("Unable to obtain MBeanInfo from " + objectName);
    }

    if (sig.length == 0) {
        MBeanUtil.OperationParams params = MBeanUtil.getOperationParams(info, method, args);
        if (params.isAttribute) {
            if (method.startsWith("set")) {
                try {
                    mServer.setAttribute(obj, new Attribute(method.substring(3), params.arguments[0]));
                } catch (AttributeNotFoundException e) {
                    throw new MetricNotFoundException(e.getMessage(), e);
                } catch (Exception e) {
                    throw new PluginException(e);
                }
                return null;
            } else {
                try {
                    return mServer.getAttribute(obj, method.substring(3));
                } catch (AttributeNotFoundException e) {
                    throw new MetricNotFoundException(e.getMessage(), e);
                } catch (Exception e) {
                    throw new PluginException(e);
                }
            }
        }
        sig = params.signature;
        args = params.arguments;
    }

    try {
        return mServer.invoke(obj, method, args, sig);
    } catch (Exception e) {
        throw new PluginException(e);
    }
}

From source file:com.betfair.testing.utils.cougar.helpers.CougarHelpers.java

public void setJMXMBeanAttributeValue(String mBeanName, String attributeName, Object value) {

    try {/*from  www .j a  v a  2 s  .c  o m*/
        MBeanServerConnection mBeanServerConnection = getJMXConnection();
        ObjectName mbeanName = new ObjectName(mBeanName);
        Attribute attr = new Attribute(attributeName, value);
        mBeanServerConnection.setAttribute(mbeanName, attr);

    } catch (Exception e) {
        throw new RuntimeException(JMX_RETRIEVAL_ERROR + mBeanName + ": " + attributeName, e);
    }
}

From source file:com.stumbleupon.hbaseadmin.JMXQuery.java

protected Object doAttributeOperation(MBeanServerConnection mbsc, ObjectInstance instance, String command,
        MBeanAttributeInfo[] infos) throws Exception {
    final CommandParse parse = new CommandParse(command);

    if ((parse.getArgs() == null) || (parse.getArgs().length == 0)) {
        return mbsc.getAttribute(instance.getObjectName(), parse.getCmd());
    }/*from ww w  .ja  v  a 2s .c o  m*/

    if (parse.getArgs().length != 1) {
        throw new IllegalArgumentException("One only argument setting attribute values: " + parse.getArgs());
    }

    final MBeanAttributeInfo info = (MBeanAttributeInfo) getFeatureInfo(infos, parse.getCmd());

    final Constructor c = getResolvedClass(info.getType()).getConstructor(new Class[] { String.class });

    final Attribute a = new Attribute(parse.getCmd(), c.newInstance(new Object[] { parse.getArgs()[0] }));

    mbsc.setAttribute(instance.getObjectName(), a);
    return null;
}

From source file:com.betfair.testing.utils.cougar.helpers.CougarHelpers.java

private void setJMXMBeanAttribute(String mBeanName, String attributeName, Object newMbeanValue) {

    try {//from  ww w.ja  v a2 s .  co m
        MBeanServerConnection mBeanServerConnection = getJMXConnection();
        ObjectName mbeanName = new ObjectName(mBeanName);
        Object currentAttributeValue = mBeanServerConnection.getAttribute(mbeanName, attributeName);
        Object reflectedValue = reflect.getRealProperty(currentAttributeValue.getClass(), newMbeanValue);
        Attribute attribute = new Attribute(attributeName, reflectedValue);
        mBeanServerConnection.setAttribute(mbeanName, attribute);
    } catch (Exception e) {
        throw new RuntimeException(JMX_SETTING_ERROR + mBeanName + ": " + attributeName, e);
    }
}

From source file:org.helios.collector.jmx.connection.AbstractMBeanServerConnectionFactory.java

/**
 * Sets the value of an attribute//from   w  ww . jav a2s .co m
 * @param objectName
 * @param attribute
 * @throws InstanceNotFoundException
 * @throws AttributeNotFoundException
 * @throws InvalidAttributeValueException
 * @throws MBeanException
 * @throws ReflectionException
 * @throws IOException
 */
@Override
@ManagedOperation
public void setAttribute(ObjectName objectName, Attribute attribute)
        throws InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException,
        MBeanException, ReflectionException, IOException {
    validateConn();
    MBeanServerConnection conn = null;
    try {
        conn = getPooledConnection();
        conn.setAttribute(objectName, attribute);
    } catch (MBeanServerConnectionFactoryException e) {
        throw new RuntimeException("Failed to get pooled connection", e);
    } finally {
        try {
            this.returnPooledConnection(conn);
        } catch (Exception e) {
            log.debug(e.getMessage());
        }
    }

}