javax.management.MBeanServerConnection#setAttribute ( )源码实例Demo

下面列出了javax.management.MBeanServerConnection#setAttribute ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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 {
        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;
        }
    }
}
 
源代码2 项目: jdk1.8-source-analysis   文件: MXBeanProxy.java
@Override
Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
        throws Exception {
    assert(args.length == 1);
    Attribute attr = new Attribute(getName(), args[0]);
    mbsc.setAttribute(name, attr);
    return null;
}
 
源代码3 项目: dragonwell8_jdk   文件: MXBeanProxy.java
@Override
Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
        throws Exception {
    assert(args.length == 1);
    Attribute attr = new Attribute(getName(), args[0]);
    mbsc.setAttribute(name, attr);
    return null;
}
 
源代码4 项目: dragonwell8_jdk   文件: AuthorizationTest.java
protected int doSetRequest(MBeanServerConnection mbsc,
                           ObjectName on,
                           boolean expectedException) {
    int errorCount = 0;

    try {
        Utils.debug(Utils.DEBUG_STANDARD,
            "ClientSide::doSetRequest: Set attributes of the MBean") ;

        Attribute attribute = new Attribute("Attribute", "My value") ;
        mbsc.setAttribute(on, attribute) ;

        if (expectedException) {
            System.out.println("ClientSide::doSetRequest: " +
                "(ERROR) Set did not fail with expected SecurityException");
            errorCount++;
        } else {
            System.out.println("ClientSide::doSetRequest: (OK) Set succeed") ;
        }
    } catch(Exception e) {
        Utils.printThrowable(e, true) ;
        if (expectedException) {
            if (e instanceof java.lang.SecurityException) {
                System.out.println("ClientSide::doSetRequest: " +
                    "(OK) Set failed with expected SecurityException") ;
            } else {
                System.out.println("ClientSide::doSetRequest: " +
                    "(ERROR) Set failed with " +
                    e.getClass() + " instead of expected SecurityException");
                errorCount++;
            }
        } else {
            System.out.println("ClientSide::doSetRequest: (ERROR) Set failed");
            errorCount++;
        }
    }
    return errorCount;
}
 
源代码5 项目: openjdk-8-source   文件: MXBeanProxy.java
@Override
Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
        throws Exception {
    assert(args.length == 1);
    Attribute attr = new Attribute(getName(), args[0]);
    mbsc.setAttribute(name, attr);
    return null;
}
 
源代码6 项目: openjdk-jdk8u-backup   文件: MXBeanProxy.java
@Override
Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
        throws Exception {
    assert(args.length == 1);
    Attribute attr = new Attribute(getName(), args[0]);
    mbsc.setAttribute(name, attr);
    return null;
}
 
源代码7 项目: TencentKona-8   文件: AuthorizationTest.java
protected int doSetRequest(MBeanServerConnection mbsc,
                           ObjectName on,
                           boolean expectedException) {
    int errorCount = 0;

    try {
        Utils.debug(Utils.DEBUG_STANDARD,
            "ClientSide::doSetRequest: Set attributes of the MBean") ;

        Attribute attribute = new Attribute("Attribute", "My value") ;
        mbsc.setAttribute(on, attribute) ;

        if (expectedException) {
            System.out.println("ClientSide::doSetRequest: " +
                "(ERROR) Set did not fail with expected SecurityException");
            errorCount++;
        } else {
            System.out.println("ClientSide::doSetRequest: (OK) Set succeed") ;
        }
    } catch(Exception e) {
        Utils.printThrowable(e, true) ;
        if (expectedException) {
            if (e instanceof java.lang.SecurityException) {
                System.out.println("ClientSide::doSetRequest: " +
                    "(OK) Set failed with expected SecurityException") ;
            } else {
                System.out.println("ClientSide::doSetRequest: " +
                    "(ERROR) Set failed with " +
                    e.getClass() + " instead of expected SecurityException");
                errorCount++;
            }
        } else {
            System.out.println("ClientSide::doSetRequest: (ERROR) Set failed");
            errorCount++;
        }
    }
    return errorCount;
}
 
