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

File XarExtensionPlan.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

32
84
12
1
270
191
33
0.39
7
12
2.75

Classes

Class Line # Actions
XarExtensionPlan 55 84 0% 33 11
0.914062591.4%
 

Contributing tests

This file is covered by 18 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.io.Closeable;
23    import java.io.File;
24    import java.io.IOException;
25    import java.util.Collection;
26    import java.util.HashMap;
27    import java.util.Map;
28   
29    import org.slf4j.Logger;
30    import org.slf4j.LoggerFactory;
31    import org.xwiki.extension.ExtensionException;
32    import org.xwiki.extension.ExtensionId;
33    import org.xwiki.extension.InstalledExtension;
34    import org.xwiki.extension.LocalExtension;
35    import org.xwiki.extension.job.plan.ExtensionPlan;
36    import org.xwiki.extension.job.plan.ExtensionPlanAction;
37    import org.xwiki.extension.job.plan.ExtensionPlanAction.Action;
38    import org.xwiki.extension.repository.InstalledExtensionRepository;
39    import org.xwiki.extension.repository.LocalExtensionRepository;
40    import org.xwiki.extension.xar.internal.handler.packager.Packager;
41    import org.xwiki.extension.xar.internal.repository.XarInstalledExtension;
42    import org.xwiki.model.reference.DocumentReference;
43    import org.xwiki.model.reference.LocalDocumentReference;
44    import org.xwiki.model.reference.WikiReference;
45    import org.xwiki.xar.XarEntry;
46    import org.xwiki.xar.XarException;
47    import org.xwiki.xar.XarPackage;
48   
49    import com.xpn.xwiki.doc.XWikiDocument;
50   
51    /**
52    * @version $Id: a24e598d967f2866053bc7ee87cee8ccab5fdda7 $
53    * @since 5.4M1
54    */
 
