use of javax.management.Attribute in project neo4j by neo4j.
the class Dbinfo method printAttribute.
private void printAttribute(JSONObject json, Object value) throws RemoteException, ShellException {
try {
Attribute attribute = (Attribute) value;
Object attributeValue = attribute.getValue();
if (attributeValue != null && attributeValue.getClass().isArray()) {
Object[] arrayValue = (Object[]) attributeValue;
JSONArray array = new JSONArray();
for (Object item : (Object[]) arrayValue) {
if (item instanceof CompositeData) {
array.put(compositeDataAsMap((CompositeData) item));
} else {
array.put(item.toString());
}
}
json.put(attribute.getName(), array);
} else {
json.put(attribute.getName(), attributeValue);
}
} catch (JSONException e) {
throw ShellException.wrapCause(e);
}
}
use of javax.management.Attribute in project spring-framework by spring-projects.
the class MBeanClientInterceptor method invokeAttribute.
private Object invokeAttribute(PropertyDescriptor pd, MethodInvocation invocation) throws JMException, IOException {
String attributeName = JmxUtils.getAttributeName(pd, this.useStrictCasing);
MBeanAttributeInfo inf = this.allowedAttributes.get(attributeName);
// 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");
}
}
use of javax.management.Attribute in project spring-framework by spring-projects.
the class MBeanExporterTests method testWithExposeClassLoader.
@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());
}
use of javax.management.Attribute in project spring-framework by spring-projects.
the class NotificationListenerTests method testRegisterNotificationListenerWithBeanNameBeforeObjectNameMappedToSameBeanInstance.
@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"));
}
use of javax.management.Attribute in project spring-framework by spring-projects.
the class NotificationListenerTests method testRegisterNotificationListenerWithHandback.
@Test
public void testRegisterNotificationListenerWithHandback() throws Exception {
String objectName = "spring:name=Test";
JmxTestBean bean = new JmxTestBean();
Map<String, Object> beans = new HashMap<>();
beans.put(objectName, bean);
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
Object handback = new Object();
NotificationListenerBean listenerBean = new NotificationListenerBean();
listenerBean.setNotificationListener(listener);
listenerBean.setMappedObjectName("spring:name=Test");
listenerBean.setHandback(handback);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(server);
exporter.setBeans(beans);
exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean });
start(exporter);
// update the attribute
String attributeName = "Name";
server.setAttribute(ObjectNameManager.getInstance("spring:name=Test"), new Attribute(attributeName, "Rob Harrop"));
assertEquals("Listener not notified", 1, listener.getCount(attributeName));
assertEquals("Handback object not transmitted correctly", handback, listener.getLastHandback(attributeName));
}
Aggregations