1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.component.wiki.internal

File DefaultWikiComponentManagerEventListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
27
4
1
169
94
14
0.52
6.75
4
3.5

Classes

Class Line # Actions
DefaultWikiComponentManagerEventListener 58 27 0% 14 3
0.926829392.7%
 

Contributing tests

This file is covered by 19 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.component.wiki.internal;
21   
22    import java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.apache.commons.lang3.exception.ExceptionUtils;
29    import org.slf4j.Logger;
30    import org.xwiki.bridge.DocumentModelBridge;
31    import org.xwiki.bridge.event.ApplicationReadyEvent;
32    import org.xwiki.bridge.event.DocumentCreatedEvent;
33    import org.xwiki.bridge.event.DocumentDeletedEvent;
34    import org.xwiki.bridge.event.DocumentUpdatedEvent;
35    import org.xwiki.bridge.event.WikiReadyEvent;
36    import org.xwiki.component.annotation.Component;
37    import org.xwiki.component.manager.ComponentLookupException;
38    import org.xwiki.component.manager.ComponentManager;
39    import org.xwiki.component.wiki.WikiComponent;
40    import org.xwiki.component.wiki.WikiComponentBuilder;
41    import org.xwiki.component.wiki.WikiComponentException;
42    import org.xwiki.model.reference.DocumentReference;
43    import org.xwiki.observation.AbstractEventListener;
44    import org.xwiki.observation.EventListener;
45    import org.xwiki.observation.event.Event;
46   
47    /**
48    * An {@link EventListener} responsible for registering all the wiki components when the application starts. It also
49    * dynamically registers and unregisters wiki components when documents holding those components are created, updated,
50    * or deleted.
51    *
52    * @version $Id: 6702ab5aeb09fbf0873e225a74267e572f7e24a4 $
53    * @since 4.2M3
54    */
55    @Component
56    @Named(DefaultWikiComponentManagerEventListener.EVENT_LISTENER_NAME)
57    @Singleton
 
58    public class DefaultWikiComponentManagerEventListener extends AbstractEventListener
59    {
60    /**
61    * The event listener name.
62    */
63    public static final String EVENT_LISTENER_NAME = "defaultWikiComponentManagerEventListener";
64   
65    /**
66    * The logger to log.
67    */
68    @Inject
69    private Logger logger;
70   
71    /**
72    * The wiki component stores, used to retrieve component definitions.
73    */
74    @Inject
75    private List<WikiComponentBuilder> wikiComponentProviders;
76   
77    @Inject
78    private ComponentManager componentManager;
79   
80    @Inject
81    private WikiComponentManagerEventListenerHelper wikiComponentManagerEventListenerHelper;
82   
83    /**
84    * Builds a new {@link DefaultWikiComponentManagerEventListener}.
85    */
 
86  95 toggle public DefaultWikiComponentManagerEventListener()
87    {
88  95 super(DefaultWikiComponentManagerEventListener.EVENT_LISTENER_NAME,
89    new DocumentCreatedEvent(), new DocumentUpdatedEvent(), new DocumentDeletedEvent(),
90    new ApplicationReadyEvent(), new WikiReadyEvent());
91    }
92   
 
93  8388 toggle @Override
94    public void onEvent(Event event, Object source, Object data)
95    {
96  8388 if (source instanceof DocumentModelBridge) {
97    // Get the document reference
98  8305 DocumentModelBridge document = (DocumentModelBridge) source;
99  8305 DocumentReference documentReference = document.getDocumentReference();
100   
101  8305 if (event instanceof DocumentCreatedEvent || event instanceof DocumentUpdatedEvent) {
102  8015 registerDocumentComponents(document.getDocumentReference());
103  290 } else if (event instanceof DocumentDeletedEvent) {
104    // Unregister components from the deleted document, if any
105  289 this.wikiComponentManagerEventListenerHelper.unregisterComponents(documentReference);
106    }
107    /* If we are at application startup time, we have to instanciate every document or object that we can find
108    * in the wiki */
109  83 } else if (event instanceof ApplicationReadyEvent || event instanceof WikiReadyEvent) {
110    // These 2 events are created when the database is ready. We register all wiki components.
111  82 registerAllDocumentComponents();
112    }
113    }
114   
115    /**
116    * Register all the wiki components that come from a document in the current wiki.
117    */
 
118  82 toggle private void registerAllDocumentComponents()
119    {
120  82 try {
121    // Retrieve all components definitions and register them.
122  82 this.wikiComponentProviders = this.componentManager.getInstanceList(WikiComponentBuilder.class);
123  82 for (WikiComponentBuilder provider : this.wikiComponentProviders) {
124  160 for (DocumentReference reference : provider.getDocumentReferences()) {
125  30 try {
126  30 List<WikiComponent> components = provider.buildComponents(reference);
127  30 this.wikiComponentManagerEventListenerHelper.registerComponentList(components);
128    } catch (WikiComponentException e) {
129  0 this.logger.warn("Failed to build the wiki component located in the document [{}]: {}",
130    reference, ExceptionUtils.getRootCauseMessage(e));
131    }
132    }
133    }
134    } catch (ComponentLookupException e) {
135  0 this.logger.warn(String.format("Unable to get a list of registered WikiComponentBuilder: %s", e));
136    }
137    }
138   
139    /**
140    * Registers the components linked to the given document. For that, we get a list of providers that can build a
141    * component using this document, then the first available provider is selected in order to register a component.
142    *
143    * @param documentReference the document used to create the component
144    */
 
145  8015 toggle private void registerDocumentComponents(DocumentReference documentReference)
146    {
147    // Unregister all wiki components registered under the given entity. We do this as otherwise we would need to
148    // handle the specific cases of elements added, elements updated and elements deleted, etc.
149    // Instead we unregister all wiki components and re-register them all.
150  8015 this.wikiComponentManagerEventListenerHelper.unregisterComponents(documentReference);
151   
152    // Re-register all wiki components in the passed document
153  8015 for (WikiComponentBuilder provider : this.wikiComponentProviders) {
154    // Check whether the given document has a wiki component defined in it.
155  16020 if (provider.getDocumentReferences().contains(documentReference)) {
156  7 try {
157  7 List<WikiComponent> components = provider.buildComponents(documentReference);
158  7 this.wikiComponentManagerEventListenerHelper.registerComponentList(components);
159    } catch (WikiComponentException e) {
160  0 this.logger.warn("Failed to create wiki component(s) for document [{}]: {}", documentReference,
161    ExceptionUtils.getRootCauseMessage(e));
162    }
163  7 break;
164    }
165    }
166    }
167   
168   
169    }