/** * Enables the to get the values of several attributes of the Dynamic MBean. */ public AttributeList getAttributes(String[] attributeNames) { // Check attributeNames is not null to avoid NullPointerException later on if (attributeNames == null) { throw new RuntimeOperationsException( new IllegalArgumentException("attributeNames[] cannot be null"), "Cannot invoke a getter of " + dClassName); } AttributeList resultList = new AttributeList(); // if attributeNames is empty, return an empty result list if (attributeNames.length == 0) return resultList; // build the result attribute list for (int i=0 ; i<attributeNames.length ; i++){ try { Object value = getAttribute((String) attributeNames[i]); resultList.add(new Attribute(attributeNames[i],value)); } catch (JMException e) { e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace(); } } return(resultList); }
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(); }
throw new RuntimeOperationsException( new IllegalArgumentException("Attribute cannot be null"), "Cannot invoke a setter of " + dClassName + " with null attribute"); String name = attribute.getName(); Object value = attribute.getValue(); throw new RuntimeOperationsException( new IllegalArgumentException("Attribute name cannot be null"), "Cannot invoke the setter of "+dClassName+ p = null; } else { p = OptionConverter.toLevel(s, p); throw(new AttributeNotFoundException("Attribute " + name + " not found in " + this.getClass().getName()));
@Test public void testMetricsMBean() throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); final ObjectName oname = new ObjectName( "org.apache.hadoop.hive.common.metrics:type=MetricsMBean"); MBeanInfo mBeanInfo = mbs.getMBeanInfo(oname); Attribute attr = new Attribute("fooMetric", Long.valueOf(-77)); mbs.setAttribute(oname, attr); mBeanInfo = mbs.getMBeanInfo(oname); MBeanAttributeInfo[] attrinuteInfos = mBeanInfo.getAttributes(); assertEquals(1, attrinuteInfos.length); boolean attrFound = false; for (MBeanAttributeInfo info : attrinuteInfos) { if ("fooMetric".equals(info.getName())) { assertEquals("java.lang.Long", info.getType()); assertTrue(info.isReadable()); assertTrue(info.isWritable()); assertFalse(info.isIs());
private void set(final String cmd) { final String[] split = cmd.split(" "); if (split.length < 2) { streamManager.writeErr("you need to specify an attribute, an objectname and a value"); return; } final MBeanServer mBeanServer = LocalMBeanServer.get(); final String newValue = cmd.substring(split[0].length() + split[1].length() + 1).trim(); try { final ObjectName oname = new ObjectName(split[1]); final MBeanInfo minfo = mBeanServer.getMBeanInfo(oname); final MBeanAttributeInfo attrs[] = minfo.getAttributes(); String type = String.class.getName(); for (int i = 0; i < attrs.length; i++) { if (attrs[i].getName().equals(split[0])) { type = attrs[i].getType(); break; } } final Object valueObj = PropertyEditors.getValue(type, newValue, Thread.currentThread().getContextClassLoader()); mBeanServer.setAttribute(oname, new Attribute(split[0], valueObj)); streamManager.writeOut("done"); } catch (final Exception ex) { streamManager.writeOut("Error - " + ex.toString()); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testRegisterNotificationListenerWithWildcard() throws Exception { ObjectName objectName = ObjectName.getInstance("spring:name=Test"); JmxTestBean bean = new JmxTestBean(); Map<String, Object> beans = new HashMap<>(); beans.put(objectName.getCanonicalName(), bean); CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener(); Map notificationListeners = new HashMap(); notificationListeners.put("*", listener); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); exporter.setNotificationListenerMappings(notificationListeners); start(exporter); // update the attribute String attributeName = "Name"; server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop")); assertEquals("Listener not notified", 1, listener.getCount(attributeName)); }
Set<ObjectInstance> queryMBeans = null; try { queryMBeans = mbeanServer.queryMBeans(null, null); } catch (Exception ex) { LOG.error("Could not get Mbeans for monitoring", ex); if (!obj.getObjectName().toString().startsWith("org.apache.flume")) { continue; MBeanAttributeInfo[] attrs = mbeanServer.getMBeanInfo(obj.getObjectName()).getAttributes(); String[] strAtts = new String[attrs.length]; for (int i = 0; i < strAtts.length; i++) { strAtts[i] = attrs[i].getName(); AttributeList attrList = mbeanServer.getAttributes(obj.getObjectName(), strAtts); String component = obj.getObjectName().toString().substring( obj.getObjectName().toString().indexOf('=') + 1); Map<String, String> attrMap = Maps.newHashMap(); if (localAttr.getName().equalsIgnoreCase("type")) { component = localAttr.getValue() + "." + component; attrMap.put(localAttr.getName(), localAttr.getValue().toString());
for (int idx = 0; idx < attrInfos.length; ++idx) { MBeanAttributeInfo attr = attrInfos[idx]; if (!attr.isReadable()) { logScrape(mbeanName, attr, "not readable"); continue; name2AttrInfo.put(attr.getName(), attr); return; for (Attribute attribute : attributes.asList()) { MBeanAttributeInfo attr = name2AttrInfo.get(attribute.getName()); logScrape(mbeanName, attr, "process"); processBeanValue( mbeanName.getDomain(), jmxMBeanPropertyCache.getKeyPropertyList(mbeanName), new LinkedList<String>(), attr.getName(), attr.getType(), attr.getDescription(), attribute.getValue() );
private Object setAttribute(JmxRequest request, MBeanServer server) throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException, IntrospectionException, InvalidAttributeValueException, IllegalAccessException, InvocationTargetException { // Old value, will throw an exception if attribute is not known. That's good. Object oldValue = server.getAttribute(request.getObjectName(), request.getAttributeName()); MBeanInfo mInfo = server.getMBeanInfo(request.getObjectName()); MBeanAttributeInfo aInfo = null; MBeanAttributeInfo[] aInfos = mInfo.getAttributes(); for (int i = 0;i<aInfos.length;i++) { if (aInfos[i].getName().equals(request.getAttributeName())) { aInfo = aInfos[i]; break; } } if (aInfo == null) { throw new AttributeNotFoundException("No attribute " + request.getAttributeName() + " found for MBean " + request.getObjectNameAsString()); } String type = aInfo.getType(); Object[] values = objectToJsonConverter.getValues(type,oldValue,request); Attribute attribute = new Attribute(request.getAttributeName(),values[0]); server.setAttribute(request.getObjectName(),attribute); return values[1]; } }
throw new RuntimeOperationsException( new IllegalArgumentException("AttributeList attributes cannot be null"), "Cannot invoke a setter of " + dClassName); AttributeList resultList = new AttributeList(); if (attributes.isEmpty()) return resultList; for (Iterator i = attributes.iterator(); i.hasNext();) { Attribute attr = (Attribute) i.next(); try { setAttribute(attr); String name = attr.getName(); Object value = getAttribute(name); resultList.add(new Attribute(name,value)); } catch(JMException e) { e.printStackTrace();
private void handleSetAttribute(MBeanServer proxy, ObjectName objectName, Attribute attribute) throws JMException, IOException { String dataType = null; MBeanInfo info = proxy.getMBeanInfo(objectName); for (MBeanAttributeInfo attr : info.getAttributes()) { if (attr.getName().equals(attribute.getName())) { dataType = attr.getType(); break; } } if (dataType == null) throw new IllegalStateException("Attribute data type can not be found"); handleInvoke(objectName, "set" + attribute.getName(), new Object[]{attribute.getValue()}, new String[]{dataType}); }
@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()); }
@Override public AttributeList getAttributes(String[] names) { AttributeList list = new AttributeList(); for (String name : names) { try { list.add(new Attribute(name, getAttribute(name))); } catch (Exception e) { log.warn("Error getting JMX attribute '{}'", name, e); } } return list; }
@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")); }
public AttributeList setAttributes(AttributeList list) { AttributeList results=new AttributeList(); for(int i=0;i < list.size();i++) { Attribute attr=(Attribute)list.get(i); if(setNamedAttribute(attr)) results.add(attr); else { if(log.isWarnEnabled()) log.warn("Failed to update attribute name " + attr.getName() + " with value " + attr.getValue()); } } return results; }
@Override public AttributeList setAttributes(AttributeList attributes) { if (attributes == null) throw new RuntimeOperationsException(new IllegalArgumentException( "Attribute list cannot be null.")); Logger logger = getLogger(); AttributeList list = new AttributeList(); for (Iterator i = attributes.iterator(); i.hasNext();) { Attribute attribute = (Attribute) i.next(); String name = attribute.getName(); try { setAttribute(attribute); list.add(attribute); } catch (Exception x) { if (logger.isEnabledFor(Logger.TRACE)) logger.trace("setAttribute for attribute " + name + " failed", x); // And go on with the next one } } return list; }
public void walkTree(MBeanServerConnection connection) throws Exception { // key here is null, null returns everything! Set<ObjectName> mbeans = connection.queryNames(null, null); for (ObjectName name : mbeans) { MBeanInfo info = connection.getMBeanInfo(name); MBeanAttributeInfo[] attrs = info.getAttributes(); String[] attrNames = new String[attrs.length]; for (int i = 0; i < attrs.length; i++) { attrNames[i] = attrs[i].getName(); } try { AttributeList attributes = connection.getAttributes(name, attrNames); for (Attribute attribute : attributes.asList()) { output(name.getCanonicalName() + "%" + attribute.getName(), attribute.getValue()); } } catch (Exception e) { log.error("error getting " + name + ":" + e.getMessage(), e); } } }
if (attribute.isReadable() && !"password".equalsIgnoreCase(attribute.getName())) { attributeNames.add(attribute.getName()); try { final List<Object> attributes = mbeanServer.getAttributes(name, attributeNamesArray); for (final Object object : attributes) { final Attribute attribute = (Attribute) object; final Object value = convertValueIfNeeded(attribute.getValue()); final String attributeDescription = getAttributeDescription(attribute.getName(), attributeInfos); final String formattedAttributeValue = formatAttributeValue(value); final MBeanAttribute mbeanAttribute = new MBeanAttribute(attribute.getName(), attributeDescription, formattedAttributeValue); result.add(mbeanAttribute);
/** {@inheritDoc} */ public void setAttribute(Attribute pAttribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { try { if (!attributeInfoMap.containsKey(pAttribute.getName())) { jolokiaMBeanServer.setAttribute(objectName, pAttribute); } else { String name = pAttribute.getName(); MBeanAttributeInfo info = attributeInfoMap.get(name); Object value; if (info instanceof OpenMBeanAttributeInfo) { value = fromJson(((OpenMBeanAttributeInfo) info).getOpenType(), (String) pAttribute.getValue()); } else { value = fromJson(info.getType(), (String) pAttribute.getValue()); } Attribute attr = new Attribute(name, value); jolokiaMBeanServer.setAttribute(objectName, attr); } } catch (InstanceNotFoundException e) { AttributeNotFoundException exp = new AttributeNotFoundException("MBean " + objectName + " not found for attribute " + pAttribute); exp.initCause(e); throw exp; } }