1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.xar.internal.handler

File WikiEventListener.java

 

Coverage histogram

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

Code metrics

16
44
6
1
188
126
18
0.41
7.33
6
3

Classes

Class Line # Actions
WikiEventListener 55 44 0% 18 7
0.893939489.4%
 

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