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

File XarExtensionPlan.java

 

Coverage histogram

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

Code metrics

30
81
12
1
265
189
31
0.38
6.75
12
2.58

Classes

Class Line # Actions
XarExtensionPlan 57 81 0% 31 23
0.813008181.3%
 

Contributing tests

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