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

File DefaultCoreExtensionRepository.java

 

Coverage histogram

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

Code metrics

14
50
19
1
274
180
33
0.66
2.63
19
1.74

Classes

Class Line # Actions
DefaultCoreExtensionRepository 60 50 0% 33 29
0.650602465.1%
 

Contributing tests

This file is covered by 121 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.core;
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.Map;
27    import java.util.concurrent.ConcurrentHashMap;
28   
29    import javax.inject.Inject;
30    import javax.inject.Singleton;
31   
32    import org.slf4j.Logger;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.component.phase.Initializable;
35    import org.xwiki.component.phase.InitializationException;
36    import org.xwiki.extension.CoreExtension;
37    import org.xwiki.extension.Extension;
38    import org.xwiki.extension.ExtensionDependency;
39    import org.xwiki.extension.ExtensionId;
40    import org.xwiki.extension.ExtensionNotFoundException;
41    import org.xwiki.extension.ResolveException;
42    import org.xwiki.extension.repository.AbstractExtensionRepository;
43    import org.xwiki.extension.repository.CoreExtensionRepository;
44    import org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor;
45    import org.xwiki.extension.repository.internal.RepositoryUtils;
46    import org.xwiki.extension.repository.result.CollectionIterableResult;
47    import org.xwiki.extension.repository.result.IterableResult;
48    import org.xwiki.extension.repository.search.ExtensionQuery;
49    import org.xwiki.extension.repository.search.SearchException;
50    import org.xwiki.extension.version.Version;
51   
52    /**
53    * Default implementation of {@link CoreExtensionRepository}.
54    *
55    * @version $Id: 3b643689ccf039c8640be4f16e136a4f25110bce $
56    * @since 4.0M1
57    */
58    @Component
59    @Singleton
 