源代码8 项目: samza   文件: TestJmxAppender.java
@Test
public void testJmxAppender() throws Exception {
  MBeanServerConnection mbserver = JMXConnectorFactory.connect(URL).getMBeanServerConnection();
  ObjectName objectName = new ObjectName(JmxAppender.JMX_OBJECT_DOMAIN + ":type=" + JmxAppender.JMX_OBJECT_TYPE + ",name=" + JmxAppender.JMX_OBJECT_NAME);
  String level = null;
  MockAppender mockAppender = new MockAppender();
  Logger.getRootLogger().addAppender(mockAppender);

  // Check INFO is set (from log4j.xml).
  level = (String) mbserver.getAttribute(objectName, "Level");
  assertEquals("INFO", level);

  log.info("info1");
  log.debug("debug1");

  // Set to debug.
  mbserver.setAttribute(objectName, new Attribute("Level", "debug"));

  // Check DEBUG is set.
  level = (String) mbserver.getAttribute(objectName, "Level");
  assertEquals("DEBUG", level);

  log.info("info2");
  log.debug("debug2");

  List<LoggingEvent> logLines = mockAppender.getLogLines();

  // Should not have debug1 because log level is info at first.
  Iterator<LoggingEvent> logLineIterator = logLines.iterator();
  assertEquals(3, logLines.size());
  assertEquals("info1", logLineIterator.next().getMessage());
  assertEquals("info2", logLineIterator.next().getMessage());
  assertEquals("debug2", logLineIterator.next().getMessage());
}
 
源代码9 项目: openjdk-8   文件: MXBeanProxy.java
@Override
Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
        throws Exception {
    assert(args.length == 1);
    Attribute attr = new Attribute(getName(), args[0]);
    mbsc.setAttribute(name, attr);
    return null;
}
 
源代码10 项目: hottub   文件: MXBeanProxy.java
@Override
Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
        throws Exception {
    assert(args.length == 1);
    Attribute attr = new Attribute(getName(), args[0]);
    mbsc.setAttribute(name, attr);
    return null;
}
 
源代码11 项目: jdk8u_jdk   文件: MXBeanProxy.java
@Override
Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
        throws Exception {
    assert(args.length == 1);
    Attribute attr = new Attribute(getName(), args[0]);
    mbsc.setAttribute(name, attr);
    return null;
}
 
源代码12 项目: jdk8u-jdk   文件: RMIDownloadTest.java
private static void testWithException(boolean send)
throws Exception {
    ClassLoader zoobyCL = new ZoobyClassLoader();
    Class<?> zoobyClass = Class.forName("Zooby", false, zoobyCL);
    Object zooby = zoobyClass.newInstance();

    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///");
    JMXConnectorServer cs =
            JMXConnectorServerFactory.newJMXConnectorServer(url, null, pmbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();

    Object rzooby;
    if (send) {
        System.out.println("Sending object...");
        mbsc.setAttribute(getSetName, new Attribute("It", zooby));
        rzooby = getSetInstance.getIt();
    } else {
        System.out.println("Receiving object...");
        getSetInstance.setIt(zooby);
        rzooby = mbsc.getAttribute(getSetName, "It");
    }

    if (!rzooby.getClass().getName().equals("Zooby")) {
        throw new Exception("FAILED: remote object is not a Zooby");
    }
    if (rzooby.getClass().getClassLoader() ==
            zooby.getClass().getClassLoader()) {
        throw new Exception("FAILED: same class loader: " +
                zooby.getClass().getClassLoader());
    }

    cc.close();
    cs.stop();
}
 
源代码13 项目: cougar   文件: CougarHelpers.java
public void setJMXMBeanAttributeValue(String mBeanName, String attributeName, Object value) {

		try {
			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);
		}
	}
 
源代码14 项目: openjdk-jdk8u   文件: AuthorizationTest.java
protected int doSetRequest(MBeanServerConnection mbsc,
                           ObjectName on,
                           boolean expectedException) {
    int errorCount = 0;

    try {
        Utils.debug(Utils.DEBUG_STANDARD,
            "ClientSide::doSetRequest: Set attributes of the MBean") ;

        Attribute attribute = new Attribute("Attribute", "My value") ;
        mbsc.setAttribute(on, attribute) ;

        if (expectedException) {
            System.out.println("ClientSide::doSetRequest: " +
                "(ERROR) Set did not fail with expected SecurityException");
            errorCount++;
        } else {
            System.out.println("ClientSide::doSetRequest: (OK) Set succeed") ;
        }
    } catch(Exception e) {
        Utils.printThrowable(e, true) ;
        if (expectedException) {
            if (e instanceof java.lang.SecurityException) {
                System.out.println("ClientSide::doSetRequest: " +
                    "(OK) Set failed with expected SecurityException") ;
            } else {
                System.out.println("ClientSide::doSetRequest: " +
                    "(ERROR) Set failed with " +
                    e.getClass() + " instead of expected SecurityException");
                errorCount++;
            }
        } else {
            System.out.println("ClientSide::doSetRequest: (ERROR) Set failed");
            errorCount++;
        }
    }
    return errorCount;
}
 