55    public class XarExtensionPlan implements Closeable
56    {
57    public static final String CONTEXTKEY_XARINSTALLPLAN = "extension.xar.installplan";
58   
59    /**
60    * Logging helper object.
61    */
62    private static final Logger LOGGER = LoggerFactory.getLogger(XarExtensionPlan.class);
63   
64    public final Map<String, Map<XarEntry, XarExtensionPlanEntry>> previousXAREntries;
65   
66    /**
67    * Map<namespace, >
68    */
69    public final Map<String, Map<XarEntry, LocalExtension>> nextXAREntries;
70   
 
71  58 toggle public XarExtensionPlan(ExtensionPlan plan, InstalledExtensionRepository xarRepository,
72    LocalExtensionRepository localReposirory) throws ExtensionException, XarException, IOException
73    {
74  58 this.previousXAREntries = new HashMap<>();
75  58 this.nextXAREntries = new HashMap<>();
76   
77  58 Map<ExtensionId, XarExtensionPlanEntry> planEntry = new HashMap<>();
78   
79  58 for (ExtensionPlanAction action : plan.getActions()) {
80  628 if (action.getExtension().getType().equals(XarExtensionHandler.TYPE)) {
81    // Get previous entries
82  271 Collection<InstalledExtension> previousExtensions = action.getPreviousExtensions();
83  271 for (InstalledExtension previousExtension : previousExtensions) {
84  131 if (previousExtension != null && previousExtension.getType().equals(XarExtensionHandler.TYPE)) {
85  131 XarInstalledExtension previousXARExtension =
86    (XarInstalledExtension) xarRepository.getInstalledExtension(previousExtension.getId());
87   
88  131 if (previousXARExtension == null) {
89    // Very weird situation but let's be safe
90  0 LOGGER.error("Installed extension [{}] is not properly registered in"
91    + " the virtual XAR installed extensions repository", previousExtension);
92   
93  0 continue;
94    }
95   
96  131 XarExtensionPlanEntry xarPlanEntry = planEntry.get(previousXARExtension.getId());
97  131 if (xarPlanEntry == null) {
98  125 xarPlanEntry =
99    new XarExtensionPlanEntry(previousXARExtension, previousXARExtension.getXarPackage());
100  125 planEntry.put(previousXARExtension.getId(), xarPlanEntry);
101    }
102   
103  131 for (XarEntry entry : previousXARExtension.getXarPackage().getEntries()) {
104  1755 String wiki;
105  1755 try {
106  1755 wiki = XarHandlerUtils.getWikiFromNamespace(action.getNamespace());
107    } catch (UnsupportedNamespaceException e) {
108  0 throw new ExtensionException("Failed to extract wiki id from namespace", e);
109    }
110  1755 Map<XarEntry, XarExtensionPlanEntry> pages = previousXAREntries.get(wiki);
111  1755 if (pages == null) {
112  24 pages = new HashMap<>();
113  24 this.previousXAREntries.put(wiki, pages);
114    }
115  1755 pages.put(entry, xarPlanEntry);
116    }
117    }
118    }
119   
120    // Get new entries
121  271 LocalExtension nextExtension = action.getAction() != Action.UNINSTALL && action.getExtension() != null
122    ? localReposirory.getLocalExtension(action.getExtension().getId()) : null;
123   
124  271 if (nextExtension != null) {
125  247 try {
126  247 Collection<XarEntry> entries =
127    XarPackage.getEntries(new File(nextExtension.getFile().getAbsolutePath()));
128   
129  247 for (XarEntry entry : entries) {
130  3262 String wiki;
131  3262 try {
132  3262 wiki = XarHandlerUtils.getWikiFromNamespace(action.getNamespace());
133    } catch (UnsupportedNamespaceException e) {
134  0 throw new ExtensionException("Failed to extract wiki id from namespace", e);
135    }
136  3262 Map<XarEntry, LocalExtension> pages = this.nextXAREntries.get(wiki);
137  3262 if (pages == null) {
138  44 pages = new HashMap<>();
139  44 this.nextXAREntries.put(wiki, pages);
140    }
141  3262 pages.put(entry, nextExtension);
142    }
143    } catch (Exception e) {
144  0 LOGGER.error("Failed to parse extension file [{}]", nextExtension.getFile().getAbsolutePath(),
145    e);
146    }
147    }
148    }
149    }
150    }
151   
 
152  0 toggle public XarExtensionPlanEntry getPreviousXarExtensionPlanEntry(DocumentReference documentReference)
153    {
154  0 String wiki = documentReference.getWikiReference().getName();
155  0 LocalDocumentReference localDocumentReference = new LocalDocumentReference(documentReference);
156   
157  0 return getPreviousXarExtensionPlanEntry(wiki, localDocumentReference);
158    }
159   
 
160  4645 toggle public XarExtensionPlanEntry getPreviousXarExtensionPlanEntry(String wiki,
161    LocalDocumentReference localDocumentReference)
162    {
163  4645 XarEntry xarEntry = new XarEntry(localDocumentReference);
164   
165  4645 XarExtensionPlanEntry planEntry = null;
166   
167  4645 Map<XarEntry, XarExtensionPlanEntry> wikiEntry = this.previousXAREntries.get(wiki);
168   
169  4645 if (wikiEntry != null) {
170  3086 planEntry = wikiEntry.get(xarEntry);
171    }
172   
173  4645 if (planEntry == null) {
174  2091 wikiEntry = this.previousXAREntries.get(null);
175   
176  2091 if (wikiEntry != null) {
177  56 planEntry = wikiEntry.get(xarEntry);
178    }
179    }
180   
181  4645 return planEntry;
182    }
183   
 
184  1161 toggle public XarInstalledExtension getPreviousXarExtension(DocumentReference documentReference)
185    {
186  1161 String wiki = documentReference.getWikiReference().getName();
187  1161 LocalDocumentReference localDocumentReference = new LocalDocumentReference(documentReference);
188   
189  1161 return getPreviousXarExtension(wiki, localDocumentReference);
190    }
191   
 
192  1161 toggle public XarInstalledExtension getPreviousXarExtension(String wiki, LocalDocumentReference localDocumentReference)
193    {
194  1161 XarExtensionPlanEntry entry = getPreviousXarExtensionPlanEntry(wiki, localDocumentReference);
195   
196  1161 return entry != null ? entry.extension : null;
197    }
198   
 
199  1161 toggle public LocalExtension getNextXarExtension(DocumentReference documentReference)
200    {
201  1161 WikiReference wikiReference = documentReference.getWikiReference();
202  1161 LocalDocumentReference localDocumentReference = new LocalDocumentReference(documentReference);
203   
204  1161 return getNextXarExtension(wikiReference.getName(), localDocumentReference);
205    }
206   
 
207  1319 toggle public LocalExtension getNextXarExtension(String wiki, LocalDocumentReference localDocumentReference)
208    {
209  1319 XarEntry xarEntry = new XarEntry(localDocumentReference);
210   
211  1319 LocalExtension nextExtension = null;
212   
213  1319 Map<XarEntry, LocalExtension> wikiEntry = this.nextXAREntries.get(wiki);
214   
215  1319 if (wikiEntry != null) {
216  1229 nextExtension = wikiEntry.get(xarEntry);
217    }
218   
219  1319 if (nextExtension == null) {
220  216 wikiEntry = this.nextXAREntries.get(null);
221   
222  216 if (wikiEntry != null) {
223  27 nextExtension = wikiEntry.get(xarEntry);
224    }
225    }
226   
227  1319 return nextExtension;
228    }
229   
 
230  3484 toggle public XWikiDocument getPreviousXWikiDocument(DocumentReference documentReference, Packager packager)
231    throws XarException, IOException
232    {
233  3484 WikiReference wikiReference = documentReference.getWikiReference();
234  3484 LocalDocumentReference localDocumentReference = new LocalDocumentReference(documentReference);
235   
236  3484 return getPreviousXWikiDocument(wikiReference, localDocumentReference, packager);
237    }
238   
 
239  3484 toggle public XWikiDocument getPreviousXWikiDocument(WikiReference wikiReference, LocalDocumentReference localReference,
240    Packager packager) throws XarException, IOException
241    {
242  3484 XarExtensionPlanEntry xarPlanEntry = getPreviousXarExtensionPlanEntry(wikiReference.getName(), localReference);
243   
244  3484 return xarPlanEntry != null ? packager.getXWikiDocument(wikiReference, localReference, xarPlanEntry.xarFile)
245    : null;
246    }
247   
 
248  58 toggle @Override
249    public void close() throws IOException
250    {
251  58 for (Map<XarEntry, XarExtensionPlanEntry> wikiEntry : this.previousXAREntries.values()) {
252  24 for (XarExtensionPlanEntry entry : wikiEntry.values()) {
253  1694 entry.close();
254    }
255    }
256    }
257   
 
258  158 toggle public boolean containsNewPage(DocumentReference documentReference)
259    {
260  158 WikiReference wikiReference = documentReference.getWikiReference();
261  158 LocalDocumentReference localDocumentReference = new LocalDocumentReference(documentReference);
262   
263  158 return containsNewPage(wikiReference, localDocumentReference);
264    }
265   
 
266  158 toggle public boolean containsNewPage(WikiReference wikiReference, LocalDocumentReference localDocumentReference)
267    {
268  158 return getNextXarExtension(wikiReference.getName(), localDocumentReference) != null;
269    }
270    }