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

File AbstractInstalledExtensionRepository.java

 

Coverage histogram

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

Code metrics

12
33
12
1
170
117
19
0.58
2.75
12
1.58

Classes

Class Line # Actions
AbstractInstalledExtensionRepository 48 33 0% 19 2
0.964912396.5%
 

Contributing tests

This file is covered by 110 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.repository.internal.installed;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.Collection;
25    import java.util.Collections;
26    import java.util.LinkedHashSet;
27    import java.util.List;
28    import java.util.regex.Pattern;
29   
30    import org.xwiki.extension.ExtensionId;
31    import org.xwiki.extension.InstallException;
32    import org.xwiki.extension.InstalledExtension;
33    import org.xwiki.extension.LocalExtension;
34    import org.xwiki.extension.repository.InstalledExtensionRepository;
35    import org.xwiki.extension.repository.internal.AbstractCachedExtensionRepository;
36    import org.xwiki.extension.repository.internal.RepositoryUtils;
37    import org.xwiki.extension.repository.result.IterableResult;
38    import org.xwiki.extension.repository.search.ExtensionQuery;
39    import org.xwiki.extension.repository.search.SearchException;
40   
41    /**
42    * Base class for {@link InstalledExtensionRepository} implementations.
43    *
44    * @param <E> the type of the extension
45    * @version $Id: 6e01e04068e493a987833473acc7b9dbae5d1c81 $
46    * @since 7.0M2
47    */
 
48    public abstract class AbstractInstalledExtensionRepository<E extends InstalledExtension>
49    extends AbstractCachedExtensionRepository<E> implements InstalledExtensionRepository
50    {
 
51  2 toggle @Override
52    public int countExtensions()
53    {
54  2 return this.extensions.size();
55    }
56   
 
57  78 toggle @Override
58    public Collection<InstalledExtension> getInstalledExtensions(String namespace)
59    {
60  78 List<InstalledExtension> installedExtensions = new ArrayList<InstalledExtension>(extensions.size());
61  78 for (InstalledExtension localExtension : this.extensions.values()) {
62  338 if (localExtension.isInstalled(namespace)) {
63  228 installedExtensions.add(localExtension);
64    }
65    }
66   
67  78 return installedExtensions;
68    }
69   
 
70  499 toggle @Override
71    public Collection<InstalledExtension> getInstalledExtensions()
72    {
73  499 return Collections.<InstalledExtension>unmodifiableCollection(this.extensions.values());
74    }
75   
 
76  605 toggle @Override
77    public InstalledExtension getInstalledExtension(ExtensionId extensionId)
78    {
79  605 return this.extensions.get(extensionId);
80    }
81   
 
82  28 toggle @Override
83    public InstalledExtension installExtension(LocalExtension extension, String namespace, boolean dependency)
84    throws InstallException
85    {
86  28 return installExtension(extension, namespace, dependency, Collections.<String, Object>emptyMap());
87    }
88   
 
89  5 toggle @Override
90    public IterableResult<InstalledExtension> searchInstalledExtensions(String pattern, String namespace, int offset,
91    int nb) throws SearchException
92    {
93  5 ExtensionQuery query = new ExtensionQuery(pattern);
94   
95  5 query.setOffset(offset);
96  5 query.setLimit(nb);
97   
98  5 return searchInstalledExtensions(namespace, query);
99    }
100   
 
101  8 toggle @Override
102    public IterableResult<InstalledExtension> searchInstalledExtensions(ExtensionQuery query)
103    {
104  8 return searchInstalledExtensions((List<String>) null, query, this.extensions.values());
105    }
106   
 
107  0 toggle @Override
108    public IterableResult<InstalledExtension> searchInstalledExtensions(Collection<String> namespaces,
109    ExtensionQuery query)
110    {
111  0 return searchInstalledExtensions(namespaces, query, this.extensions.values());
112    }
113   
 
114  6 toggle @Override
115    public IterableResult<InstalledExtension> searchInstalledExtensions(String namespace, ExtensionQuery query)
116    throws SearchException
117    {
118  6 return searchInstalledExtensions(namespace, query, this.extensions.values());
119    }
120   
 
121  6 toggle protected IterableResult<InstalledExtension> searchInstalledExtensions(String namespace, ExtensionQuery query,
122    Collection<? extends InstalledExtension> installedExtensions)
123    {
124  6 return searchInstalledExtensions(Arrays.asList(namespace), query, installedExtensions);
125    }
126   
 
127  14 toggle protected IterableResult<InstalledExtension> searchInstalledExtensions(Collection<String> namespaces,
128    ExtensionQuery query, Collection<? extends InstalledExtension> installedExtensions)
129    {
130    // Filter extension matching passed query and namespaces
131  14 List<InstalledExtension> result = filter(namespaces, query, installedExtensions);
132   
133    // Make sure all the elements of the list are unique
134  14 if (result.size() > 1) {
135  3 result = new ArrayList<>(new LinkedHashSet<>(result));
136    }
137   
138    // Sort
139  14 RepositoryUtils.sort(result, query.getSortClauses());
140   
141  14 return RepositoryUtils.getIterableResult(query.getOffset(), query.getLimit(), result);
142    }
143   
 
144  14 toggle protected List<InstalledExtension> filter(Collection<String> namespaces, ExtensionQuery query,
145    Collection<? extends InstalledExtension> installedExtensions)
146    {
147  14 Pattern patternMatcher = RepositoryUtils.createPatternMatcher(query.getQuery());
148   
149  14 List<InstalledExtension> result = new ArrayList<>(installedExtensions.size());
150   
151  14 for (InstalledExtension installedExtension : installedExtensions) {
152  70 if (namespaces == null || namespaces.isEmpty()) {
153  8 if (RepositoryUtils.matches(patternMatcher, query.getFilters(), installedExtension)) {
154  5 result.add(installedExtension);
155    }
156    } else {
157  62 for (String namespace : namespaces) {
158  62 if (installedExtension.isInstalled(namespace)) {
159  42 if (RepositoryUtils.matches(patternMatcher, query.getFilters(), installedExtension)) {
160  34 result.add(installedExtension);
161    }
162    }
163    }
164    }
165    }
166   
167  14 return result;
168   
169    }
170    }