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

File DefaultExtensionManager.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

6
33
6
1
193
124
18
0.55
5.5
6
3

Classes

Class Line # Actions
DefaultExtensionManager 56 33 0% 18 9
0.880%
 

Contributing tests

This file is covered by 3 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.internal;
21   
22    import java.util.HashMap;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Provider;
28    import javax.inject.Singleton;
29   
30    import org.slf4j.Logger;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.manager.ComponentLookupException;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.component.phase.Initializable;
35    import org.xwiki.component.phase.InitializationException;
36    import org.xwiki.extension.Extension;
37    import org.xwiki.extension.ExtensionDependency;
38    import org.xwiki.extension.ExtensionId;
39    import org.xwiki.extension.ExtensionManager;
40    import org.xwiki.extension.InstalledExtension;
41    import org.xwiki.extension.ResolveException;
42    import org.xwiki.extension.repository.CoreExtensionRepository;
43    import org.xwiki.extension.repository.ExtensionRepository;
44    import org.xwiki.extension.repository.ExtensionRepositoryManager;
45    import org.xwiki.extension.repository.InstalledExtensionRepository;
46    import org.xwiki.extension.repository.LocalExtensionRepository;
47   
48    /**
49    * Default implementation of {@link ExtensionManager}.
50    *
51    * @version $Id: 23f27b21cc313700359d7d4d124d86f33bcce7ef $
52    * @since 4.0M1
53    */
54    @Component
55    @Singleton
 
56    public class DefaultExtensionManager implements ExtensionManager, Initializable
57    {
58    /**
59    * Used to manipulate remote repositories.
60    */
61    @Inject
62    private ExtensionRepositoryManager repositoryManager;
63   
64    /**
65    * Used to manipulate core extensions.
66    */
67    @Inject
68    private CoreExtensionRepository coreExtensionRepository;
69   
70    /**
71    * Used to manipulate local extensions.
72    */
73    @Inject
74    private LocalExtensionRepository localExtensionRepository;
75   
76    /**
77    * Used to manipulate installed extensions.
78    */
79    @Inject
80    private InstalledExtensionRepository installedExtensionRepository;
81   
82    @Inject
83    @Named("context")
84    private Provider<ComponentManager> componentManagerProvider;
85   
86    @Inject
87    private Logger logger;
88   
89    /**
90    * The standard repositories.
91    */
92    private Map<String, ExtensionRepository> standardRepositories = new HashMap<String, ExtensionRepository>(3);
93   
 
94  17 toggle @Override
95    public void initialize() throws InitializationException
96    {
97  17 this.standardRepositories.put(this.coreExtensionRepository.getDescriptor().getId(),
98    this.coreExtensionRepository);
99  17 this.standardRepositories.put(this.localExtensionRepository.getDescriptor().getId(),
100    this.localExtensionRepository);
101  17 this.standardRepositories.put(this.installedExtensionRepository.getDescriptor().getId(),
102    this.installedExtensionRepository);
103    }
104   
 
105  244 toggle @Override
106    public Extension resolveExtension(ExtensionId extensionId) throws ResolveException
107    {
108  244 try {
109  244 return this.coreExtensionRepository.resolve(extensionId);
110    } catch (ResolveException notACoreExtension) {
111  242 return resolveExtensionFromInstalled(extensionId);
112    }
113    }
114   
115    /**
116    * @param extensionId the extension identifier
117    * @return the resolved extension
118    * @throws ResolveException error when trying to resolve extension
119    */
 
120  242 toggle private Extension resolveExtensionFromInstalled(ExtensionId extensionId) throws ResolveException
121    {
122  242 try {
123  242 return this.installedExtensionRepository.resolve(extensionId);
124    } catch (ResolveException notAnInstalledExtension) {
125  157 try {
126  157 return this.localExtensionRepository.resolve(extensionId);
127    } catch (ResolveException notALocalExtension) {
128  33 return this.repositoryManager.resolve(extensionId);
129    }
130    }
131    }
132   
 
133  0 toggle @Override
134    @Deprecated
135    public Extension resolveExtension(ExtensionDependency extensionDependency) throws ResolveException
136    {
137  0 try {
138  0 return this.coreExtensionRepository.resolve(extensionDependency);
139    } catch (ResolveException notACoreExtension) {
140  0 try {
141  0 return this.localExtensionRepository.resolve(extensionDependency);
142    } catch (ResolveException notALocalExtension) {
143  0 return this.repositoryManager.resolve(extensionDependency);
144    }
145    }
146    }
147   
 
148  11 toggle @Override
149    public Extension resolveExtension(ExtensionDependency extensionDependency, String namespace) throws ResolveException
150    {
151  11 try {
152  11 return this.coreExtensionRepository.resolve(extensionDependency);
153    } catch (ResolveException notACoreExtension) {
154  11 InstalledExtension extension =
155    this.installedExtensionRepository.getInstalledExtension(extensionDependency.getId(), namespace);
156   
157  11 if (extension != null
158    && extensionDependency.getVersionConstraint().containsVersion(extension.getId().getVersion())) {
159  1 return extension;
160    }
161   
162  10 try {
163  10 return this.localExtensionRepository.resolve(extensionDependency);
164    } catch (ResolveException notALocalExtension) {
165  10 return this.repositoryManager.resolve(extensionDependency);
166    }
167    }
168    }
169   
 
170  521 toggle @Override
171    public ExtensionRepository getRepository(String repositoryId)
172    {
173    // Try internal repositories
174  521 ExtensionRepository repository = this.standardRepositories.get(repositoryId);
175   
176    // Try component repositories
177  521 ComponentManager componentManager = this.componentManagerProvider.get();
178  521 if (componentManager.hasComponent(ExtensionRepository.class, repositoryId)) {
179  13 try {
180  13 return componentManager.getInstance(ExtensionRepository.class, repositoryId);
181    } catch (ComponentLookupException e) {
182  0 this.logger.error("Failed to lookup component", e);
183    }
184    }
185   
186    // Try remote repositories
187  508 if (repository == null) {
188  0 repository = this.repositoryManager.getRepository(repositoryId);
189    }
190   
191  508 return repository;
192    }
193    }