1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.xar.internal.handler

File WikiEventListener.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart7.png
66% of files have more coverage

Code metrics

16
46
6
1
204
137
18
0.39
7.67
6
3

Classes

Class Line # Actions
WikiEventListener 59 46 0% 18 20
0.705882470.6%
 

Contributing tests

This file is covered by 5 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.extension.xar.internal.handler;
21   
22    import java.util.Collection;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Provider;
27    import javax.inject.Singleton;
28   
29    import org.slf4j.Logger;
30    import org.xwiki.bridge.event.WikiCopiedEvent;
31    import org.xwiki.bridge.event.WikiCreatedEvent;
32    import org.xwiki.bridge.event.WikiDeletedEvent;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.extension.ExtensionDependency;
35    import org.xwiki.extension.ExtensionException;
36    import org.xwiki.extension.InstallException;
37    import org.xwiki.extension.InstalledExtension;
38    import org.xwiki.extension.UninstallException;
39    import org.xwiki.extension.event.ExtensionInstalledEvent;
40    import org.xwiki.extension.event.ExtensionInstallingEvent;
41    import org.xwiki.extension.handler.ExtensionHandler;
42    import org.xwiki.extension.handler.ExtensionHandlerManager;
43    import org.xwiki.extension.job.InstallRequest;
44    import org.xwiki.extension.repository.InstalledExtensionRepository;
45    import org.xwiki.model.namespace.WikiNamespace;
46    import org.xwiki.model.reference.DocumentReference;
47    import org.xwiki.observation.AbstractEventListener;
48    import org.xwiki.observation.ObservationManager;
49    import org.xwiki.observation.event.Event;
50   
51    import com.xpn.xwiki.XWikiContext;
52   
53    /**
54    * @version $Id: 48b2163ef9519c69e2748a67694ed0d828c59757 $
55    */
56    @Component
57    @Singleton
58    @Named("extension.xar.WikiCopiedListener")
 
