javax.management.Attribute Java Examples

The following examples show how to use javax.management.Attribute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JMXAccessorSetTask.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example #2
Source File: RMIConnector.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void setAttribute(ObjectName name,
        Attribute attribute)
        throws InstanceNotFoundException,
        AttributeNotFoundException,
        InvalidAttributeValueException,
        MBeanException,
        ReflectionException,
        IOException {

    if (logger.debugOn()) logger.debug("setAttribute",
            "name=" + name + ", attribute name="
            + attribute.getName());

    final MarshalledObject<Attribute> sAttribute =
            new MarshalledObject<Attribute>(attribute);
    final ClassLoader old = pushDefaultClassLoader();
    try {
        connection.setAttribute(name, sAttribute, delegationSubject);
    } catch (IOException ioe) {
        communicatorAdmin.gotIOException(ioe);

        connection.setAttribute(name, sAttribute, delegationSubject);
    } finally {
        popDefaultClassLoader(old);
    }
}
 
Example #3
Source File: JmxClient.java    From vjtools with Apache License 2.0 6 votes vote down vote up
private synchronized NameValueMap getCachedAttributes(ObjectName objName, Set<String> attrNames)
		throws InstanceNotFoundException, ReflectionException, IOException {
	NameValueMap values = cachedValues.get(objName);
	if (values != null && values.keySet().containsAll(attrNames)) {
		return values;
	}
	attrNames = new TreeSet<String>(attrNames);
	Set<String> oldNames = cachedNames.get(objName);
	if (oldNames != null) {
		attrNames.addAll(oldNames);
	}
	values = new NameValueMap();
	final AttributeList attrs = conn.getAttributes(objName, attrNames.toArray(new String[attrNames.size()]));
	for (Attribute attr : attrs.asList()) {
		values.put(attr.getName(), attr.getValue());
	}
	cachedValues.put(objName, values);
	cachedNames.put(objName, attrNames);
	return values;
}
 
Example #4
Source File: RMIConnector.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void setAttribute(ObjectName name,
        Attribute attribute)
        throws InstanceNotFoundException,
        AttributeNotFoundException,
        InvalidAttributeValueException,
        MBeanException,
        ReflectionException,
        IOException {

    if (logger.debugOn()) logger.debug("setAttribute",
            "name=" + name + ", attribute name="
            + attribute.getName());

    final MarshalledObject<Attribute> sAttribute =
            new MarshalledObject<Attribute>(attribute);
    final ClassLoader old = pushDefaultClassLoader();
    try {
        connection.setAttribute(name, sAttribute, delegationSubject);
    } catch (IOException ioe) {
        communicatorAdmin.gotIOException(ioe);

        connection.setAttribute(name, sAttribute, delegationSubject);
    } finally {
        popDefaultClassLoader(old);
    }
}
 
Example #5
Source File: RMIConnectorLogAttributesTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void doTest(JMXConnector connector) throws IOException,
MalformedObjectNameException, ReflectionException,
InstanceAlreadyExistsException, MBeanRegistrationException,
MBeanException, NotCompliantMBeanException, InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException {
    MBeanServerConnection  mbsc = connector.getMBeanServerConnection();


    ObjectName objName = new ObjectName("com.redhat.test.jmx:type=NameMBean");
    System.out.println("DEBUG: Calling createMBean");
    mbsc.createMBean(Name.class.getName(), objName);

    System.out.println("DEBUG: Calling setAttributes");
    AttributeList attList = new AttributeList();
    attList.add(new Attribute("FirstName", ANY_NAME));
    attList.add(new Attribute("LastName", ANY_NAME));
    mbsc.setAttributes(objName, attList);
}
 
Example #6
Source File: RMIConnector.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void setAttribute(ObjectName name,
        Attribute attribute)
        throws InstanceNotFoundException,
        AttributeNotFoundException,
        InvalidAttributeValueException,
        MBeanException,
        ReflectionException,
        IOException {

    if (logger.debugOn()) logger.debug("setAttribute",
            "name=" + name + ", attribute name="
            + attribute.getName());

    final MarshalledObject<Attribute> sAttribute =
            new MarshalledObject<Attribute>(attribute);
    final ClassLoader old = pushDefaultClassLoader();
    try {
        connection.setAttribute(name, sAttribute, delegationSubject);
    } catch (IOException ioe) {
        communicatorAdmin.gotIOException(ioe);

        connection.setAttribute(name, sAttribute, delegationSubject);
    } finally {
        popDefaultClassLoader(old);
    }
}
 
Example #7
Source File: JMXProxyServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Sets an MBean attribute's value.
 */
