1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.macro.wikibridge

File WikiMacroEventListener.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

12
28
6
1
176
98
17
0.61
4.67
6
2.83

Classes

Class Line # Actions
WikiMacroEventListener 55 28 0% 17 7
0.8478260684.8%
 

Contributing tests

This file is covered by 11 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.rendering.internal.macro.wikibridge;
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.Singleton;
28   
29    import org.slf4j.Logger;
30    import org.xwiki.bridge.DocumentModelBridge;
31    import org.xwiki.bridge.event.AbstractDocumentEvent;
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.component.annotation.Component;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.observation.EventListener;
38    import org.xwiki.observation.event.Event;
39    import org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException;
40    import org.xwiki.rendering.macro.wikibridge.WikiMacro;
41    import org.xwiki.rendering.macro.wikibridge.WikiMacroException;
42    import org.xwiki.rendering.macro.wikibridge.WikiMacroFactory;
43    import org.xwiki.rendering.macro.wikibridge.WikiMacroManager;
44   
45    /**
46    * An {@link EventListener} responsible for dynamically registering / unregistering / updating xwiki rendering macros
47    * based on wiki macro create / delete / update actions.
48    *
49    * @version $Id: bf389787c3333b4ef62a5cd2b7dd283197d4f05f $
50    * @since 2.0M2
51    */
52    @Component
53    @Named("wikimacrolistener")
54    @Singleton
 
55    public class WikiMacroEventListener implements EventListener
56    {
57    /**
58    * The {@link org.xwiki.rendering.macro.wikibridge.WikiMacroFactory} component.
59    */
60    @Inject
61    private WikiMacroFactory macroFactory;
62   
63    /**
64    * The {@link WikiMacroManager} component.
65    */
66    @Inject
67    private WikiMacroManager wikiMacroManager;
68   
69    /**
70    * The logger to log.
71    */
72    @Inject
73    private Logger logger;
74   
 
75  226 toggle @Override
76    public String getName()
77    {
78  226 return "wikimacrolistener";
79    }
80   
 
81  30 toggle @Override
82    public List<Event> getEvents()
83    {
84  30 return Arrays
85    .<Event> asList(new DocumentCreatedEvent(), new DocumentUpdatedEvent(), new DocumentDeletedEvent());
86    }
87   
 
88  416 toggle @Override
89    public void onEvent(Event event, Object source, Object data)
90    {
91  416 if (event instanceof AbstractDocumentEvent) {
92  416 DocumentModelBridge document = (DocumentModelBridge) source;
93  416 DocumentReference documentReference = document.getDocumentReference();
94   
95    // We've decided not to log any exception raised in the XWiki logs since a failure to register or
96    // unregister a macro isn't a failure of the XWiki software. It's something normal, same as, for example,
97    // not being able to edit a page if the user doesn't have edit rights.
98    // The problem here is that since Macros are registered by an Event Listener there's no way defined
99    // to let the user know about the status. We'd need to:
100    // - create a status page listing the state of all macros with the last error messages
101    // - don't use a listener to register macros and instead have a page listing all macros with a
102    // register/unregister button (and thus provide visual feedback when the action fails).
103   
104  416 if (event instanceof DocumentCreatedEvent || event instanceof DocumentUpdatedEvent) {
105  372 registerMacro(documentReference);
106  44 } else if (event instanceof DocumentDeletedEvent) {
107  44 unregisterMacro(documentReference);
108    }
109    }
110    }
111   
112    /**
113    * @param documentReference the reference of the document containing the macro to register
114    */
 
115  372 toggle private void registerMacro(DocumentReference documentReference)
116    {
117    // Unregister any existing macro registered under this document.
118  372 if (unregisterMacro(documentReference)) {
119    // Check whether the given document has a wiki macro defined in it.
120  372 if (this.macroFactory.containsWikiMacro(documentReference)) {
121    // Attempt to create a wiki macro.
122  19 WikiMacro wikiMacro;
123  19 try {
124  19 wikiMacro = this.macroFactory.createWikiMacro(documentReference);
125    } catch (WikiMacroException e) {
126  0 this.logger.debug(String.format("Failed to create wiki macro [%s]", documentReference), e);
127  0 return;
128    }
129   
130    // Register the macro.
131  19 registerMacro(documentReference, wikiMacro);
132    }
133    }
134    }
135   
136    /**
137    * Register a new wiki macro.
138    *
139    * @param documentReference the reference of the document containing the wiki macro to register
140    * @param wikiMacro the wiki macro to register
141    */
 
142  19 toggle private void registerMacro(DocumentReference documentReference, WikiMacro wikiMacro)
143    {
144  19 try {
145  19 this.wikiMacroManager.registerWikiMacro(documentReference, wikiMacro);
146    } catch (WikiMacroException e) {
147  0 this.logger.debug(
148    String.format("Unable to register macro [%s] in document [%s]", wikiMacro.getDescriptor().getId()
149    .getId(), documentReference), e);
150    } catch (InsufficientPrivilegesException e) {
151  0 this.logger.debug(String.format("Insufficient privileges for registering macro [%s] in document [%s]",
152    wikiMacro.getDescriptor().getId().getId(), documentReference), e);
153    }
154    }
155   
156    /**
157    * Unregister wiki macro.
158    *
159    * @param documentReference the reference of the document containing the wiki macro to register
160    * @return false if failed to unregister wiki macro, true otherwise
161    */
 
162  416 toggle private boolean unregisterMacro(DocumentReference documentReference)
163    {
164  416 boolean result = true;
165  416 if (this.wikiMacroManager.hasWikiMacro(documentReference)) {
166  18 try {
167  18 this.wikiMacroManager.unregisterWikiMacro(documentReference);
168    } catch (WikiMacroException e) {
169  3 this.logger.debug(String.format("Unable to unregister macro in document [%s]", documentReference), e);
170  3 result = false;
171    }
172    }
173   
174  416 return result;
175    }
176    }