Search in sources :

Example 11 with ObservationManager

use of org.xwiki.observation.ObservationManager in project xwiki-platform by xwiki.

the class XWikiServletContextListener method contextDestroyed.

@Override
public void contextDestroyed(ServletContextEvent sce) {
    SHUTDOWN_LOGGER.debug("Stopping XWiki...");
    // It's possible that the Component Manager failed to initialize some of the required components.
    if (this.componentManager != null) {
        // to do something on stop to do it.
        try {
            ObservationManager observationManager = this.componentManager.getInstance(ObservationManager.class);
            observationManager.notify(new ApplicationStoppedEvent(), this);
        } catch (ComponentLookupException e) {
        // Nothing to do here.
        // TODO: Log a warning
        }
        // below in an Event Listener and move it to the legacy module.
        try {
            ApplicationContextListenerManager applicationContextListenerManager = this.componentManager.getInstance(ApplicationContextListenerManager.class);
            Container container = this.componentManager.getInstance(Container.class);
            applicationContextListenerManager.destroyApplicationContext(container.getApplicationContext());
        } catch (ComponentLookupException ex) {
        // Nothing to do here.
        // TODO: Log a warning
        }
        // Make sure to dispose all components before leaving
        this.componentManager.dispose();
    }
    SHUTDOWN_LOGGER.debug("XWiki has been stopped!");
}
Also used : ApplicationContextListenerManager(org.xwiki.container.ApplicationContextListenerManager) Container(org.xwiki.container.Container) ObservationManager(org.xwiki.observation.ObservationManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ApplicationStoppedEvent(org.xwiki.observation.event.ApplicationStoppedEvent)

Example 12 with ObservationManager

use of org.xwiki.observation.ObservationManager in project xwiki-platform by xwiki.

the class AttachmentEventGeneratorListener method onDocumentUpdatedEvent.

/**
 * @param originalDoc the previous version of the document
 * @param doc the new version of the document
 * @param context the XWiki context
 */
private void onDocumentUpdatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context) {
    ObservationManager observation = Utils.getComponent(ObservationManager.class);
    String reference = this.defaultEntityReferenceSerializer.serialize(doc.getDocumentReference());
    for (AttachmentDiff diff : doc.getAttachmentDiff(originalDoc, doc, context)) {
        if (StringUtils.isEmpty(diff.getOrigVersion())) {
            observation.notify(new AttachmentAddedEvent(reference, diff.getFileName()), doc, context);
        } else if (StringUtils.isEmpty(diff.getNewVersion())) {
            observation.notify(new AttachmentDeletedEvent(reference, diff.getFileName()), doc, context);
        } else {
            observation.notify(new AttachmentUpdatedEvent(reference, diff.getFileName()), doc, context);
        }
    }
}
Also used : ObservationManager(org.xwiki.observation.ObservationManager) AttachmentDiff(com.xpn.xwiki.doc.AttachmentDiff)

Example 13 with ObservationManager

use of org.xwiki.observation.ObservationManager in project xwiki-platform by xwiki.

the class DefaultDocumentCacheTest method testEventBasedCleanup.

@Test
public void testEventBasedCleanup() throws Exception {
    this.cache.set("data", this.document.getDocumentReference());
    this.cache.set("data", this.document.getDocumentReference(), "ext1", "ext2");
    ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
    observationManager.notify(new DocumentUpdatedEvent(this.document.getDocumentReference()), this.document, getContext());
    Assert.assertNull(this.cache.get(this.document.getDocumentReference()));
    Assert.assertNull(this.cache.get(this.document.getDocumentReference(), "ext1", "ext2"));
}
Also used : ObservationManager(org.xwiki.observation.ObservationManager) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) Test(org.junit.Test)

Example 14 with ObservationManager

use of org.xwiki.observation.ObservationManager in project xwiki-platform by xwiki.

the class DefaultRenderingCacheTest method testGetSetRenderedContent.