private void setAttributeInternal(String onameStr, String attributeName, String value)
        throws OperationsException, MBeanException, ReflectionException {
    ObjectName oname = new ObjectName(onameStr);
    String type = registry.getType(oname, attributeName);
    Object valueObj = registry.convertValue(type, value);
    mBeanServer.setAttribute(oname, new Attribute(attributeName, valueObj));
}
 
Example #8
Source File: MBeanExceptionTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void setAttribute(Attribute attr)
        throws MBeanException {
    String attrName = attr.getName();
    if (attrName.equals("UncheckedException"))
        throw theUncheckedException;
    else
        throw new AssertionError();
}
 
Example #9
Source File: MXBeanProxy.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #10
Source File: Client.java    From vjtools with Apache License 2.0 5 votes vote down vote up
protected static Object doAttributeOperation(MBeanServerConnection mbsc, ObjectInstance instance, String command,
		MBeanAttributeInfo[] infos) throws Exception {
	// Usually we get attributes. If an argument, then we're being asked
	// to set attribute.
	CommandParse parse = new CommandParse(command);
	if (parse.getArgs() == null || parse.getArgs().length == 0) {
		// Special-casing. If the subCommand is 'Attributes', then return
		// list of all attributes.
		if (command.equals("Attributes")) {
			String[] names = new String[infos.length];
			for (int i = 0; i < infos.length; i++) {
				names[i] = infos[i].getName();
			}
			return mbsc.getAttributes(instance.getObjectName(), names);
		}
		return mbsc.getAttribute(instance.getObjectName(), parse.getCmd());
	}
	if (parse.getArgs().length != 1) {
		throw new IllegalArgumentException("One only argument setting " + "attribute values: " + parse.getArgs());
	}
	// Get first attribute of name 'cmd'. Assumption is no method
	// overrides. Then, look at the attribute and use its type.
	MBeanAttributeInfo info = (MBeanAttributeInfo) getFeatureInfo(infos, parse.getCmd());
	java.lang.reflect.Constructor c = Class.forName(info.getType()).getConstructor(new Class[] { String.class });
	Attribute a = new Attribute(parse.getCmd(), c.newInstance(new Object[] { parse.getArgs()[0] }));
	mbsc.setAttribute(instance.getObjectName(), a);
	return null;
}
 
Example #11
Source File: JmxRecordSetProvider.java    From presto with Apache License 2.0 5 votes vote down vote up
private ImmutableMap<String, Optional<Object>> getAttributes(Set<String> uniqueColumnNames, String name)
        throws JMException
{
    ObjectName objectName = new ObjectName(name);

    String[] columnNamesArray = uniqueColumnNames.toArray(new String[uniqueColumnNames.size()]);

    ImmutableMap.Builder<String, Optional<Object>> attributes = ImmutableMap.builder();
    for (Attribute attribute : mbeanServer.getAttributes(objectName, columnNamesArray).asList()) {
        attributes.put(attribute.getName(), Optional.ofNullable(attribute.getValue()));
    }
    return attributes.build();
}
 
Example #12
Source File: MBeanClientInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
private Object invokeAttribute(PropertyDescriptor pd, MethodInvocation invocation)
		throws JMException, IOException {

	Assert.state(this.serverToUse != null, "No MBeanServerConnection available");

	String attributeName = JmxUtils.getAttributeName(pd, this.useStrictCasing);
	MBeanAttributeInfo inf = this.allowedAttributes.get(attributeName);
	// If no attribute is returned, we know that it is not defined in the
	// 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");
	}
}
 
Example #13
Source File: NotificationListenerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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"));
}
 
Example #14
Source File: MBeanSupport.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public final AttributeList setAttributes(AttributeList attributes) {
    final AttributeList result = new AttributeList(attributes.size());
    for (Object attrObj : attributes) {
        // We can't use AttributeList.asList because it has side-effects
        Attribute attr = (Attribute) attrObj;
        try {
            setAttribute(attr);
            result.add(new Attribute(attr.getName(), attr.getValue()));
        } catch (Exception e) {
            // OK: attribute is not included in returned list, per spec
            // XXX: log the exception
        }
    }
    return result;
}
 
Example #15
Source File: OldMBeanServerTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public AttributeList setAttributes(AttributeList attributes) {
    AttributeList list = new AttributeList();
    // We carefully avoid using any new stuff from AttributeList here!
    for (Iterator<?> it = attributes.iterator(); it.hasNext(); ) {
        Attribute attr = (Attribute) it.next();
        try {
            setAttribute(attr);
            list.add(attr);
        } catch (Exception e) {
            // OK: ignore per spec
        }
    }
    return list;
}
 
Example #16
Source File: NotificationListenerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRegisterNotificationListenerWithBeanNameAndBeanNameInBeansMap() 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, beanName);

	Map listenerMappings = new HashMap();
	CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
	listenerMappings.put(beanName, 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 not notified", 1, listener.getCount("Age"));
}
 