源代码15 项目: openjdk-jdk8u   文件: RMIDownloadTest.java
private static void testWithException(boolean send)
throws Exception {
    ClassLoader zoobyCL = new ZoobyClassLoader();
    Class<?> zoobyClass = Class.forName("Zooby", false, zoobyCL);
    Object zooby = zoobyClass.newInstance();

    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///");
    JMXConnectorServer cs =
            JMXConnectorServerFactory.newJMXConnectorServer(url, null, pmbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();

    Object rzooby;
    if (send) {
        System.out.println("Sending object...");
        mbsc.setAttribute(getSetName, new Attribute("It", zooby));
        rzooby = getSetInstance.getIt();
    } else {
        System.out.println("Receiving object...");
        getSetInstance.setIt(zooby);
        rzooby = mbsc.getAttribute(getSetName, "It");
    }

    if (!rzooby.getClass().getName().equals("Zooby")) {
        throw new Exception("FAILED: remote object is not a Zooby");
    }
    if (rzooby.getClass().getClassLoader() ==
            zooby.getClass().getClassLoader()) {
        throw new Exception("FAILED: same class loader: " +
                zooby.getClass().getClassLoader());
    }

    cc.close();
    cs.stop();
}
 
源代码16 项目: jdk8u-dev-jdk   文件: MXBeanProxy.java
@Override
Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
        throws Exception {
    assert(args.length == 1);
    Attribute attr = new Attribute(getName(), args[0]);
    mbsc.setAttribute(name, attr);
    return null;
}
 
@Test
public void testReadWriteAttributeStandalone() throws Exception {
    MBeanServerConnection connection = setupAndGetConnection(new MBeanInfoAdditionalInitialization(ProcessType.STANDALONE_SERVER, new TestExtension()));

    ObjectName name = createObjectName(LEGACY_DOMAIN + ":subsystem=test");
    checkAttributeValues(connection, name, 1, null, 2, BigInteger.valueOf(3), BigDecimal.valueOf(4), false, new byte[]{5, 6}, 7.0, "8",
            Collections.singletonList(9), 10, ModelType.INT, "key1", 11, "key2", 12);
    Assert.assertNull(connection.getAttribute(name, "complex"));


    try {
        connection.setAttribute(name, new Attribute("roInt", 101));
        Assert.fail("roInt not writable");
    } catch (Exception expected) {
        //expected
    }

    connection.setAttribute(name, new Attribute("int", 102));
    connection.setAttribute(name, new Attribute("undefinedInt", 103));
    connection.setAttribute(name, new Attribute("bigint", BigInteger.valueOf(104)));
    connection.setAttribute(name, new Attribute("bigdec", BigDecimal.valueOf(105)));
    connection.setAttribute(name, new Attribute("boolean", Boolean.TRUE));
    connection.setAttribute(name, new Attribute("bytes", new byte[]{106, 107}));
    connection.setAttribute(name, new Attribute("double", 108.0));
    connection.setAttribute(name, new Attribute("string", "109"));
    connection.setAttribute(name, new Attribute("list", new Integer[]{110}));
    connection.setAttribute(name, new Attribute("long", 111L));
    connection.setAttribute(name, new Attribute("type", ModelType.STRING.toString()));
    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("keyA", 112);
    map.put("keyB", 113);
    connection.setAttribute(name, new Attribute("map", map));
    MBeanInfo info = connection.getMBeanInfo(name);
    CompositeType complexType = assertCast(CompositeType.class, findAttribute(info.getAttributes(), "complex").getOpenType());
    connection.setAttribute(name, new Attribute("complex", createComplexData(connection, complexType, 1, BigDecimal.valueOf(2.0))));


    checkAttributeValues(connection, name, 1, 103, 102, BigInteger.valueOf(104), BigDecimal.valueOf(105), true, new byte[]{106, 107}, 108.0, "109",
            Collections.singletonList(110), 111, ModelType.STRING, "keyA", 112, "keyB", 113);
    CompositeData compositeData = assertCast(CompositeData.class, connection.getAttribute(name, "complex"));
    Assert.assertEquals(1, compositeData.get("int-value"));
    Assert.assertEquals(BigDecimal.valueOf(2.0), compositeData.get("bigdecimal-value"));
}
 
@Test
public void testReadWriteAttributeExpressionsStandalone() throws Exception {
    MBeanServerConnection connection = setupAndGetConnection(new MBeanInfoAdditionalInitialization(ProcessType.STANDALONE_SERVER, new TestExtension(true)));

    ObjectName name = createObjectName(EXPR_DOMAIN + ":subsystem=test");
    checkAttributeValues(connection, name, "1", null, "2", "3", "4", "false", new byte[]{5, 6}, "7.0", "8",
            Collections.singletonList("9"), "10", ModelType.INT.toString(), "key1", "11", "key2", "12");
    Assert.assertNull(connection.getAttribute(name, "complex"));


    try {
        connection.setAttribute(name, new Attribute("roInt", 101));
        Assert.fail("roInt not writable");
    } catch (Exception expected) {
        //expected
    }

    connection.setAttribute(name, new Attribute("int", "${should.not.exist!!!!!:102}"));
    connection.setAttribute(name, new Attribute("undefinedInt", "${should.not.exist!!!!!:103}"));
    connection.setAttribute(name, new Attribute("bigint", "${should.not.exist!!!!!:104}"));
    connection.setAttribute(name, new Attribute("bigdec", "${should.not.exist!!!!!:105}"));
    connection.setAttribute(name, new Attribute("boolean", "${should.not.exist!!!!!:true}"));
    connection.setAttribute(name, new Attribute("bytes", new byte[]{106, 107}));
    connection.setAttribute(name, new Attribute("double", "${should.not.exist!!!!!:108.0}"));
    connection.setAttribute(name, new Attribute("string", "${should.not.exist!!!!!:109}"));
    connection.setAttribute(name, new Attribute("list", new String[]{"${should.not.exist!!!!!:110}"}));
    connection.setAttribute(name, new Attribute("long", "${should.not.exist!!!!!:111}"));
    connection.setAttribute(name, new Attribute("type", "${should.not.exist!!!!!:STRING}"));
    Map<String, String> map = new HashMap<String, String>();
    map.put("keyA", "${should.not.exist!!!!!:112}");
    map.put("keyB", "${should.not.exist!!!!!:113}");
    connection.setAttribute(name, new Attribute("map", map));
    MBeanInfo info = connection.getMBeanInfo(name);
    CompositeType complexType = assertCast(CompositeType.class, findAttribute(info.getAttributes(), "complex").getOpenType());
    connection.setAttribute(name, new Attribute("complex", createComplexData(connection, complexType, "${should.not.exist!!!!!:1}", "${should.not.exist!!!!!:2.0}")));


    checkAttributeValues(connection, name, "1", "${should.not.exist!!!!!:103}", "${should.not.exist!!!!!:102}", "${should.not.exist!!!!!:104}", "${should.not.exist!!!!!:105}", "${should.not.exist!!!!!:true}",
            new byte[]{106, 107}, "${should.not.exist!!!!!:108.0}", "${should.not.exist!!!!!:109}",
            Collections.singletonList("${should.not.exist!!!!!:110}"), "${should.not.exist!!!!!:111}", "${should.not.exist!!!!!:STRING}", "keyA", "${should.not.exist!!!!!:112}", "keyB", "${should.not.exist!!!!!:113}");
    CompositeData compositeData = assertCast(CompositeData.class, connection.getAttribute(name, "complex"));
    Assert.assertEquals("${should.not.exist!!!!!:1}", compositeData.get("int-value"));
    Assert.assertEquals("${should.not.exist!!!!!:2.0}", compositeData.get("bigdecimal-value"));
}
 
源代码19 项目: openjdk-jdk9   文件: MXBeanInteropTest2.java
private final int doBasicMXBeanTest(MBeanServerConnection mbsc) {
    int errorCount = 0 ;
    System.out.println("---- doBasicMXBeanTest") ;

    try {
        ObjectName objName =
                new ObjectName("sqe:type=BasicMXBean") ;
        mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
        MBeanInfo mbInfo = mbsc.getMBeanInfo(objName);
        printMBeanInfo(mbInfo);
        System.out.println("---- OK\n") ;
        System.out.println("getMBeanInfo\t\t"
                + mbInfo);
        System.out.println("---- OK\n") ;

        System.out.println("Check mxbean field in the MBeanInfo");
        String mxbeanField =
                (String)mbInfo.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD);

        if ( mxbeanField == null || ! mxbeanField.equals("true")) {
            System.out.println("---- ERROR : Improper mxbean field value "
                    + mxbeanField);
            errorCount++;
        }
        System.out.println("---- OK\n") ;

        System.out.println("Set attribute ObjectNameAtt");
        Attribute att = new Attribute("ObjectNameAtt", objName);
        mbsc.setAttribute(objName, att);
        ObjectName value =
                (ObjectName)mbsc.getAttribute(objName, "ObjectNameAtt");

        if ( ! value.equals(objName) ) {
            errorCount++;
            System.out.println("---- ERROR : setAttribute failed, got "
                    + value
                    + " while expecting "
                    + objName);
        }
        System.out.println("---- OK\n") ;

        System.out.println("Call operation doNothing");
        mbsc.invoke(objName,  "doNothing", null, null);
        System.out.println("---- OK\n") ;

        System.out.println("Call operation getWeather");
        Object weather = mbsc.invoke(objName,
                "getWeather",
                new Object[]{Boolean.TRUE},
                new String[]{"boolean"});
        System.out.println("Weather is " + weather);
        System.out.println("---- OK\n") ;
    } catch (Exception e) {
        Utils.printThrowable(e, true) ;
        errorCount++ ;
        System.out.println("---- ERROR\n") ;
    }

    return errorCount ;
}
 
