Search in sources :

Example 1 with StackingComponentEventManager

use of org.xwiki.component.internal.StackingComponentEventManager in project xwiki-platform by xwiki.

the class XWikiServletContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    // Initializes the Embeddable Component Manager
    EmbeddableComponentManager ecm = new EmbeddableComponentManager();
    // Initialize all the components. Note that this can fail with a Runtime Exception. This is done voluntarily so
    // that the XWiki webapp will not be available if one component fails to load. It's better to fail-fast.
    ecm.initialize(this.getClass().getClassLoader());
    this.componentManager = ecm;
    // This is a temporary bridge to allow non XWiki components to lookup XWiki components.
    // We're putting the XWiki Component Manager instance in the Servlet Context so that it's
    // available in the XWikiAction class which in turn puts it into the XWikiContext instance.
    // Class that need to lookup then just need to get it from the XWikiContext instance.
    // This is of course not necessary for XWiki components since they just need to implement
    // the Composable interface to get access to the Component Manager or better they simply
    // need to declare their components requirements using the @Inject annotation of the xwiki
    // component manager together with a private class member, for automatic injection by the CM on init.
    servletContextEvent.getServletContext().setAttribute(org.xwiki.component.manager.ComponentManager.class.getName(), this.componentManager);
    // Use a Component Event Manager that stacks Component instance creation events till we tell it to flush them.
    // The reason is that the Observation Manager used to send the events but we need the Application Context to
    // be set up before we start sending events since there can be Observation Listener components that require
    // the Application Context (this is the case for example for the Office Importer Lifecycle Listener).
    StackingComponentEventManager eventManager = new StackingComponentEventManager();
    this.componentManager.setComponentEventManager(eventManager);
    // Initialize the Environment
    try {
        ServletEnvironment servletEnvironment = this.componentManager.getInstance(Environment.class);
        servletEnvironment.setServletContext(servletContextEvent.getServletContext());
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to initialize the Servlet Environment", e);
    }
    // below in an Event Listener and move it to the legacy module.
    try {
        ServletContainerInitializer containerInitializer = this.componentManager.getInstance(ServletContainerInitializer.class);
        containerInitializer.initializeApplicationContext(servletContextEvent.getServletContext());
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to initialize the Application Context", e);
    }
    // Send an Observation event to signal the XWiki application is started. This allows components who need to do
    // something on startup to do it.
    ObservationManager observationManager;
    try {
        observationManager = this.componentManager.getInstance(ObservationManager.class);
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to find the Observation Manager component", e);
    }
    // Now that the Application Context is set up, send the Component instance creation events we had stacked up.
    eventManager.setObservationManager(observationManager);
    eventManager.shouldStack(false);
    eventManager.flushEvents();
    // Make sure installed extensions are initialized before sending ApplicationStartedEvent
    try {
        this.componentManager.getInstance(ExtensionInitializer.class);
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to initialize installed extensions", e);
    }
    // Indicate to the various components that XWiki is ready
    observationManager.notify(new ApplicationStartedEvent(), this);
}
Also used : EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ApplicationStartedEvent(org.xwiki.observation.event.ApplicationStartedEvent) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ObservationManager(org.xwiki.observation.ObservationManager) StackingComponentEventManager(org.xwiki.component.internal.StackingComponentEventManager) ServletEnvironment(org.xwiki.environment.internal.ServletEnvironment)

Aggregations

EmbeddableComponentManager (org.xwiki.component.embed.EmbeddableComponentManager)1 StackingComponentEventManager (org.xwiki.component.internal.StackingComponentEventManager)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 ServletEnvironment (org.xwiki.environment.internal.ServletEnvironment)1 ObservationManager (org.xwiki.observation.ObservationManager)1 ApplicationStartedEvent (org.xwiki.observation.event.ApplicationStartedEvent)1