Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
RegisterMacrosOnImportListener | 51 | 8 | 0% | 4 | 1 |
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.wikimacro.internal; | |
21 | ||
22 | import java.util.ArrayList; | |
23 | import java.util.Arrays; | |
24 | import java.util.List; | |
25 | ||
26 | import javax.inject.Inject; | |
27 | import javax.inject.Named; | |
28 | import javax.inject.Singleton; | |
29 | ||
30 | import org.slf4j.Logger; | |
31 | import org.xwiki.component.annotation.Component; | |
32 | import org.xwiki.context.Execution; | |
33 | import org.xwiki.observation.EventListener; | |
34 | import org.xwiki.observation.event.Event; | |
35 | import org.xwiki.rendering.macro.wikibridge.WikiMacroInitializer; | |
36 | ||
37 | import com.xpn.xwiki.XWikiContext; | |
38 | import com.xpn.xwiki.internal.event.XARImportedEvent; | |
39 | ||
40 | /** | |
41 | * Import action event listener to handle registering of the wiki macros after import. The problem is with the wiki | |
42 | * macros imported in the empty wiki, which cannot be registered at the save of the document on import time due to lack | |
43 | * of rights (initial import is done with XWikiGuest). <br> | |
44 | * FIXME: remove this when the initial import will be done with the appropriate user | |
45 | * | |
46 | * @version $Id: 7a8f3915341e0e21667485edee49e706a2dd5ce2 $ | |
47 | */ | |
48 | @Component | |
49 | @Singleton | |
50 | @Named("register-macros-on-import") | |
51 | public class RegisterMacrosOnImportListener implements EventListener | |
52 | { | |
53 | /** | |
54 | * The macro initializer used to register the wiki macros. | |
55 | */ | |
56 | @Inject | |
57 | private WikiMacroInitializer macroInitializer; | |
58 | ||
59 | /** | |
60 | * The execution used to get the xwiki context, to reset the context grouplist cache. | |
61 | */ | |
62 | @Inject | |
63 | private Execution execution; | |
64 | ||
65 | /** | |
66 | * The logger to log. | |
67 | */ | |
68 | @Inject | |
69 | private Logger logger; | |
70 | ||
71 | /** | |
72 | * The events observed by this event listener. | |
73 | */ | |
74 | private final List<Event> eventsList = new ArrayList<Event>(Arrays.asList(new XARImportedEvent())); | |
75 | ||
76 | 23 | ![]() |
77 | public List<Event> getEvents() | |
78 | { | |
79 | 23 | return eventsList; |
80 | } | |
81 | ||
82 | 69 | ![]() |
83 | public String getName() | |
84 | { | |
85 | 69 | return "RegisterMacrosOnImportListener"; |
86 | } | |
87 | ||
88 | 19 | ![]() |
89 | public void onEvent(Event event, Object source, Object data) | |
90 | { | |
91 | // when import is done, re-register macros in the current wiki | |
92 | 19 | try { |
93 | // clean the grouplist cache from the context since now, after import, the groups have changed. All | |
94 | // cache saved on the context during the import (save listeners) should be cleaned since it might not be | |
95 | // accurate. | |
96 | // FIXME: somehow, it's a bit of time loss to clean the context grouplist, especially when this is done | |
97 | // for a subsequent import (not the initial import of the .xar), when macros are registered anyway, there | |
98 | // are no rights issues on registering macros on documents save. | |
99 | 19 | XWikiContext xcontext = (XWikiContext) execution.getContext().getProperty("xwikicontext"); |
100 | 19 | xcontext.remove("grouplist"); |
101 | // get the current wiki to register macros for | |
102 | 19 | String currentWiki = xcontext.getWikiId(); |
103 | 19 | macroInitializer.registerExistingWikiMacros(currentWiki); |
104 | } catch (Exception e) { | |
105 | 0 | this.logger.warn("Could not register existing macros on import", e); |
106 | } | |
107 | } | |
108 | } |