源代码20 项目: openjdk-jdk8u-backup   文件: MXBeanInteropTest2.java
private final int doBasicMXBeanTest(MBeanServerConnection mbsc) {
    int errorCount = 0 ;
    System.out.println("---- doBasicMXBeanTest") ;

    try {
        ObjectName objName =
                new ObjectName("sqe:type=BasicMXBean") ;
        mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
        MBeanInfo mbInfo = mbsc.getMBeanInfo(objName);
        printMBeanInfo(mbInfo);
        System.out.println("---- OK\n") ;
        System.out.println("getMBeanInfo\t\t"
                + mbInfo);
        System.out.println("---- OK\n") ;

        System.out.println("Check mxbean field in the MBeanInfo");
        String mxbeanField =
                (String)mbInfo.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD);

        if ( mxbeanField == null || ! mxbeanField.equals("true")) {
            System.out.println("---- ERROR : Improper mxbean field value "
                    + mxbeanField);
            errorCount++;
        }
        System.out.println("---- OK\n") ;

        System.out.println("Set attribute ObjectNameAtt");
        Attribute att = new Attribute("ObjectNameAtt", objName);
        mbsc.setAttribute(objName, att);
        ObjectName value =
                (ObjectName)mbsc.getAttribute(objName, "ObjectNameAtt");

        if ( ! value.equals(objName) ) {
            errorCount++;
            System.out.println("---- ERROR : setAttribute failed, got "
                    + value
                    + " while expecting "
                    + objName);
        }
        System.out.println("---- OK\n") ;

        System.out.println("Call operation doNothing");
        mbsc.invoke(objName,  "doNothing", null, null);
        System.out.println("---- OK\n") ;

        System.out.println("Call operation getWeather");
        Object weather = mbsc.invoke(objName,
                "getWeather",
                new Object[]{Boolean.TRUE},
                new String[]{"boolean"});
        System.out.println("Weather is " + weather);
        System.out.println("---- OK\n") ;
    } catch (Exception e) {
        Utils.printThrowable(e, true) ;
        errorCount++ ;
        System.out.println("---- ERROR\n") ;
    }

    return errorCount ;
}