@RemoteMethod(description = "Get current server tps", impact = ModelMBeanOperationInfo.ACTION) public double tps() { try { double result = 0; double[] recentTps = (double[]) recentTpsField.get(getServerMethod.invoke(null)); for(double one : recentTps) result += one; return result / recentTps.length; } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return 0; }
@Test public void testAttributeHasCorrespondingOperations() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanOperationInfo get = info.getOperation("getName"); assertNotNull("get operation should not be null", get); assertEquals("get operation should have visibility of four", get.getDescriptor().getFieldValue("visibility"), new Integer(4)); assertEquals("get operation should have role \"getter\"", "getter", get.getDescriptor().getFieldValue("role")); ModelMBeanOperationInfo set = info.getOperation("setName"); assertNotNull("set operation should not be null", set); assertEquals("set operation should have visibility of four", set.getDescriptor().getFieldValue("visibility"), new Integer(4)); assertEquals("set operation should have role \"setter\"", "setter", set.getDescriptor().getFieldValue("role")); }
@Override public MBeanInfo getMBeanInfo() { try { ModelMBeanAttributeInfo[] attributes = new ModelMBeanAttributeInfo[0]; ModelMBeanConstructorInfo[] constructors = new ModelMBeanConstructorInfo[] { new ModelMBeanConstructorInfo("-", this.getClass().getConstructor()) }; ModelMBeanOperationInfo[] operations = new ModelMBeanOperationInfo[] { new ModelMBeanOperationInfo("info", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.INFO), new ModelMBeanOperationInfo("action", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.ACTION), new ModelMBeanOperationInfo("actionInfo", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.ACTION_INFO), new ModelMBeanOperationInfo("unknown", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.UNKNOWN) }; ModelMBeanNotificationInfo[] notifications = new ModelMBeanNotificationInfo[0]; return new ModelMBeanInfoSupport(this.getClass().getName(), "-", attributes, constructors, operations, notifications); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Verify that returned value of invoked method never retrieves value from * cache if currencyTimeLimit is not defended in descriptor of * ModelMBeanOperationInfo. * <p> * Instructions are the same as in testNegative. */ public Result testNotPresent() throws Exception { Method method = class1.getDeclaredMethod("simpleMethod", null); ModelMBeanOperationInfo operationInfo1 = new ModelMBeanOperationInfo( "description", method); ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport( class1.getName(), "description", null, null, new ModelMBeanOperationInfo[] { operationInfo1 }, null); RequiredModelMBean requiredModelMBean = new RequiredModelMBean( beanInfoSupport); requiredModelMBean.setManagedResource(this, "ObjectReference"); ObjectName objectName = new ObjectName("domain", "name", "simple name"); MBeanServer server = MBeanServerFactory.createMBeanServer(); server.registerMBean(requiredModelMBean, objectName); Object value = server.invoke(objectName, method.getName(), null, null); assertEquals(value, returnedObject); assertTrue(isInvokedMethod()); ModelMBeanOperationInfo operationInfo2 = (ModelMBeanOperationInfo)server .getMBeanInfo(objectName).getOperations()[0]; assertTrue(operationInfo1 == operationInfo2); value = server.invoke(objectName, method.getName(), null, null); assertEquals(value, returnedObject); assertTrue(isInvokedMethod()); return result(); }
/** * Verify default fields of descriptor from ModelMBeanOperationInfo: * name=nameOfMethod, displayName=nameOfMethod, role=operation and * descriptorType=operation. */ public Result testModelMBeanOperationInfo() throws Exception { Method method = sampleClass.getMethod("sayOk", null); ModelMBeanOperationInfo operationInfo = new ModelMBeanOperationInfo( "description", method); descriptor = operationInfo.getDescriptor(); String name = (String)descriptor.getFieldValue("name"); assertEquals(name, descriptor.getFieldValue("Name")); assertEquals(name, method.getName()); assertEquals(descriptor.getFieldValue("displayName"), method.getName()); assertEquals(descriptor.getFieldValue("role"), "operation"); assertEquals(descriptor.getFieldValue("descriptorType"), "operation"); assertEquals(descriptor.getFields().length, 4); commonCheck(); return result(); }
/** * Builds an operationInfor for getter purposes. * @param name * @param klass * @param description * @param signature */ public static ModelMBeanOperationInfo buildGetterModelMBeanOperationInfo( String name, String klass, String description, String signature) { Descriptor theDescriptor = buildGetterDescriptor(name, klass); return new ModelMBeanOperationInfo( name, description, null, signature, ModelMBeanOperationInfo.INFO, theDescriptor); }
/** * Creates an instance of {@code ModelMBeanOperationInfo} for the * given method. Populates the parameter info for the operation. * @param method the {@code Method} to create a {@code ModelMBeanOperationInfo} for * @param name the logical name for the operation (method name or property name); * not used by the default implementation but possibly by subclasses * @param beanKey the key associated with the MBean in the beans map * of the {@code MBeanExporter} * @return the {@code ModelMBeanOperationInfo} */ protected ModelMBeanOperationInfo createModelMBeanOperationInfo(Method method, String name, String beanKey) { MBeanParameterInfo[] params = getOperationParameters(method, beanKey); if (params.length == 0) { return new ModelMBeanOperationInfo(getOperationDescription(method, beanKey), method); } else { return new ModelMBeanOperationInfo(method.getName(), getOperationDescription(method, beanKey), getOperationParameters(method, beanKey), method.getReturnType().getName(), MBeanOperationInfo.UNKNOWN); } }
@RemoteMethod(description = "Add a player to the whitelist", impact = ModelMBeanOperationInfo.ACTION) public void addPlayer(UUID player) { Bukkit.getWhitelistedPlayers().add(Bukkit.getOfflinePlayer(player)); Bukkit.reloadWhitelist(); Bukkit.getLogger().info("Added player " + player + " to whitelist"); }
@RemoteMethod(description = "Remove a player to the whitelist", impact = ModelMBeanOperationInfo.ACTION) public void removePlayer(UUID player) { Bukkit.getWhitelistedPlayers().add(Bukkit.getOfflinePlayer(player)); Bukkit.reloadWhitelist(); Bukkit.getLogger().info("Added player " + player + " to whitelist"); }
@Test public void testOperationParameterMetadata() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanOperationInfo oper = info.getOperation("add"); MBeanParameterInfo[] params = oper.getSignature(); assertEquals("Invalid number of params", 2, params.length); assertEquals("Incorrect name for x param", "x", params[0].getName()); assertEquals("Incorrect type for x param", int.class.getName(), params[0].getType()); assertEquals("Incorrect name for y param", "y", params[1].getName()); assertEquals("Incorrect type for y param", int.class.getName(), params[1].getType()); }
@Test public void testMetricDescription() throws Exception { ModelMBeanInfo inf = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo metric = inf.getAttribute(QUEUE_SIZE_METRIC); ModelMBeanOperationInfo operation = inf.getOperation("getQueueSize"); assertEquals("The description for the queue size metric is incorrect", "The QueueSize metric", metric.getDescription()); assertEquals("The description for the getter operation of the queue size metric is incorrect", "The QueueSize metric", operation.getDescription()); }
private void extractMbeanOperations(Object managedBean, Set<ManagedOperationInfo> operations, Set<ModelMBeanOperationInfo> mBeanOperations) { for (ManagedOperationInfo info : operations) { ModelMBeanOperationInfo mbean = new ModelMBeanOperationInfo(info.getDescription(), info.getOperation()); Descriptor opDesc = mbean.getDescriptor(); mbean.setDescriptor(opDesc); mBeanOperations.add(mbean); LOGGER.trace("Assembled operation: {}", mbean); } }
private void extractMbeanOperations(Object managedBean, Set<ManagedOperationInfo> operations, Set<ModelMBeanOperationInfo> mBeanOperations) { for (ManagedOperationInfo info : operations) { ModelMBeanOperationInfo mbean = new ModelMBeanOperationInfo(info.getDescription(), info.getOperation()); Descriptor opDesc = mbean.getDescriptor(); opDesc.setField("mask", info.isMask() ? "true" : "false"); mbean.setDescriptor(opDesc); mBeanOperations.add(mbean); LOG.trace("Assembled operation: {}", mbean); } }
/** * Do 1-6 steps. */ private ModelMBeanInfoSupport constractModelMBeanInfoSupport() throws Exception { ModelMBeanOperationInfo operationInfo = new ModelMBeanOperationInfo( "description", class1.getMethod("simpleOperartion", null)); setDescriptor(operationInfo); ModelMBeanConstructorInfo constructorInfo = new ModelMBeanConstructorInfo( "description", class1.getConstructor(null)); setDescriptor(constructorInfo); ModelMBeanAttributeInfo attributeInfo = new ModelMBeanAttributeInfo( "name", "description", class1.getMethod("getH", null), class1 .getMethod("setH", new Class[] { int.class })); setDescriptor(attributeInfo); ModelMBeanNotificationInfo notificationInfo = new ModelMBeanNotificationInfo( new String[] { "specific notification tepes" }, "name", "description"); setDescriptor(notificationInfo); ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport( class1.getName(), "description", new ModelMBeanAttributeInfo[] { attributeInfo }, new ModelMBeanConstructorInfo[] { constructorInfo }, new ModelMBeanOperationInfo[] { operationInfo }, new ModelMBeanNotificationInfo[] { notificationInfo }); Descriptor descriptor = beanInfoSupport.getMBeanDescriptor(); String[] strings = getSpesific(beanInfoSupport.getClass()); descriptor.setField(strings[0], strings[1]); map.put(beanInfoSupport.getClass().getName(), descriptor); beanInfoSupport.setMBeanDescriptor(descriptor); return beanInfoSupport; }
/** * Verify that invoke method throws exception if target operation method * throws exception. * <ul> * Step by step: * <li>Create operation method with one string parameter which always * throws an exception with message=parameter of this method. * <li>Create ModelMBeanOperationInfo object for operation method. * <li>Set value currencyTimeLimit = 0 in descriptor for * ModelMBeanOperationInfo object. * <li>Create ModelMBeanInfoSupport object with default descriptor. All * ModelMBeanXXXInfo except ModelMBeanOperationInfo are default. * <li>Instance of class created in 1st step sets managed resource for * RequiredModelMBean using setManagedResource method. * <li>Create ObjectName object. * <li>Register RequiredModelMBean object in MBeanServer with above * ObjectName. * <li>Invoke operation methodThrowException method thru invoke method of * MBeanServer with specific msg. * <li>Verify that MBeanException was thrown with nested exception which * has message which specified in previous step. * </ul> */ public Result testException() throws Exception { Method method = class1.getMethod("methodThrowException", new Class[] { String.class }); ModelMBeanOperationInfo operationInfo1 = new ModelMBeanOperationInfo( "description", method); Descriptor descriptor = operationInfo1.getDescriptor(); descriptor.setField("currencyTimeLimit", "0"); operationInfo1.setDescriptor(descriptor); ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport( class1.getName(), "description", null, null, new ModelMBeanOperationInfo[] { operationInfo1 }, null); RequiredModelMBean requiredModelMBean = new RequiredModelMBean( beanInfoSupport); requiredModelMBean.setManagedResource(this, "ObjectReference"); ObjectName objectName = new ObjectName("domain", "name", "simple name"); MBeanServer server = MBeanServerFactory.createMBeanServer(); server.registerMBean(requiredModelMBean, objectName); try { server.invoke(objectName, method.getName(), new Object[] { "message" }, new String[] { String.class .getName() }); assertTrue(false); } catch (MBeanException e) { assertEquals(e.getCause().getMessage(), "message"); } assertTrue(isInvokedMethod()); return result(); }
/** * Verify that invoke method never caches returned value of method if * currencyTimeLimit < 0. * <ul> * Step by step: * <li>Create operation method without parameters which always returns the * same value. * <li>Create ModelMBeanOperationInfo object for operation method. * <li>Set value currencyTimeLimit <0 in descriptor for * ModelMBeanOperationInfo object. * <li>Create ModelMBeanInfoSupport object with default descriptor. All * ModelMBeanXXXInfo except ModelMBeanOperationInfo are default. * <li>Create RequiredModelMBean object. * <li>Instance of class created in 1st step sets managed resource for * RequiredModelMBean using setManagedResource method. * <li>Create ObjectName object. * <li>Register RequiredModelMBean object in MBeanServer with above * ObjectName. * <li>Invoke operation method thru invoke method of MBeanServer. * <li>Verify value which the invoke method returned is the same as value * which the operation method returned. * <li>Verify that operation method was invoked. * <li>Invoke again operation method thru invoke method of MBeanServer. * <li>Verify value which the invoke method returned is the same as value * which the operation method returned. * <li>Verify that operation method was invoked. * </ul> */ public Result testNegative() throws Exception { Method method = class1.getDeclaredMethod("simpleMethod", null); ModelMBeanOperationInfo operationInfo1 = new ModelMBeanOperationInfo( "description", method); Descriptor descriptor = operationInfo1.getDescriptor(); descriptor.setField("currencyTimeLimit", "-1"); operationInfo1.setDescriptor(descriptor); ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport( class1.getName(), "description", null, null, new ModelMBeanOperationInfo[] { operationInfo1 }, null); RequiredModelMBean requiredModelMBean = new RequiredModelMBean( beanInfoSupport); requiredModelMBean.setManagedResource(this, "ObjectReference"); ObjectName objectName = new ObjectName("domain", "name", "simple name"); MBeanServer server = MBeanServerFactory.createMBeanServer(); server.registerMBean(requiredModelMBean, objectName); Object value = server.invoke(objectName, method.getName(), null, null); assertEquals(value, returnedObject); assertTrue(isInvokedMethod()); ModelMBeanOperationInfo operationInfo2 = (ModelMBeanOperationInfo)server .getMBeanInfo(objectName).getOperations()[0]; assertTrue(operationInfo1 == operationInfo2); value = server.invoke(objectName, method.getName(), null, null); assertEquals(value, returnedObject); assertTrue(isInvokedMethod()); return result(); }
/** * Extract all operations and attributes from the given object that have * been annotated with the Jmx annotation. Operations are all methods that * are marked with the JmxOperation annotation. * * @param object The object to process * @return An array of operations taken from the object */ public static ModelMBeanOperationInfo[] extractOperationInfo(Object object) { ArrayList<ModelMBeanOperationInfo> infos = new ArrayList<ModelMBeanOperationInfo>(); for(Method m: object.getClass().getMethods()) { JmxOperation jmxOperation = m.getAnnotation(JmxOperation.class); JmxGetter jmxGetter = m.getAnnotation(JmxGetter.class); JmxSetter jmxSetter = m.getAnnotation(JmxSetter.class); if(jmxOperation != null || jmxGetter != null || jmxSetter != null) { String description = ""; int visibility = 1; int impact = MBeanOperationInfo.UNKNOWN; if(jmxOperation != null) { description = jmxOperation.description(); impact = jmxOperation.impact(); } else if(jmxGetter != null) { description = jmxGetter.description(); impact = MBeanOperationInfo.INFO; visibility = 4; } else if(jmxSetter != null) { description = jmxSetter.description(); impact = MBeanOperationInfo.ACTION; visibility = 4; } ModelMBeanOperationInfo info = new ModelMBeanOperationInfo(m.getName(), description, extractParameterInfo(m), m.getReturnType() .getName(), impact); info.getDescriptor().setField("visibility", Integer.toString(visibility)); infos.add(info); } } return infos.toArray(new ModelMBeanOperationInfo[infos.size()]); }
@RemoteMethod(description = "Get the whitelist", impact = ModelMBeanOperationInfo.ACTION) public ArrayList<UUID> getWhiteList() { return Bukkit.getWhitelistedPlayers().stream().map(OfflinePlayer::getUniqueId).collect(Collectors.toCollection(ArrayList::new)); }
@RemoteMethod(description = "Know if the whitelist is actived", impact = ModelMBeanOperationInfo.INFO) public boolean isWhiteListActived() { return Bukkit.hasWhitelist(); }
@RemoteMethod(description = "Set if the whitelist is actived", impact = ModelMBeanOperationInfo.ACTION) public void setWhiteListActived(boolean actived) { Bukkit.setWhitelist(actived); }
@RemoteMethod(description = "Shutdown a server", impact = ModelMBeanOperationInfo.ACTION) public void stop() { Bukkit.getServer().shutdown(); }
@Test public void testOperationFromInterface() throws Exception { ModelMBeanInfo inf = getMBeanInfoFromAssembler(); ModelMBeanOperationInfo op = inf.getOperation("fromInterface"); assertNotNull(op); }
@Test public void testOperationOnGetter() throws Exception { ModelMBeanInfo inf = getMBeanInfoFromAssembler(); ModelMBeanOperationInfo op = inf.getOperation("getExpensiveToCalculate"); assertNotNull(op); }
public ModelMBeanInfo getMBeanInfo(Object defaultManagedBean, Object customManagedBean, String objectName) throws JMException { if ((defaultManagedBean == null && customManagedBean == null) || objectName == null) return null; // skip proxy classes if (defaultManagedBean != null && Proxy.isProxyClass(defaultManagedBean.getClass())) { LOGGER.trace("Skip creating ModelMBeanInfo due proxy class {}", defaultManagedBean.getClass()); return null; } // maps and lists to contain information about attributes and operations Map<String, ManagedAttributeInfo> attributes = new LinkedHashMap<>(); Set<ManagedOperationInfo> operations = new LinkedHashSet<>(); Set<ModelMBeanAttributeInfo> mBeanAttributes = new LinkedHashSet<>(); Set<ModelMBeanOperationInfo> mBeanOperations = new LinkedHashSet<>(); Set<ModelMBeanNotificationInfo> mBeanNotifications = new LinkedHashSet<>(); // extract details from default managed bean if (defaultManagedBean != null) { extractAttributesAndOperations(defaultManagedBean.getClass(), attributes, operations); extractMbeanAttributes(defaultManagedBean, attributes, mBeanAttributes, mBeanOperations); extractMbeanOperations(defaultManagedBean, operations, mBeanOperations); extractMbeanNotifications(defaultManagedBean, mBeanNotifications); } // extract details from custom managed bean if (customManagedBean != null) { extractAttributesAndOperations(customManagedBean.getClass(), attributes, operations); extractMbeanAttributes(customManagedBean, attributes, mBeanAttributes, mBeanOperations); extractMbeanOperations(customManagedBean, operations, mBeanOperations); extractMbeanNotifications(customManagedBean, mBeanNotifications); } // create the ModelMBeanInfo String name = getName(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName); String description = getDescription(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName); ModelMBeanAttributeInfo[] arrayAttributes = mBeanAttributes.toArray(new ModelMBeanAttributeInfo[mBeanAttributes.size()]); ModelMBeanOperationInfo[] arrayOperations = mBeanOperations.toArray(new ModelMBeanOperationInfo[mBeanOperations.size()]); ModelMBeanNotificationInfo[] arrayNotifications = mBeanNotifications.toArray(new ModelMBeanNotificationInfo[mBeanNotifications.size()]); ModelMBeanInfo info = new ModelMBeanInfoSupport(name, description, arrayAttributes, null, arrayOperations, arrayNotifications); LOGGER.trace("Created ModelMBeanInfo {}", info); return info; }
/** * Gets the {@link ModelMBeanInfo} for the given managed bean * * @param defaultManagedBean the default managed bean * @param customManagedBean an optional custom managed bean * @param objectName the object name * @return the model info, or <tt>null</tt> if not possible to create, for example due the managed bean is a proxy class * @throws JMException is thrown if error creating the model info */ public ModelMBeanInfo getMBeanInfo(Object defaultManagedBean, Object customManagedBean, String objectName) throws JMException { // skip proxy classes if (defaultManagedBean != null && Proxy.isProxyClass(defaultManagedBean.getClass())) { LOG.trace("Skip creating ModelMBeanInfo due proxy class {}", defaultManagedBean.getClass()); return null; } // maps and lists to contain information about attributes and operations Map<String, ManagedAttributeInfo> attributes = new LinkedHashMap<String, ManagedAttributeInfo>(); Set<ManagedOperationInfo> operations = new LinkedHashSet<ManagedOperationInfo>(); Set<ModelMBeanAttributeInfo> mBeanAttributes = new LinkedHashSet<ModelMBeanAttributeInfo>(); Set<ModelMBeanOperationInfo> mBeanOperations = new LinkedHashSet<ModelMBeanOperationInfo>(); Set<ModelMBeanNotificationInfo> mBeanNotifications = new LinkedHashSet<ModelMBeanNotificationInfo>(); // extract details from default managed bean if (defaultManagedBean != null) { extractAttributesAndOperations(defaultManagedBean.getClass(), attributes, operations); extractMbeanAttributes(defaultManagedBean, attributes, mBeanAttributes, mBeanOperations); extractMbeanOperations(defaultManagedBean, operations, mBeanOperations); extractMbeanNotifications(defaultManagedBean, mBeanNotifications); } // extract details from custom managed bean if (customManagedBean != null) { extractAttributesAndOperations(customManagedBean.getClass(), attributes, operations); extractMbeanAttributes(customManagedBean, attributes, mBeanAttributes, mBeanOperations); extractMbeanOperations(customManagedBean, operations, mBeanOperations); extractMbeanNotifications(customManagedBean, mBeanNotifications); } // create the ModelMBeanInfo String name = getName(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName); String description = getDescription(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName); ModelMBeanAttributeInfo[] arrayAttributes = mBeanAttributes.toArray(new ModelMBeanAttributeInfo[mBeanAttributes.size()]); ModelMBeanOperationInfo[] arrayOperations = mBeanOperations.toArray(new ModelMBeanOperationInfo[mBeanOperations.size()]); ModelMBeanNotificationInfo[] arrayNotifications = mBeanNotifications.toArray(new ModelMBeanNotificationInfo[mBeanNotifications.size()]); ModelMBeanInfo info = new ModelMBeanInfoSupport(name, description, arrayAttributes, null, arrayOperations, arrayNotifications); LOG.trace("Created ModelMBeanInfo {}", info); return info; }
protected ModelMBeanOperationInfo[] getOperationInfo(Object managedBean, String beanKey) { managedBean = AspectUtil.exposeRootBean(managedBean); return super.getOperationInfo(managedBean, beanKey); }
private void printValue(MBeanInfo beanInfo) { ModelMBeanOperationInfo operationInfo2 = (ModelMBeanOperationInfo)beanInfo .getOperations()[0]; assertEquals(returnedObject, operationInfo2.getDescriptor() .getFieldValue("value")); }
/** * Verify that value of attribute return value which return getter method if * currencyTimeLimit < 0. * <ul> * Step by step: * <li>1.Create java class, which is not MBean. This class has getter and * setter methods. * <li>2.Create ModelMBeanAttibuteInfo object for class in 1st step. * <li>3.Extract descriptor from ModelMBeanAttibuteInfo class and set * additional fields currencyTimeLimit < 0 and setMethod=nameOfSetterMethod. * <li>4.Create ModelMBeanInfoSupport object. * <li>5.Create RequiredModelMBean object. * <li>6.Create notification listener and register it in RequiredModelMBean * object for attribute using addAttributeChangeNotificationListener. * <li>7.Instance of java class in 1st step sets managed resource for * RequiredModelMBean using setManagedResource method. * <li>8.Create objectName. * <li>9.Register RequiredModelMBean object in MBeanServer with objectName * specified in previous step. * <li>10.Set attribute on MBeanServer using setAttribute method. * <li>11.Verify that notification listener was notified about change * attribute. * <li>12.Verify that setter method was invoked. * <li>13.Verify that getAttribute of MBeanServer returns correct value of * attribute and getter method was invoked. * <li>14. Invoke again getAttribute of MBeanServer method and verify * returned value. Also verify that getter method was invoked. * </ul> */ public Result testNegative() throws Exception { Method getter = class1.getMethod("getG", null); Method setter = class1.getMethod("setG", new Class[] { String.class }); ModelMBeanAttributeInfo attributeInfoForG = new ModelMBeanAttributeInfo( "name", "description", getter, setter); Descriptor descriptor = attributeInfoForG.getDescriptor(); descriptor.setField("currencyTimeLimit", "-1"); descriptor.setField("setMethod", setter.getName()); descriptor.setField("getMethod", getter.getName()); attributeInfoForG.setDescriptor(descriptor); ModelMBeanAttributeInfo[] attributeInfos = new ModelMBeanAttributeInfo[] { attributeInfoForG }; ModelMBeanOperationInfo[] operationInfos = new ModelMBeanOperationInfo[] { new ModelMBeanOperationInfo("description", setter), new ModelMBeanOperationInfo("description", getter) }; ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport( class1.getName(), "description", attributeInfos, null, operationInfos, null); RequiredModelMBean requiredModelMBean = new RequiredModelMBean( beanInfoSupport); ManageResourceAndNotification notificationListener = new ManageResourceAndNotification(); requiredModelMBean.addAttributeChangeNotificationListener( notificationListener, attributeInfoForG.getName(), ManageResourceAndNotification.handback); ManageResourceAndNotification managedResource = new ManageResourceAndNotification(); requiredModelMBean.setManagedResource(managedResource, "ObjectReference"); ObjectName objectName = new ObjectName("domain", "name", "simple name"); MBeanServer server = MBeanServerFactory.createMBeanServer(); server.registerMBean(requiredModelMBean, objectName); String newValue = "new value"; server.setAttribute(objectName, new Attribute(attributeInfoForG .getName(), newValue)); assertTrue(notificationListener.isWasHandleNotificationInvoked()); assertEquals(managedResource.getG(), newValue); managedResource.isGetGWasInvoked(); assertEquals(server.getAttribute(objectName, attributeInfoForG .getName()), newValue); assertTrue(managedResource.isGetGWasInvoked()); assertTrue(server.getMBeanInfo(objectName).getAttributes()[0] == attributeInfoForG); assertNull(attributeInfoForG.getDescriptor().getFieldValue("value")); return result(); }
/** * Verify that value of attribute retrieves a value from cache if * currencyTimeLimit = 0. * <ul> * Step by step: * <li>1.Create java class, which is not MBean. This class has getter and * setter methods. * <li>2.Create ModelMBeanAttibuteInfo object for class in 1st step. * <li>3.Extract descriptor from ModelMBeanAttibuteInfo class and set * additional fields currencyTimeLimit = 0 and setMethod=nameOfSetterMethod. * <li>4.Create ModelMBeanInfoSupport object. * <li>5.Create RequiredModelMBean object. * <li>6.Create notification listener and register it in RequiredModelMBean * object for attribute using addAttributeChangeNotificationListener. * <li>7.Instance of java class in 1st step sets managed resource for * RequiredModelMBean using setManagedResource method. * <li>8.Create objectName. * <li>9.Register RequiredModelMBean object in MBeanServer with objectName * specified in previous step. * <li>10.Set attribute on MBeanServer using setAttribute method. * <li>11.Verify that notification listener was notified about change * attribute. * <li>12.Verify that setter method was invoked. * <li>13.Verify that getAttribute returns correct value of attribute and * getter method was not invoked. * <li>14.Verify value of field value of descriptor of * ModelMBeanAttibuteInfo. */ public Result testZero() throws Exception { Method getter = class1.getMethod("getG", null); Method setter = class1.getMethod("setG", new Class[] { String.class }); ModelMBeanAttributeInfo attributeInfoForG = new ModelMBeanAttributeInfo( "name", "description", getter, setter); Descriptor descriptor = attributeInfoForG.getDescriptor(); descriptor.setField("currencyTimeLimit", "0"); descriptor.setField("setMethod", setter.getName()); descriptor.setField("getMethod", getter.getName()); attributeInfoForG.setDescriptor(descriptor); ModelMBeanAttributeInfo[] attributeInfos = new ModelMBeanAttributeInfo[] { attributeInfoForG }; ModelMBeanOperationInfo[] operationInfos = new ModelMBeanOperationInfo[] { new ModelMBeanOperationInfo("description", setter), new ModelMBeanOperationInfo("description", getter) }; ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport( class1.getName(), "description", attributeInfos, null, operationInfos, null); RequiredModelMBean requiredModelMBean = new RequiredModelMBean( beanInfoSupport); ManageResourceAndNotification notificationListener = new ManageResourceAndNotification(); requiredModelMBean.addAttributeChangeNotificationListener( notificationListener, attributeInfoForG.getName(), ManageResourceAndNotification.handback); ManageResourceAndNotification managedResource = new ManageResourceAndNotification(); requiredModelMBean.setManagedResource(managedResource, "ObjectReference"); ObjectName objectName = new ObjectName("domain", "name", "simple name"); MBeanServer server = MBeanServerFactory.createMBeanServer(); server.registerMBean(requiredModelMBean, objectName); String newValue = "new value"; server.setAttribute(objectName, new Attribute(attributeInfoForG .getName(), newValue)); assertTrue(notificationListener.isWasHandleNotificationInvoked()); assertEquals(managedResource.getG(), newValue); managedResource.isGetGWasInvoked(); assertEquals(server.getAttribute(objectName, attributeInfoForG .getName()), newValue); assertFalse(managedResource.isGetGWasInvoked()); assertTrue(server.getMBeanInfo(objectName).getAttributes()[0] == attributeInfoForG); assertEquals(attributeInfoForG.getDescriptor().getFieldValue("value"), newValue); return result(); }