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); }
@Override public AttributeList getAttributes(String[] attributeNames) { if (attributeNames == null || attributeNames.length == 0) throw new IllegalArgumentException(); updateMbeanInfoIfMetricsListChanged(); AttributeList result = new AttributeList(attributeNames.length); for (String iAttributeName : attributeNames) { try { Object value = getAttribute(iAttributeName); result.add(new Attribute(iAttributeName, value)); } catch (Exception e) { continue; } } return result; }
/** * Obtain and return the values of several attributes of this MBean. * * @param names Names of the requested attributes */ @Override public AttributeList getAttributes(String names[]) { // Validate the input parameters if (names == null) throw new RuntimeOperationsException (new IllegalArgumentException("Attribute names list is null"), "Attribute names list is null"); // Prepare our response, eating all exceptions AttributeList response = new AttributeList(); for (int i = 0; i < names.length; i++) { try { response.add(new Attribute(names[i],getAttribute(names[i]))); } catch (Exception e) { // Not having a particular attribute in the response // is the indication of a getter problem } } return (response); }
/** * Set the values of several attributes of this MBean. * * @param attributes THe names and values to be set * * @return The list of attributes that were set and their new values */ @Override public AttributeList setAttributes(AttributeList attributes) { AttributeList response = new AttributeList(); // Validate the input parameters if (attributes == null) return response; // Prepare and return our response, eating all exceptions String names[] = new String[attributes.size()]; int n = 0; Iterator<?> items = attributes.iterator(); while (items.hasNext()) { Attribute item = (Attribute) items.next(); names[n++] = item.getName(); try { setAttribute(item); } catch (Exception e) { // Ignore all exceptions } } return (getAttributes(names)); }
/** * Obtain and return the values of several attributes of this MBean. * * @param names * Names of the requested attributes */ @Override public AttributeList getAttributes(String names[]) { // Validate the input parameters if (names == null) throw new RuntimeOperationsException(new IllegalArgumentException("Attribute names list is null"), "Attribute names list is null"); // Prepare our response, eating all exceptions AttributeList response = new AttributeList(); for (int i = 0; i < names.length; i++) { try { response.add(new Attribute(names[i], getAttribute(names[i]))); } catch (Exception e) { // Not having a particular attribute in the response // is the indication of a getter problem } } return (response); }
@Override public AttributeList getAttributes(String[] attributeNames) { if (attributeNames == null) { throw new RuntimeOperationsException(new IllegalArgumentException("attributeNames[] cannot be null"), "Cannot call getAttributes with null attribute names"); } AttributeList resultList = new AttributeList(); if (attributeNames.length == 0) { return resultList; } for (String attributeName : attributeNames) { try { Object value = getAttribute(attributeName); resultList.add(new Attribute(attributeName, value)); } catch (Exception e) { e.printStackTrace(); } } return (resultList); }
/** * Obtain and return the values of several attributes of this MBean. * * @param names Names of the requested attributes */ public AttributeList getAttributes(String names[]) { // Validate the input parameters if (names == null) throw new RuntimeOperationsException (new IllegalArgumentException("Attribute names list is null"), "Attribute names list is null"); // Prepare our response, eating all exceptions AttributeList response = new AttributeList(); for (int i = 0; i < names.length; i++) { try { response.add(new Attribute(names[i],getAttribute(names[i]))); } catch (Exception e) { ; // Not having a particular attribute in the response ; // is the indication of a getter problem } } return (response); }
@Test public void testSetAttribute() throws Exception { DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer, threadPoolDynamicWrapperON, DynamicMBean.class); proxy.setAttribute(new Attribute(THREAD_COUNT, newThreadCount)); assertEquals(newThreadCount, proxy.getAttribute(THREAD_COUNT)); assertEquals(newThreadCount, threadPoolConfigBean.getThreadCount()); AttributeList attributeList = new AttributeList(); attributeList.add(new Attribute(THREAD_COUNT, threadCount)); boolean bool = true; attributeList.add(new Attribute(TRIGGER_NEW_INSTANCE_CREATION, bool)); proxy.setAttributes(attributeList); assertEquals(threadCount, threadPoolConfigBean.getThreadCount()); assertEquals(bool, threadPoolConfigBean.isTriggerNewInstanceCreation()); }
public static double getProcessCpuLoad(MBeanServerConnection mbs) throws MalformedObjectNameException, NullPointerException, InstanceNotFoundException, ReflectionException, IOException { ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem"); AttributeList list = mbs.getAttributes(name, new String[]{"ProcessCpuLoad"}); if (list.isEmpty()) { return 0.0; } Attribute att = (Attribute) list.get(0); Double value = (Double) att.getValue(); // usually takes a couple of seconds before we get real values if (value == -1.0) { return 0.0; } // returns a percentage value with 1 decimal point precision return ((int) (value * 1000) / 10.0); }
public AttributeList getAttributes(ObjectName name, String[] attributes) throws InstanceNotFoundException, ReflectionException, IOException { if (logger.debugOn()) logger.debug("getAttributes", "name=" + name + ", attributes=" + strings(attributes)); final ClassLoader old = pushDefaultClassLoader(); try { return connection.getAttributes(name, attributes, delegationSubject); } catch (IOException ioe) { communicatorAdmin.gotIOException(ioe); return connection.getAttributes(name, attributes, delegationSubject); } finally { popDefaultClassLoader(old); } }
@Test public void testReadAttributes() throws Exception { DynamicMBean proxy = JMX.newMBeanProxy(platformMBeanServer, threadPoolDynamicWrapperON, DynamicMBean.class); assertEquals(threadCount, proxy.getAttribute(THREAD_COUNT)); assertEquals(threadPoolConfigBean.isTriggerNewInstanceCreation(), proxy.getAttribute(TRIGGER_NEW_INSTANCE_CREATION)); AttributeList attributes = proxy.getAttributes(new String[] { THREAD_COUNT, TRIGGER_NEW_INSTANCE_CREATION }); assertEquals(2, attributes.size()); Attribute threadCountAttr = (Attribute) attributes.get(0); assertEquals(THREAD_COUNT, threadCountAttr.getName()); assertEquals(threadCount, threadCountAttr.getValue()); Attribute boolTestAttr = (Attribute) attributes.get(1); assertEquals(TRIGGER_NEW_INSTANCE_CREATION, boolTestAttr.getName()); assertEquals(threadPoolConfigBean.isTriggerNewInstanceCreation(), boolTestAttr.getValue()); MBeanInfo beanInfo = proxy.getMBeanInfo(); assertEquals(2, beanInfo.getAttributes().length); }
public AttributeList setAttributes(AttributeList attributes) { if (attributes == null) throw new RuntimeOperationsException(new IllegalArgumentException( LocalizedStrings.MX4JModelMBean_ATTRIBUTE_LIST_CANNOT_BE_NULL.toLocalizedString())); 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 static double getProcessCpuLoad() { double cpuLoad; try { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem"); AttributeList list = mbs.getAttributes(name, new String[] { "ProcessCpuLoad" }); if(list.isEmpty()) { return Double.NaN; } Attribute att = (Attribute) list.get(0); Double value = (Double) att.getValue(); if(value == -1.0) { return Double.NaN; } cpuLoad = value * 100d; } catch (InstanceNotFoundException | ReflectionException | MalformedObjectNameException err) { cpuLoad = Double.NaN; } return cpuLoad; }
public AttributeList getAttributes(String[] attributeNames) { if(attributeNames != null) { AttributeList resultList = new AttributeList(); if (attributeNames.length == 0) return resultList; // Build the result attribute list for(int i=0 ; i<attributeNames.length; i++) { try { Object oValue = getAttribute(attributeNames[i]); resultList.add(new Attribute(attributeNames[i], oValue)); } catch (Exception e) { } } return resultList; } return null; }
@Override public Object invoke(final String actionName, final Object[] params, final String[] signature) throws MBeanException, ReflectionException { if ("getAttribute".equals(actionName) && params.length == 1 && signature[0].equals(String.class.getName())) { try { return getAttribute((String) params[0]); } catch (final AttributeNotFoundException e) { throw new MBeanException(e, "Attribute not found on " + moduleIdentifier); } } else if ("getAttributes".equals(actionName) && params.length == 1 && signature[0].equals(String[].class.getName())) { return getAttributes((String[]) params[0]); } else if ("setAttributes".equals(actionName) && params.length == 1 && signature[0].equals(AttributeList.class.getName())) { return setAttributes((AttributeList) params[0]); } else { LOG.debug("Operation not found {} ", actionName); throw new UnsupportedOperationException(String.format( "Operation not found on %s. Method invoke is only supported for getInstance and getAttribute(s) " + "method, got actionName %s, params %s, signature %s ", moduleIdentifier, actionName, params, signature)); } }
@Override public AttributeList setAttributes(final AttributeList attributes) { AttributeList result = new AttributeList(); for (Object attributeObject : attributes) { Attribute attribute = (Attribute) attributeObject; try { setAttribute(attribute); result.add(attribute); } catch (final InvalidAttributeValueException | AttributeNotFoundException | MBeanException | ReflectionException e) { LOG.warn("Setting attribute {} failed on {}", attribute.getName(), moduleIdentifier, e); throw new IllegalArgumentException( "Setting attribute failed - " + attribute.getName() + " on " + moduleIdentifier, e); } } return result; }
@Override public long[] getFileDecriptorInfo() { MBeanServer mbsc = MBeans.getMBeanServer(); AttributeList attributes; try { attributes = mbsc.getAttributes( new ObjectName("java.lang:type=OperatingSystem"), new String[]{"OpenFileDescriptorCount", "MaxFileDescriptorCount"}); List<Attribute> attrList = attributes.asList(); long openFdCount = (Long)attrList.get(0).getValue(); long maxFdCount = (Long)attrList.get(1).getValue(); long[] fdCounts = { openFdCount, maxFdCount}; return fdCounts; } catch (Exception e) { LogFactory.getLog(SdkMBeanRegistrySupport.class).debug( "Failed to retrieve file descriptor info", e); } return null; }
/** * Set the values of several attributes of this MBean. * * @param attributes * THe names and values to be set * * @return The list of attributes that were set and their new values */ @Override public AttributeList setAttributes(AttributeList attributes) { AttributeList response = new AttributeList(); // Validate the input parameters if (attributes == null) return response; // Prepare and return our response, eating all exceptions String names[] = new String[attributes.size()]; int n = 0; Iterator<?> items = attributes.iterator(); while (items.hasNext()) { Attribute item = (Attribute) items.next(); names[n++] = item.getName(); try { setAttribute(item); } catch (Exception e) { // Ignore all exceptions } } return (getAttributes(names)); }
public AttributeList getAttributes(String[] attributes) { try { return mbsc.getAttributes(objectName, attributes); } catch (Exception ex) { Logger.getLogger(DynamicProxy.class.getName()).log(Level.SEVERE, null, ex); return null; } }
/** * 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.TRACE)) { MODELMBEAN_LOGGER.log(Level.TRACE, RequiredModelMBean.class.getName(), "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.TRACE)) { MODELMBEAN_LOGGER.log(Level.TRACE, "Failed to get \"" + attrNames[i] + "\": ", e); } } } if (MODELMBEAN_LOGGER.isLoggable(Level.TRACE)) { MODELMBEAN_LOGGER.log(Level.TRACE, "Exit"); } return responseList; }
@Override public AttributeList getAttributes(String[] attributes) { AttributeList attributeList = new AttributeList(); for (String atr : attributes) { try { attributeList.add(new Attribute(atr, folder.getProperty(atr).getValue())); } catch (IntrospectionException ex) { Logger.getLogger(FolderMBean.class.getName()).log(Level.SEVERE, null, ex); } } return attributeList; }
@Override protected AttributeList getAttributes(String[] attributes) throws SchedulerException { try { return server.getAttributes(getSchedulerObjectName(), attributes); } catch (Exception e) { throw new SchedulerException("Failed to get Scheduler MBean attributes: " + Arrays.asList(attributes), e); } }
public SchedulerMetaData getMetaData() throws SchedulerException { AttributeList attributeList = getAttributes( new String[] { "SchedulerName", "SchedulerInstanceId", "StandbyMode", "Shutdown", "JobStoreClassName", "ThreadPoolClassName", "ThreadPoolSize", "Version", "PerformanceMetrics" }); try { return new SchedulerMetaData( (String)getAttribute(attributeList, 0).getValue(), (String)getAttribute(attributeList, 1).getValue(), getClass(), true, false, (Boolean)getAttribute(attributeList, 2).getValue(), (Boolean)getAttribute(attributeList, 3).getValue(), null, Integer.parseInt(((Map)getAttribute(attributeList, 8).getValue()).get("JobsExecuted").toString()), Class.forName((String)getAttribute(attributeList, 4).getValue()), false, false, Class.forName((String)getAttribute(attributeList, 5).getValue()), (Integer)getAttribute(attributeList, 6).getValue(), (String)getAttribute(attributeList, 7).getValue()); } catch (ClassNotFoundException e) { throw new SchedulerException(e); } }
public SchedulerMetaData getMetaData() throws SchedulerException { AttributeList attributeList = getAttributes( new String[] { "schedulerName", "schedulerInstanceId", "inStandbyMode", "shutdown", "jobStoreClass", "threadPoolClass", "threadPoolSize", "version" }); return new SchedulerMetaData( (String)attributeList.get(0), (String)attributeList.get(1), getClass(), true, isStarted(), ((Boolean)attributeList.get(2)).booleanValue(), ((Boolean)attributeList.get(3)).booleanValue(), (Date)invoke("runningSince", new Object[] {}, new String[] {}), ((Integer)invoke("numJobsExecuted", new Object[] {}, new String[] {})).intValue(), (Class)attributeList.get(4), ((Boolean)invoke("supportsPersistence", new Object[] {}, new String[] {})).booleanValue(), ((Boolean)invoke("isClustered", new Object[] {}, new String[] {})).booleanValue(), (Class)attributeList.get(5), ((Integer)attributeList.get(6)).intValue(), (String)attributeList.get(7)); }
/** * Sets the values of an array of attributes of this ModelMBean. * Executes the setAttribute() method for each attribute in the list. * * @param attributes A list of attributes: The identification of the * attributes to be set and the values they are to be set to. * * @return The array of attributes that were set, with their new * values in Attribute instances. * * @exception RuntimeOperationsException Wraps an * {@link IllegalArgumentException}: The object name in parameter * is null or attributes in parameter is null. * * @see #getAttributes **/ public AttributeList setAttributes(AttributeList attributes) { if (MODELMBEAN_LOGGER.isLoggable(Level.TRACE)) { MODELMBEAN_LOGGER.log(Level.TRACE, "Entry"); } if (attributes == null) throw new RuntimeOperationsException(new IllegalArgumentException("attributes must not be null"), "Exception occurred trying to set attributes of a "+ "RequiredModelMBean"); final AttributeList responseList = new AttributeList(); // Go through the list of attributes for (Attribute attr : attributes.asList()) { try { setAttribute(attr); responseList.add(attr); } catch (Exception excep) { responseList.remove(attr); } } return responseList; }
/** * Makes it possible to get the values of several attributes of * the MBeanServerDelegate. * * @param attributes A list of the attributes to be retrieved. * * @return The list of attributes retrieved. * */ public AttributeList getAttributes(String[] attributes) { // If attributes is null, the get all attributes. // final String[] attn = (attributes==null?attributeNames:attributes); // Prepare the result list. // final int len = attn.length; final AttributeList list = new AttributeList(len); // Get each requested attribute. // for (int i=0;i<len;i++) { try { final Attribute a = new Attribute(attn[i],getAttribute(attn[i])); list.add(a); } catch (Exception x) { // Skip the attribute that couldn't be obtained. // if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) { MBEANSERVER_LOGGER.logp(Level.FINEST, MBeanServerDelegateImpl.class.getName(), "getAttributes", "Attribute " + attn[i] + " not found"); } } } // Finally return the result. // return list; }
private static void doOp(AttributeList alist, Op op) { Object x = "oops"; switch (op) { case ADD: alist.add(x); break; case ADD_AT: alist.add(0, x); break; case ADD_ALL: alist.add(Collections.singleton(x)); break; case ADD_ALL_AT: alist.add(0, Collections.singleton(x)); break; case SET: alist.set(0, x); break; default: throw new AssertionError("Case not covered"); } }
private Long getLongValue(AttributeList attributes, String name) { List<Attribute> _attributes = attributes.asList(); for (Attribute attr : _attributes) { if (attr.getName().equalsIgnoreCase(name)) { return (Long) attr.getValue(); } } return 0L; }
public final AttributeList getAttributes(String[] attributes) { final AttributeList result = new AttributeList(attributes.length); for (String attrName : attributes) { try { final Object attrValue = getAttribute(attrName); result.add(new Attribute(attrName, attrValue)); } catch (Exception e) { // OK: attribute is not included in returned list, per spec // XXX: log the exception } } return result; }
/** * 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; }
/** * Sets the values of an array of attributes of this ModelMBean. * Executes the setAttribute() method for each attribute in the list. * * @param attributes A list of attributes: The identification of the * attributes to be set and the values they are to be set to. * * @return The array of attributes that were set, with their new * values in Attribute instances. * * @exception RuntimeOperationsException Wraps an * {@link IllegalArgumentException}: The object name in parameter * is null or attributes in parameter is null. * * @see #getAttributes **/ public AttributeList setAttributes(AttributeList attributes) { if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) { MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), "setAttribute(Attribute)", "Entry"); } if (attributes == null) throw new RuntimeOperationsException(new IllegalArgumentException("attributes must not be null"), "Exception occurred trying to set attributes of a "+ "RequiredModelMBean"); final AttributeList responseList = new AttributeList(); // Go through the list of attributes for (Attribute attr : attributes.asList()) { try { setAttribute(attr); responseList.add(attr); } catch (Exception excep) { responseList.remove(attr); } } return responseList; }
/** * Creates and adds a generic GaugeMonitor for an attribute of the MemberMXBean. * * @param attribute the attribute to monitor. * @param highThreshold the high threshold trigger. * @param lowThreshold the low threshold trigger. */ protected void setupOffHeapMonitor(String attribute, long highThreshold, long lowThreshold) { ObjectName memberMBeanObjectName = MBeanJMXAdapter.getMemberMBeanName( InternalDistributedSystem.getConnectedInstance().getDistributedMember()); assertNotNull(memberMBeanObjectName); try { ObjectName offHeapMonitorName = new ObjectName("monitors:type=Gauge,attr=" + attribute); mbeanServer.createMBean("javax.management.monitor.GaugeMonitor", offHeapMonitorName); AttributeList al = new AttributeList(); al.add(new Attribute("ObservedObject", memberMBeanObjectName)); al.add(new Attribute("GranularityPeriod", 500)); al.add(new Attribute("ObservedAttribute", attribute)); al.add(new Attribute("Notify", true)); al.add(new Attribute("NotifyHigh", true)); al.add(new Attribute("NotifyLow", true)); al.add(new Attribute("HighTheshold", highThreshold)); al.add(new Attribute("LowThreshold", lowThreshold)); mbeanServer.setAttributes(offHeapMonitorName, al); mbeanServer.addNotificationListener(offHeapMonitorName, notificationListener, null, null); mbeanServer.invoke(offHeapMonitorName, "start", new Object[] {}, new String[] {}); } catch (Exception e) { fail(e.getMessage()); } }
public AttributeList setAttributes(AttributeList attributes) { // Check attributes is not null to avoid NullPointerException later on // if (attributes != null) { AttributeList resultList = new AttributeList(); // If attributeNames is empty, nothing more to do // if (attributes.isEmpty()) return resultList; // For each attribute, try to set it and add to the result list if // successfull // 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(Exception e) { e.printStackTrace(); } } return resultList; } return null; }
public AttributeList getAttributes(String[] attributes) { AttributeList list = new AttributeList(); for (String attr : attributes) { try { list.add(new Attribute(attr, getAttribute(attr))); } catch (Exception e) { // OK: ignore per spec } } return list; }