59    public class WikiEventListener extends AbstractEventListener
60    {
61    /**
62    * The install request property that specifies which user triggered the install.
63    */
64    private static final String PROPERTY_USER_REFERENCE = "user.reference";
65   
66    @Inject
67    private InstalledExtensionRepository installedRepository;
68   
69    /**
70    * Used to install the extension itself depending of its type.
71    */
72    @Inject
73    private ExtensionHandlerManager extensionHandlerManager;
74   
75    @Inject
76    @Named("xar")
77    private Provider<ExtensionHandler> xarHandlerProvider;
78   
79    @Inject
80    private ObservationManager observation;
81   
82    @Inject
83    private Logger logger;
84   
 
85  101 toggle public WikiEventListener()
86    {
87  101 super("extension.xar.WikiCopiedListener", new WikiCopiedEvent(), new WikiCreatedEvent(),
88    new WikiDeletedEvent());
89    }
90   
 
91  4 toggle @Override
92    public void onEvent(Event event, Object o, Object context)
93    {
94  4 if (event instanceof WikiCopiedEvent) {
95  1 onWikiCopied((WikiCopiedEvent) event);
96  3 } else if (event instanceof WikiCreatedEvent) {
97  3 onWikiCreated((WikiCreatedEvent) event, (XWikiContext) context);
98  0 } else if (event instanceof WikiDeletedEvent) {
99  0 onWikiDeleted((WikiDeletedEvent) event);
100    }
101    }
102   
 
103  1 toggle private void onWikiCopied(WikiCopiedEvent event)
104    {
105  1 String sourceNamespace = new WikiNamespace(event.getSourceWikiId()).serialize();
106  1 String targetNamespace = new WikiNamespace(event.getTargetWikiId()).serialize();
107   
108  1 Collection<InstalledExtension> installedExtensions =
109    this.installedRepository.getInstalledExtensions(sourceNamespace);
110   
111  1 for (InstalledExtension installedExtension : installedExtensions) {
112  3 copyInstalledExtension(installedExtension, sourceNamespace, targetNamespace);
113    }
114    }
115   
 
116  4 toggle private void copyInstalledExtension(InstalledExtension installedExtension, String sourceNamespace,
117    String targetNamespace)
118    {
119  4 if (!installedExtension.isInstalled(targetNamespace) && !installedExtension.isInstalled(null)) {
120    // Copy dependencies first
121  2 for (ExtensionDependency dependency : installedExtension.getDependencies()) {
122  1 InstalledExtension installedDependency =
123    this.installedRepository.getInstalledExtension(dependency.getId(), sourceNamespace);
124  1 if (installedDependency != null) {
125  1 copyInstalledExtension(installedDependency, sourceNamespace, targetNamespace);
126    }
127    }
128   
129    // Copy extension
130  2 try {
131    // Notify about the install
132  2 this.observation.notify(new ExtensionInstallingEvent(installedExtension.getId(), targetNamespace),
133    installedExtension);
134   
135    // Installed extension
136  2 this.extensionHandlerManager.initialize(installedExtension, targetNamespace);
137   
138    // Register extension as installed
139  2 this.installedRepository.installExtension(installedExtension, targetNamespace,
140    installedExtension.isDependency(sourceNamespace));
141   
142    // Notify about the install
143  2 this.observation.notify(new ExtensionInstalledEvent(installedExtension.getId(), targetNamespace),
144    installedExtension);
145    } catch (ExtensionException e) {
146  0 this.logger.error("Failed to copy extension [{}] from namespace [{}] to namespace [{}]",
147    installedExtension, sourceNamespace, targetNamespace, e);
148    }
149    }
150    }
151   
 
152  0 toggle private void onWikiDeleted(WikiDeletedEvent event)
153    {
154  0 String namespace = new WikiNamespace(event.getWikiId()).serialize();
155   
156  0 Collection<InstalledExtension> installedExtensions = this.installedRepository.getInstalledExtensions(namespace);
157   
158  0 for (InstalledExtension installedExtension : installedExtensions) {
159  0 if (!installedExtension.isInstalled(null)) {
160  0 try {
161  0 this.installedRepository.uninstallExtension(installedExtension, namespace);
162    } catch (UninstallException e) {
163  0 this.logger.error("Failed to uninstall extension [{}] from namespace [{}]", installedExtension,
164    namespace, e);
165    }
166    }
167    }
168    }
169   
 
170  3 toggle private void onWikiCreated(WikiCreatedEvent event, XWikiContext context)
171    {
172  3 String namespace = new WikiNamespace(event.getWikiId()).serialize();
173   
174  3 Collection<InstalledExtension> installedExtensions = this.installedRepository.getInstalledExtensions(null);
175   
176  3 InstallRequest installRequest = new InstallRequest();
177  3 DocumentReference userReference = context.getUserReference();
178  3 if (userReference != null) {
179  3 installRequest.setProperty(PROPERTY_USER_REFERENCE, userReference);
180    // We set the string value because the extension repository doesn't know how to serialize/parse an extension
181    // property whose value is a DocumentReference, and adding support for it requires considerable refactoring
182    // because ExtensionPropertySerializers are not components (they are currently hard-coded).
183  3 installRequest.setExtensionProperty(PROPERTY_USER_REFERENCE, userReference.toString());
184    }
185  3 installRequest.setVerbose(false);
186    // TODO: make it interactive ? (require wiki creation to be job based)
187  3 installRequest.setInteractive(false);
188   
189  3 ExtensionHandler xarHandler = this.xarHandlerProvider.get();
190   
191  3 for (InstalledExtension installedExtension : installedExtensions) {
192  3 if (installedExtension.getType().equals(XarExtensionHandler.TYPE)) {
193  3 installRequest.addExtension(installedExtension.getId());
194   
195  3 try {
196  3 xarHandler.install(installedExtension, namespace, installRequest);
197    } catch (InstallException e) {
198  0 this.logger.error("Failed to import extension [{}] in wiki [{}]", installedExtension,
199    event.getWikiId(), e);
200    }
201    }
202    }
203    }
204    }