private Resource probeTransactions(MBeanServer mbs, boolean exposeAllLogs) throws OperationFailedException { try { ObjectName on = new ObjectName(osMBeanName); mbs.setAttribute(on, new javax.management.Attribute("ExposeAllRecordsAsMBeans", Boolean.valueOf(exposeAllLogs))); mbs.invoke(on, "probe", null, null); Set<ObjectInstance> transactions = mbs.queryMBeans(new ObjectName(osMBeanName + ",*"), null); final Resource resource = Resource.Factory.create(); addTransactions(resource, transactions, mbs); return resource; } catch (JMException e) { throw new OperationFailedException("Transaction discovery error: ", e); } catch (IOException e) { throw new OperationFailedException("Transaction discovery error: ", e); } }
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()));
@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)); }
/** * 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); }
@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; }
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();
@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()); }
@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 setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { Interceptor interceptor = setters.get(attribute.getName()); if (interceptor == null) { throw new AttributeNotFoundException(attribute.getName()); } invoke(interceptor, new Object[]{attribute.getValue()}); }
InvalidAttributeValueException, MBeanException, ReflectionException { if (attribute == null) throw new RuntimeOperationsException(new IllegalArgumentException( "Attribute cannot be null.")); throw new AttributeNotFoundException( "ModelMBeanInfo is null"); if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("ModelMBeanInfo is: " + info); String attrName = attribute.getName(); Object attrValue = attribute.getValue(); throw new AttributeNotFoundException( String.format("Cannot find ModelMBeanAttributeInfo for attribute %s", attrName)); throw new AttributeNotFoundException( String.format("Attribute %s is not writable", attrName)); sendAttributeChangeNotification(new Attribute(attrName, oldValue), attribute);
@Override public AttributeList setAttributes(AttributeList attributes) { // validation from javax.management.modelmbean.RequiredModelMBean if (attributes == null) throw new RuntimeOperationsException(new IllegalArgumentException("attributes must not be null"), "Exception occurred trying to set attributes of a " + getClass().getSimpleName()); AttributeList list = new AttributeList(); for (Attribute attribute : attributes.asList()) { try { JmxAttribute attr = getJmxMetadata().getAttribute(attribute.getName()); attr.set(getManagedResource(), attribute.getValue()); list.add(attribute); } catch (AttributeNotFoundException ignored) { } catch (ReflectionException ignored) { } catch (InvalidAttributeValueException ignored) { } } return list; }
info = beanConn.getMBeanInfo(mbeanName); } catch (IOException e) { logScrape(mbeanName.toString(), "getMBeanInfo Fail: " + e); return; } catch (JMException e) { logScrape(mbeanName.toString(), "getMBeanInfo Fail: " + e); return; 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.getType(), attr.getDescription(), attribute.getValue() );
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); } } }
/** {@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; } }
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; }
private static Metric toMetric(long t, ObjectName name, Attribute attribute, Tag dsType) { Tag id = Tags.newTag("id", name.getKeyProperty("name")); Tag clazz = Tags.newTag("class", name.getKeyProperty("type")); TagList list = BasicTagList.of(id, clazz, dsType); return new Metric(normalizeName(attribute.getName()), list, t, attribute.getValue()); }