@Test
public void testGetSetRenderedContent() throws Exception {
    MockConfigurationSource source = getConfigurationSource();
    source.setProperty("core.renderingcache.documents", Collections.singletonList(this.document.getPrefixedFullName()));
    this.renderingCache.setRenderedContent(this.document.getDocumentReference(), "source", "renderedContent", getContext());
    Assert.assertEquals("renderedContent", this.renderingCache.getRenderedContent(this.document.getDocumentReference(), "source", getContext()));
    this.parameters.put("param", new String[] { "value1", "value2" });
    Assert.assertNull(this.renderingCache.getRenderedContent(this.document.getDocumentReference(), "source", getContext()));
    this.parameters.remove("param");
    Assert.assertEquals("renderedContent", this.renderingCache.getRenderedContent(this.document.getDocumentReference(), "source", getContext()));
    ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
    observationManager.notify(new DocumentUpdatedEvent(this.document.getDocumentReference()), this.document, getContext());
    Assert.assertNull(this.renderingCache.getRenderedContent(this.document.getDocumentReference(), "source", getContext()));
}
Also used : MockConfigurationSource(org.xwiki.test.internal.MockConfigurationSource) ObservationManager(org.xwiki.observation.ObservationManager) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) Test(org.junit.Test)

Example 15 with ObservationManager

use of org.xwiki.observation.ObservationManager in project xwiki-platform by xwiki.

the class XWikiMockitoTest method rollbackFiresEvents.

/**
 * Verify that {@link XWiki#rollback(XWikiDocument, String, XWikiContext)} fires the right events.
 */
@Test
public void rollbackFiresEvents() throws Exception {
    ObservationManager observationManager = mocker.getInstance(ObservationManager.class);
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getDocumentReference()).thenReturn(documentReference);
    XWikiDocument originalDocument = mock(XWikiDocument.class);
    // Mark the document as existing so that the roll-back method will fire an update event.
    when(originalDocument.isNew()).thenReturn(false);
    XWikiDocument result = mock(XWikiDocument.class);
    when(result.clone()).thenReturn(result);
    when(result.getDocumentReference()).thenReturn(documentReference);
    when(result.getOriginalDocument()).thenReturn(originalDocument);
    String revision = "3.5";
    when(this.documentRevisionProvider.getRevision(document, revision)).thenReturn(result);
    this.mocker.registerMockComponent(ContextualLocalizationManager.class);
    xwiki.rollback(document, revision, context);
    verify(observationManager).notify(new DocumentRollingBackEvent(documentReference, revision), result, context);
    verify(observationManager).notify(new DocumentUpdatingEvent(documentReference), result, context);
    verify(observationManager).notify(new DocumentUpdatedEvent(documentReference), result, context);
    verify(observationManager).notify(new DocumentRolledBackEvent(documentReference, revision), result, context);
}
Also used : DocumentUpdatingEvent(org.xwiki.bridge.event.DocumentUpdatingEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentRollingBackEvent(org.xwiki.bridge.event.DocumentRollingBackEvent) DocumentRolledBackEvent(org.xwiki.bridge.event.DocumentRolledBackEvent) ObservationManager(org.xwiki.observation.ObservationManager) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

ObservationManager (org.xwiki.observation.ObservationManager)28 DocumentReference (org.xwiki.model.reference.DocumentReference)11 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)9 Test (org.junit.Test)7 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)6 DocumentDeletedEvent (org.xwiki.bridge.event.DocumentDeletedEvent)4 DocumentUpdatedEvent (org.xwiki.bridge.event.DocumentUpdatedEvent)4 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)3 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)3 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)3 IOException (java.io.IOException)3 Date (java.util.Date)3 Mock (org.jmock.Mock)3 DocumentDeletingEvent (org.xwiki.bridge.event.DocumentDeletingEvent)3 XWikiAttachmentToRemove (com.xpn.xwiki.doc.XWikiDocument.XWikiAttachmentToRemove)2 XARImportedEvent (com.xpn.xwiki.internal.event.XARImportedEvent)2 XARImportingEvent (com.xpn.xwiki.internal.event.XARImportingEvent)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)2 FileNotFoundException (java.io.FileNotFoundException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2