1. Project Clover database Tue Dec 20 2016 21:24:09 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
33
6
1
199
120
17
0.52
5.5
6
2.83

Classes

Class Line # Actions
DefaultWikiComponentManagerEventListener 61 33 0% 17 4
0.918367391.8%
 

Contributing tests

This file is covered by 7 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.Arrays;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Provider;
28    import javax.inject.Singleton;
29   
30    import org.apache.commons.lang3.exception.ExceptionUtils;
31    import org.slf4j.Logger;
32    import org.xwiki.bridge.DocumentModelBridge;
33    import org.xwiki.bridge.event.ApplicationReadyEvent;
34    import org.xwiki.bridge.event.DocumentCreatedEvent;
35    import org.xwiki.bridge.event.DocumentDeletedEvent;
36    import org.xwiki.bridge.event.DocumentUpdatedEvent;
37    import org.xwiki.bridge.event.WikiReadyEvent;
38    import org.xwiki.component.annotation.Component;
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.component.wiki.WikiComponentManager;
43    import org.xwiki.model.reference.DocumentReference;
44    import org.xwiki.observation.EventListener;
45    import org.xwiki.observation.ObservationContext;
46    import org.xwiki.observation.event.Event;
47   
48    import com.xpn.xwiki.XWikiContext;
49   
50    /**
51    * An {@link EventListener} responsible for registering all the wiki components when the application starts. It also
52    * dynamically registers and unregisters wiki components when documents holding those components are created, updated,
53    * or deleted.
54    *
55    * @version $Id: c1f1f691ad549ef9968fa892fc3bfafaa991c72a $
56    * @since 4.2M3
57    */
58    @Component
59    @Named("defaultWikiComponentManagerEventListener")
60    @Singleton
 
61    public class DefaultWikiComponentManagerEventListener implements EventListener
62    {
63    /**
64    * The logger to log.
65    */
66    @Inject
67    private Logger logger;
68   
69    /**
70    * The wiki component manager that knows how to register component definition against the underlying CM.
71    */
72    @Inject
73    private WikiComponentManager wikiComponentManager;
74   
75    /**
76    * The wiki component stores, used to retrieve component definitions.
77    */
78    @Inject
79    private List<WikiComponentBuilder> wikiComponentProviders;
80   
81    /**
82    * Used to access the current {@link XWikiContext}.
83    */
84    @Inject
85    private Provider<XWikiContext> xcontextProvider;
86   
87    @Inject
88    private ObservationContext observationContext;
89   
 
90  60 toggle @Override
91    public List<Event> getEvents()
92    {
93  60 return Arrays.asList(
94    new DocumentCreatedEvent(),
95    new DocumentUpdatedEvent(),
96    new DocumentDeletedEvent(),
97    new ApplicationReadyEvent(),
98    new WikiReadyEvent());
99    }
100   
 
101  720 toggle @Override
102    public String getName()
103    {
104  720 return "defaultWikiComponentManagerEventListener";
105    }
106   
 
107  4037 toggle @Override
108    public void onEvent(Event event, Object source, Object data)
109    {
110  4037 if (source instanceof DocumentModelBridge) {
111  3971 DocumentModelBridge document = (DocumentModelBridge) source;
112  3971 DocumentReference documentReference = document.getDocumentReference();
113   
114  3971 if (event instanceof DocumentCreatedEvent || event instanceof DocumentUpdatedEvent) {
115  3815 registerComponents(document);
116  156 } else if (event instanceof DocumentDeletedEvent) {
117    // Unregister components from the deleted document, if any
118  155 unregisterComponents(documentReference);
119    }
120  66 } else if (event instanceof ApplicationReadyEvent || event instanceof WikiReadyEvent) {
121    // These 2 events are created when the database is ready. We register all wiki components.
122  65 registerAllComponents();
123    }
124    }
125   
126    /**
127    * Register all the wiki components in the current wiki.
128    */
 
129  65 toggle private void registerAllComponents()
130    {
131    // Retrieve all components definitions and register them.
132  65 for (WikiComponentBuilder provider : this.wikiComponentProviders) {
133  199 for (DocumentReference reference : provider.getDocumentReferences()) {
134  240 try {
135  240 List<WikiComponent> components = provider.buildComponents(reference);
136  240 for (WikiComponent component : components) {
137  240 this.wikiComponentManager.registerWikiComponent(component);
138    }
139    } catch (Exception e) {
140  0 this.logger.warn("Failed to register the wiki component located in the document [{}]: {}",
141    reference, ExceptionUtils.getRootCauseMessage(e));
142    }
143    }
144    }
145    }
146   
147    /**
148    * Register wiki components from a given document.
149    *
150    * @param document the document to register the components for
151    */
 
152  3815 toggle private void registerComponents(DocumentModelBridge document)
153    {
154  3815 DocumentReference documentReference = document.getDocumentReference();
155   
156    // Unregister all wiki components registered under this document. We do this as otherwise we would need to
157    // handle the specific cases of xobject added, xobject updated, xobject deleted, etc. Instead we unregister
158    // all wiki components and re-register them all.
159  3815 unregisterComponents(documentReference);
160   
161    // Re-register all wiki components in the passed document
162  3815 for (WikiComponentBuilder provider : this.wikiComponentProviders) {
163    // Check whether the given document has a wiki component defined in it.
164  11559 if (provider.getDocumentReferences().contains(documentReference)) {
165  73 try {
166  73 List<WikiComponent> components = provider.buildComponents(documentReference);
167  73 for (WikiComponent component : components) {
168    // Register the component.
169  75 try {
170  75 this.wikiComponentManager.registerWikiComponent(component);
171    } catch (WikiComponentException e) {
172  0 this.logger.warn("Unable to register component(s) from document [{}]: {}",
173    component.getDocumentReference(), ExceptionUtils.getRootCauseMessage(e));
174    }
175    }
176    } catch (WikiComponentException e) {
177  0 this.logger.warn("Failed to create wiki component(s) for document [{}]: {}", documentReference,
178    ExceptionUtils.getRootCauseMessage(e));
179    }
180  73 break;
181    }
182    }
183    }
184   
185    /**
186    * Helper method to unregister a wiki component.
187    *
188    * @param documentReference the reference to the document for which to unregister the held wiki component.
189    */
 
190  3970 toggle private void unregisterComponents(DocumentReference documentReference)
191    {
192  3970 try {
193  3970 this.wikiComponentManager.unregisterWikiComponents(documentReference);
194    } catch (WikiComponentException e) {
195  0 this.logger.warn("Unable to unregister component(s) from document [{}]: {}", documentReference,
196    ExceptionUtils.getRootCauseMessage(e));
197    }
198    }
199    }