use of javax.management.AttributeChangeNotification in project camel by apache.
the class NotificationXmlFormatter method format.
public String format(Notification aNotification) throws NotificationFormatException {
ObjectHelper.notNull(jaxbContext, "jaxbContext");
NotificationEventType jaxb;
boolean wrap = false;
if (aNotification instanceof AttributeChangeNotification) {
AttributeChangeNotification ac = (AttributeChangeNotification) aNotification;
jaxb = mObjectFactory.createAttributeChangeNotification().withAttributeName(ac.getAttributeName()).withAttributeType(ac.getAttributeType()).withNewValue(ac.getNewValue() == null ? null : String.valueOf(ac.getNewValue())).withOldValue(ac.getOldValue() == null ? null : String.valueOf(ac.getOldValue()));
} else if (aNotification instanceof JMXConnectionNotification) {
jaxb = mObjectFactory.createJMXConnectionNotification().withConnectionId(((JMXConnectionNotification) aNotification).getConnectionId());
} else if (aNotification instanceof MBeanServerNotification) {
jaxb = mObjectFactory.createMBeanServerNotification().withMBeanName(String.valueOf(((MBeanServerNotification) aNotification).getMBeanName()));
} else if (aNotification instanceof MonitorNotification) {
MonitorNotification mn = (MonitorNotification) aNotification;
jaxb = mObjectFactory.createMonitorNotification().withDerivedGauge(String.valueOf(mn.getDerivedGauge())).withObservedAttribute(mn.getObservedAttribute()).withObservedObject(String.valueOf(mn.getObservedObject())).withTrigger(String.valueOf(mn.getTrigger()));
} else if (aNotification instanceof RelationNotification) {
RelationNotification rn = (RelationNotification) aNotification;
jaxb = mObjectFactory.createRelationNotification().withObjectName(String.valueOf(rn.getObjectName())).withRelationId(rn.getRelationId()).withRelationTypeName(rn.getRelationTypeName()).withRoleName(rn.getRoleName());
if (rn.getNewRoleValue() != null) {
ObjectNamesType ont = toObjectNamesType(rn.getNewRoleValue());
((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withNewRoleValue(ont);
}
if (rn.getOldRoleValue() != null) {
ObjectNamesType ont = toObjectNamesType(rn.getOldRoleValue());
((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withOldRoleValue(ont);
}
if (rn.getMBeansToUnregister() != null) {
ObjectNamesType ont = toObjectNamesType(rn.getMBeansToUnregister());
((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withMBeansToUnregister(ont);
}
} else if (aNotification instanceof TimerNotification) {
jaxb = mObjectFactory.createTimerNotification().withNotificationId(((TimerNotification) aNotification).getNotificationID());
} else {
jaxb = mObjectFactory.createNotificationEventType();
wrap = true;
}
// add all of the common properties
jaxb.withMessage(aNotification.getMessage()).withSequence(aNotification.getSequenceNumber()).withSource(String.valueOf(aNotification.getSource())).withTimestamp(aNotification.getTimeStamp()).withType(aNotification.getType());
if (aNotification.getUserData() != null) {
jaxb.withUserData(String.valueOf(aNotification.getUserData()));
}
try {
DatatypeFactory df = getDatatypeFactory();
Date date = new Date(aNotification.getTimeStamp());
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
jaxb.withDateTime(df.newXMLGregorianCalendar(gc));
Object bean = wrap ? mObjectFactory.createNotificationEvent(jaxb) : jaxb;
StringWriter sw = new StringWriter();
// must create a new marshaller as its not thread safe
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(bean, sw);
return sw.toString();
} catch (JAXBException e) {
throw new NotificationFormatException(e);
} catch (DatatypeConfigurationException e) {
throw new NotificationFormatException(e);
}
}
use of javax.management.AttributeChangeNotification in project spring-framework by spring-projects.
the class ModelMBeanNotificationPublisherTests method testSendAttributeChangeNotificationWhereSourceIsNotTheManagedResource.
public void testSendAttributeChangeNotificationWhereSourceIsNotTheManagedResource() throws Exception {
StubSpringModelMBean mbean = new StubSpringModelMBean();
Notification notification = new AttributeChangeNotification(this, 1872, System.currentTimeMillis(), "Shall we break for some tea?", "agree", "java.lang.Boolean", Boolean.FALSE, Boolean.TRUE);
ObjectName objectName = createObjectName();
NotificationPublisher publisher = new ModelMBeanNotificationPublisher(mbean, objectName, mbean);
publisher.sendNotification(notification);
assertNotNull(mbean.getActualNotification());
assertTrue(mbean.getActualNotification() instanceof AttributeChangeNotification);
assertSame("The exact same Notification is not being passed through from the publisher to the mbean.", notification, mbean.getActualNotification());
assertSame("The 'source' property of the Notification is *wrongly* being set to the ObjectName of the associated MBean.", this, mbean.getActualNotification().getSource());
}
use of javax.management.AttributeChangeNotification in project jdk8u_jdk by JetBrains.
the class CommunicatorServer method sendStateChangeNotification.
/**
*
*/
private void sendStateChangeNotification(int oldState, int newState) {
String oldStateString = getStringForState(oldState);
String newStateString = getStringForState(newState);
String message = new StringBuffer().append(dbgTag).append(" The value of attribute State has changed from ").append(oldState).append(" (").append(oldStateString).append(") to ").append(newState).append(" (").append(newStateString).append(").").toString();
notifCount++;
AttributeChangeNotification notif = new // source
AttributeChangeNotification(// source
this, // sequence number
notifCount, // time stamp
System.currentTimeMillis(), // message
message, // attribute name
"State", // attribute type
"int", // old value
new Integer(oldState), // new value
new Integer(newState));
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "sendStateChangeNotification", "Sending AttributeChangeNotification #" + notifCount + " with message: " + message);
}
notifBroadcaster.sendNotification(notif);
}
use of javax.management.AttributeChangeNotification in project jdk8u_jdk by JetBrains.
the class ScanManagerTest method doTestOperation.
/**
* Test of addNotificationListener method, of class com.sun.jmx.examples.scandir.ScanManager.
*/
private void doTestOperation(ScanManagerMXBean proxy, Call op, EnumSet<ScanState> after, String testName) throws Exception {
System.out.println("doTestOperation: " + testName);
final LinkedBlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
NotificationListener listener = new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
try {
queue.put(notification);
} catch (Exception x) {
System.err.println("Failed to queue notif: " + x);
}
}
};
NotificationFilter filter = null;
Object handback = null;
final ScanState before;
final NotificationEmitter emitter = (NotificationEmitter) proxy;
emitter.addNotificationListener(listener, filter, handback);
before = proxy.getState();
op.call();
try {
final Notification notification = queue.poll(3000, TimeUnit.MILLISECONDS);
assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, notification.getType());
assertEquals(AttributeChangeNotification.class, notification.getClass());
assertEquals(ScanManager.SCAN_MANAGER_NAME, notification.getSource());
AttributeChangeNotification acn = (AttributeChangeNotification) notification;
assertEquals("State", acn.getAttributeName());
assertEquals(ScanState.class.getName(), acn.getAttributeType());
assertEquals(before, ScanState.valueOf((String) acn.getOldValue()));
assertContained(after, ScanState.valueOf((String) acn.getNewValue()));
emitter.removeNotificationListener(listener, filter, handback);
} finally {
try {
op.cancel();
} catch (Exception x) {
System.err.println("Failed to cleanup: " + x);
}
}
}
use of javax.management.AttributeChangeNotification in project jdk8u_jdk by JetBrains.
the class DirectoryScannerTest method doTestOperation.
private void doTestOperation(DirectoryScannerMXBean proxy, Call op, EnumSet<ScanState> after, String testName) throws Exception {
System.out.println("doTestOperation: " + testName);
final LinkedBlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
NotificationListener listener = new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
try {
queue.put(notification);
} catch (Exception x) {
System.err.println("Failed to queue notif: " + x);
}
}
};
NotificationFilter filter = null;
Object handback = null;
final ScanState before;
final NotificationEmitter emitter = (NotificationEmitter) makeNotificationEmitter(proxy, DirectoryScannerMXBean.class);
emitter.addNotificationListener(listener, filter, handback);
before = proxy.getState();
op.call();
try {
final Notification notification = queue.poll(3000, TimeUnit.MILLISECONDS);
assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, notification.getType());
assertEquals(AttributeChangeNotification.class, notification.getClass());
assertEquals(getObjectName(proxy), notification.getSource());
AttributeChangeNotification acn = (AttributeChangeNotification) notification;
assertEquals("State", acn.getAttributeName());
assertEquals(ScanState.class.getName(), acn.getAttributeType());
assertEquals(before, ScanState.valueOf((String) acn.getOldValue()));
assertContained(after, ScanState.valueOf((String) acn.getNewValue()));
emitter.removeNotificationListener(listener, filter, handback);
} finally {
try {
op.cancel();
} catch (Exception x) {
System.err.println("Failed to cleanup: " + x);
}
}
}
Aggregations