Search in sources :

Example 16 with AttributeChangeNotification

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);
    }
}
Also used : MonitorNotification(javax.management.monitor.MonitorNotification) Marshaller(javax.xml.bind.Marshaller) AttributeChangeNotification(javax.management.AttributeChangeNotification) DatatypeFactory(javax.xml.datatype.DatatypeFactory) MBeanServerNotification(javax.management.MBeanServerNotification) JAXBException(javax.xml.bind.JAXBException) GregorianCalendar(java.util.GregorianCalendar) RelationNotification(javax.management.relation.RelationNotification) TimerNotification(javax.management.timer.TimerNotification) Date(java.util.Date) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) StringWriter(java.io.StringWriter) NotificationEventType(org.apache.camel.component.jmx.jaxb.NotificationEventType) ObjectNamesType(org.apache.camel.component.jmx.jaxb.ObjectNamesType) JMXConnectionNotification(javax.management.remote.JMXConnectionNotification)

Example 17 with AttributeChangeNotification

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());
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName)

Example 18 with AttributeChangeNotification

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);
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification)

Example 19 with AttributeChangeNotification

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);
        }
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) AttributeChangeNotification(javax.management.AttributeChangeNotification) ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) NotificationFilter(javax.management.NotificationFilter) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) JMException(javax.management.JMException) ListenerNotFoundException(javax.management.ListenerNotFoundException) NotificationListener(javax.management.NotificationListener)

Example 20 with AttributeChangeNotification

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);
        }
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) AttributeChangeNotification(javax.management.AttributeChangeNotification) ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) NotificationFilter(javax.management.NotificationFilter) Notification(javax.management.Notification) AttributeChangeNotification(javax.management.AttributeChangeNotification) NotificationListener(javax.management.NotificationListener)

Aggregations

AttributeChangeNotification (javax.management.AttributeChangeNotification)28 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)8 Test (org.junit.Test)6 ObjectName (javax.management.ObjectName)5 Bundle (org.osgi.framework.Bundle)4 ScanState (com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 NotificationFilter (javax.management.NotificationFilter)3 LinkedList (java.util.LinkedList)2 ExecutorService (java.util.concurrent.ExecutorService)2 MBeanServer (javax.management.MBeanServer)2 NotificationEmitter (javax.management.NotificationEmitter)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 CompositeData (javax.management.openmbean.CompositeData)2 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)2 Call (com.sun.jmx.examples.scandir.ScanManagerTest.Call)1 DirectoryScannerConfig (com.sun.jmx.examples.scandir.config.DirectoryScannerConfig)1