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

File DefaultCoreExtensionRepository.java

 

Coverage histogram

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

Code metrics

16
48
16
1
263
176
32
0.67
3
16
2

Classes

Class Line # Actions
DefaultCoreExtensionRepository 62 48 0% 32 29
0.637563.7%
 

Contributing tests

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