Example #17
Source File: NotificationListenerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRegisterNotificationListenerWithBeanNameAndBeanNameInBeansMap() 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, beanName);

	Map listenerMappings = new HashMap();
	CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
	listenerMappings.put(beanName, 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 not notified", 1, listener.getCount("Age"));
}
 
Example #18
Source File: RequiredModelMBean.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the values of several attributes in the ModelMBean.
 * Executes a getAttribute for each attribute name in the
 * attrNames array passed in.
 *
 * @param attrNames A String array of names of the attributes
 * to be retrieved.
 *
 * @return The array of the retrieved attributes.
 *
 * @exception RuntimeOperationsException Wraps an
 * {@link IllegalArgumentException}: The object name in parameter is
 * null or attributes in parameter is null.
 *
 * @see #setAttributes(javax.management.AttributeList)
 */
public AttributeList getAttributes(String[] attrNames)      {
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
        MODELMBEAN_LOGGER.logp(Level.FINER,
                RequiredModelMBean.class.getName(),
        "getAttributes(String[])","Entry");
    }

    if (attrNames == null)
        throw new RuntimeOperationsException(new
            IllegalArgumentException("attributeNames must not be null"),
            "Exception occurred trying to get attributes of a "+
            "RequiredModelMBean");

    AttributeList responseList = new AttributeList();
    for (int i = 0; i < attrNames.length; i++) {
        try {
            responseList.add(new Attribute(attrNames[i],
                                 getAttribute(attrNames[i])));
        } catch (Exception e) {
            // eat exceptions because interface doesn't have an
            // exception on it
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
                MODELMBEAN_LOGGER.logp(Level.FINER,
                        RequiredModelMBean.class.getName(),
                    "getAttributes(String[])",
                        "Failed to get \"" + attrNames[i] + "\": ", e);
            }
        }
    }

    if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
        MODELMBEAN_LOGGER.logp(Level.FINER,
            RequiredModelMBean.class.getName(),
                "getAttributes(String[])","Exit");
    }

    return responseList;
}
 
Example #19
Source File: NotificationListenerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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"));
}
 
Example #20
Source File: NotificationListenerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRegisterNotificationListenerWithObjectNameBeforeBeanNameMappedToSameBeanInstance() 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(objectName, listener);
	listenerMappings.put(beanName, 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"));
}
 
Example #21
Source File: MBeanExceptionTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void setAttribute(Attribute attr)
        throws MBeanException {
    String attrName = attr.getName();
    if (attrName.equals("UncheckedException"))
        throw theUncheckedException;
    else
        throw new AssertionError();
}
 
Example #22
Source File: MBeanSupport.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public final AttributeList setAttributes(AttributeList attributes) {
    final AttributeList result = new AttributeList(attributes.size());
    for (Object attrObj : attributes) {
        // We can't use AttributeList.asList because it has side-effects
        Attribute attr = (Attribute) attrObj;
        try {
            setAttribute(attr);
            result.add(new Attribute(attr.getName(), attr.getValue()));
        } catch (Exception e) {
            // OK: attribute is not included in returned list, per spec
            // XXX: log the exception
        }
    }
    return result;
}
 
Example #23
Source File: MBeanServerDelegateImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * This method always fail since all MBeanServerDelegateMBean attributes
 * are read-only.
 *
 * @param attribute The identification of the attribute to
 * be set and  the value it is to be set to.
 *
 * @exception AttributeNotFoundException
 */
public void setAttribute(Attribute attribute)
    throws AttributeNotFoundException, InvalidAttributeValueException,
           MBeanException, ReflectionException {

    // Now we will always fail:
    // Either because the attribute is null or because it is not
    // accessible (or does not exist).
    //
    final String attname = (attribute==null?null:attribute.getName());
    if (attname == null) {
        final RuntimeException r =
            new IllegalArgumentException("Attribute name cannot be null");
        throw new RuntimeOperationsException(r,
            "Exception occurred trying to invoke the setter on the MBean");
    }

    // This is a hack: we call getAttribute in order to generate an
    // AttributeNotFoundException if the attribute does not exist.
    //
    Object val = getAttribute(attname);

    // If we reach this point, we know that the requested attribute
    // exists. However, since all attributes are read-only, we throw
    // an AttributeNotFoundException.
    //
    throw new AttributeNotFoundException(attname + " not accessible");
}
 
Example #24
Source File: MXBeanProxy.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #25
Source File: MBeanServerDelegateImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method always fail since all MBeanServerDelegateMBean attributes
 * are read-only.
 *
 * @param attribute The identification of the attribute to
 * be set and  the value it is to be set to.
 *
 * @exception AttributeNotFoundException
 */