60    public class DefaultCoreExtensionRepository extends AbstractExtensionRepository
61    implements CoreExtensionRepository, Initializable
62    {
63    /**
64    * The core extensions.
65    */
66    protected transient Map<String, DefaultCoreExtension> extensions = new ConcurrentHashMap<>();
67   
68    /**
69    * The extension associated to the environment.
70    */
71    protected transient DefaultCoreExtension environmentExtension;
72   
73    /**
74    * The logger to log.
75    */
76    @Inject
77    private transient Logger logger;
78   
79    /**
80    * Used to scan jars to find extensions.
81    */
82    @Inject
83    private transient CoreExtensionScanner scanner;
84   
85    /**
86    * Default constructor.
87    */
 
88  271 toggle public DefaultCoreExtensionRepository()
89    {
90  271 super(new DefaultExtensionRepositoryDescriptor("core", "xwiki-core", null));
91    }
92   
 
93  0 toggle @Override
94    public boolean isFilterable()
95    {
96  0 return true;
97    }
98   
 
99  0 toggle @Override
100    public boolean isSortable()
101    {
102  0 return true;
103    }
104   
 
105  224 toggle @Override
106    public void initialize() throws InitializationException
107    {
108  224 try {
109  224 this.extensions.putAll(this.scanner.loadExtensions(this));
110   
111  224 this.environmentExtension = this.scanner.loadEnvironmentExtension(this);
112  224 if (this.environmentExtension != null) {
113  43 this.extensions.put(this.environmentExtension.getId().getId(), this.environmentExtension);
114    }
115   
116    // Put extensions features in the map
117  224 for (DefaultCoreExtension coreExtension : this.extensions.values()) {
118  21766 addExtensionFeatures(coreExtension);
119    }
120   
121    } catch (Exception e) {
122  0 this.logger.warn("Failed to load core extensions", e);
123    }
124    }
125   
126    /**
127    * Update core extensions only if there is any remote repository and it's not disabled.
128    */
 
129  10 toggle public void updateExtensions()
130    {
131    // Start a background thread to get more details about the found extensions
132  10 Thread thread = new Thread(new Runnable()
133    {
 
134  10 toggle @Override
135    public void run()
136    {
137  10 DefaultCoreExtensionRepository.this.scanner
138    .updateExtensions(DefaultCoreExtensionRepository.this.extensions.values());
139    }
140    });
141   
142  10 thread.setPriority(Thread.MIN_PRIORITY);
143  10 thread.setDaemon(true);
144  10 thread.setName("Core extension repository updater");
145  10 thread.start();
146    }
147   
148    /**
149    * @since 9.4RC1
150    */
 
151  1280 toggle protected void addExtension(DefaultCoreExtension coreExtension)
152    {
153  1280 this.extensions.put(coreExtension.getId().getId(), coreExtension);
154   
155  1280 addExtensionFeatures(coreExtension);
156    }
157   
 
158  23046 toggle private void addExtensionFeatures(DefaultCoreExtension coreExtension)
159    {
160  23046 for (ExtensionId feature : coreExtension.getExtensionFeatures()) {
161  4507 this.extensions.put(feature.getId(), coreExtension);
162    }
163    }
164   
165    // Repository
166   
 
167  16737 toggle @Override
168    public CoreExtension resolve(ExtensionId extensionId) throws ResolveException
169    {
170  16737 CoreExtension extension = getCoreExtension(extensionId.getId());
171   
172  16737 if (extension == null
173    || (extensionId.getVersion() != null && !extension.getId().getVersion().equals(extensionId.getVersion()))) {
174  16734 throw new ExtensionNotFoundException("Could not find extension [" + extensionId + "]");
175    }
176   
177  3 return extension;
178    }
179   
 
180  77998 toggle @Override
181    public CoreExtension resolve(ExtensionDependency extensionDependency) throws ResolveException
182    {
183  77998 CoreExtension extension = getCoreExtension(extensionDependency.getId());
184   
185  77998 if (extension == null
186    || (!extensionDependency.getVersionConstraint().containsVersion(extension.getId().getVersion()))) {
187  28943 throw new ExtensionNotFoundException("Could not find extension dependency [" + extensionDependency + "]");
188    }
189   
190  49055 return extension;
191    }
192   
 
193  0 toggle @Override
194    public boolean exists(ExtensionId extensionId)
195    {
196  0 Extension extension = getCoreExtension(extensionId.getId());
197   
198  0 if (extension == null
199    || (extensionId.getVersion() != null && !extension.getId().getVersion().equals(extensionId.getVersion()))) {
200  0 return false;
201    }
202   
203  0 return true;
204    }
205   
 
206  55901 toggle @Override
207    public boolean exists(String feature)
208    {
209  55901 return this.extensions.containsKey(feature);
210    }
211   
 
212  0 toggle @Override
213    public IterableResult<Version> resolveVersions(String id, int offset, int nb) throws ResolveException
214    {
215  0 Extension extension = getCoreExtension(id);
216   
217  0 if (extension == null) {
218  0 throw new ExtensionNotFoundException("Could not find extension with id [" + id + "]");
219    }
220   
221  0 Collection<Version> versions;
222  0 if (nb == 0 || offset > 0) {
223  0 versions = Collections.emptyList();
224    } else {
225  0 versions = Arrays.asList(extension.getId().getVersion());
226    }
227   
228  0 return new CollectionIterableResult<Version>(1, offset, versions);
229    }
230   
231    // CoreExtensionRepository
232   
 
233  38048 toggle @Override
234    public CoreExtension getEnvironmentExtension()
235    {
236  38048 return this.environmentExtension;
237    }
238   
 
239  2 toggle @Override
240    public int countExtensions()
241    {
242  2 return this.extensions.size();
243    }
244   
 
245  10 toggle @Override
246    public Collection<CoreExtension> getCoreExtensions()
247    {
248  10 return new ArrayList<CoreExtension>(this.extensions.values());
249    }
250   
 
251  318703 toggle @Override
252    public CoreExtension getCoreExtension(String feature)
253    {
254  318703 if (feature == null) {
255  0 return null;
256    }
257   
258  318703 return this.extensions.get(feature);
259    }
260   
261    // Searchable
262   
 
263  6 toggle @Override
264    public IterableResult<Extension> search(String pattern, int offset, int nb) throws SearchException
265    {
266  6 return (IterableResult) RepositoryUtils.searchInCollection(pattern, offset, nb, this.extensions.values(), true);
267    }
268   
 
269  0 toggle @Override
270    public IterableResult<Extension> search(ExtensionQuery query) throws SearchException
271    {
272  0 return (IterableResult) RepositoryUtils.searchInCollection(query, this.extensions.values(), true);
273    }
274    }