public void setAttribute(Attribute attribute)
    throws AttributeNotFoundException, InvalidAttributeValueException,
           MBeanException, ReflectionException {

    // Now we will always fail:
    // Either because the attribute is null or because it is not
    // accessible (or does not exist).
    //
    final String attname = (attribute==null?null:attribute.getName());
    if (attname == null) {
        final RuntimeException r =
            new IllegalArgumentException("Attribute name cannot be null");
        throw new RuntimeOperationsException(r,
            "Exception occurred trying to invoke the setter on the MBean");
    }

    // This is a hack: we call getAttribute in order to generate an
    // AttributeNotFoundException if the attribute does not exist.
    //
    Object val = getAttribute(attname);

    // If we reach this point, we know that the requested attribute
    // exists. However, since all attributes are read-only, we throw
    // an AttributeNotFoundException.
    //
    throw new AttributeNotFoundException(attname + " not accessible");
}
 
Example #26
Source File: JmxMBeanServer.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Clone attribute.
 */
private Attribute cloneAttribute(Attribute attribute) {
    if (attribute != null) {
        if (!attribute.getClass().equals(Attribute.class)) {
            return new Attribute(attribute.getName(), attribute.getValue());
        }
    }
    return attribute;
}
 
Example #27
Source File: XMBeanAttributes.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void tableChanged(final TableModelEvent e) {
    // only post changes to the draggable column
    if (isColumnEditable(e.getColumn())) {
        final TableModel model = (TableModel)e.getSource();
        Object tableValue = model.getValueAt(e.getFirstRow(),
                                         e.getColumn());

        if (LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer("tableChanged: firstRow="+e.getFirstRow()+
                ", lastRow="+e.getLastRow()+", column="+e.getColumn()+
                ", value="+tableValue);
        }
        // if it's a String, try construct new value
        // using the defined type.
        if (tableValue instanceof String) {
            try {
                tableValue =
                    Utils.createObjectFromString(getClassName(e.getFirstRow()), // type
                    (String)tableValue);// value
            } catch (Throwable ex) {
                popupAndLog(ex,"tableChanged",
                            Messages.PROBLEM_SETTING_ATTRIBUTE);
            }
        }
        final String attributeName = getValueName(e.getFirstRow());
        final Attribute attribute =
              new Attribute(attributeName,tableValue);
        setAttribute(attribute, "tableChanged");
    }
}
 
Example #28
Source File: NotificationListenerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRegisterNotificationListenerWithObjectNameBeforeBeanNameMappedToSameBeanInstance() 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(objectName, listener);
	listenerMappings.put(beanName, 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"));
}
 
Example #29
Source File: MBeanServerAccessController.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Call <code>checkWrite()</code>, then forward this method to the
 * wrapped object.
 */
public void setAttribute(ObjectName name, Attribute attribute)
    throws
    InstanceNotFoundException,
    AttributeNotFoundException,
    InvalidAttributeValueException,
    MBeanException,
    ReflectionException {
    checkWrite();
    getMBeanServer().setAttribute(name, attribute);
}
 
Example #30
Source File: RequiredModelMBean.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the values of several attributes in the ModelMBean.
 * Executes a getAttribute for each attribute name in the
 * attrNames array passed in.
 *
 * @param attrNames A String array of names of the attributes
 * to be retrieved.
 *
 * @return The array of the retrieved attributes.
 *
 * @exception RuntimeOperationsException Wraps an
 * {@link IllegalArgumentException}: The object name in parameter is
 * null or attributes in parameter is null.
 *
 * @see #setAttributes(javax.management.AttributeList)
 */
public AttributeList getAttributes(String[] attrNames)      {
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
        MODELMBEAN_LOGGER.logp(Level.FINER,
                RequiredModelMBean.class.getName(),
        "getAttributes(String[])","Entry");
    }

    if (attrNames == null)
        throw new RuntimeOperationsException(new
            IllegalArgumentException("attributeNames must not be null"),
            "Exception occurred trying to get attributes of a "+
            "RequiredModelMBean");

    AttributeList responseList = new AttributeList();
    for (int i = 0; i < attrNames.length; i++) {
        try {
            responseList.add(new Attribute(attrNames[i],
                                 getAttribute(attrNames[i])));
        } catch (Exception e) {
            // eat exceptions because interface doesn't have an
            // exception on it
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
                MODELMBEAN_LOGGER.logp(Level.FINER,
                        RequiredModelMBean.class.getName(),
                    "getAttributes(String[])",
                        "Failed to get \"" + attrNames[i] + "\": ", e);
            }
        }
    }

    if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
        MODELMBEAN_LOGGER.logp(Level.FINER,
            RequiredModelMBean.class.getName(),
                "getAttributes(String[])","Exit");
    }

    return responseList;
}