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

File DefaultExtensionManagerConfiguration.java

 

Coverage histogram

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

Code metrics

14
41
8
1
246
140
16
0.39
5.12
8
2

Classes

Class Line # Actions
DefaultExtensionManagerConfiguration 57 41 0% 16 10
0.8412698584.1%
 

Contributing tests

This file is covered by 26 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.io.File;
23    import java.net.URI;
24    import java.net.URISyntaxException;
25    import java.util.ArrayList;
26    import java.util.Collection;
27    import java.util.Collections;
28    import java.util.LinkedHashMap;
29    import java.util.List;
30    import java.util.Map;
31    import java.util.regex.Matcher;
32    import java.util.regex.Pattern;
33   
34    import javax.inject.Inject;
35    import javax.inject.Provider;
36    import javax.inject.Singleton;
37   
38    import org.apache.commons.lang3.StringUtils;
39    import org.apache.commons.lang3.exception.ExceptionUtils;
40    import org.slf4j.Logger;
41    import org.xwiki.component.annotation.Component;
42    import org.xwiki.configuration.ConfigurationSource;
43    import org.xwiki.environment.Environment;
44    import org.xwiki.extension.ExtensionManagerConfiguration;
45    import org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor;
46    import org.xwiki.extension.repository.ExtensionRepositoryDescriptor;
47    import org.xwiki.extension.repository.ExtensionRepositoryId;
48   
49    /**
50    * Default implementation of {@link ExtensionManagerConfiguration}.
51    *
52    * @version $Id: 94e16f66bcd415640bff313f1fbd5923ef7485f0 $
53    * @since 4.0M1
54    */
55    @Component
56    @Singleton
 
57    public class DefaultExtensionManagerConfiguration implements ExtensionManagerConfiguration
58    {
59    /**
60    * Used to parse repositories entries from the configuration.
61    */
62    private static final Pattern REPOSITORYIDPATTERN = Pattern.compile("([^:]+):([^:]+):(.+)");
63   
64    /**
65    * The default user agent.
66    */
67    private static final String DEFAULT_USERAGENT = "XWikiExtensionManager";
68   
69    /**
70    * The prefix of all the extension related properties.
71    */
72    private static final String CK_PREFIX = "extension.";
73   
74    /**
75    * The prefix of all the core extension related properties.
76    */
77    private static final String CK_CORE_PREFIX = CK_PREFIX + "core.";
78   
79    /**
80    * The prefix of all the extension repository related properties.
81    */
82    private static final String CK_REPOSITORIES_PREFIX = CK_PREFIX + "repositories.";
83   
84    /**
85    * The logger to log.
86    */
87    @Inject
88    private Logger logger;
89   
90    /**
91    * Used to get permanent directory.
92    */
93    @Inject
94    private Environment environment;
95   
96    /**
97    * The configuration.
98    */
99    @Inject
100    private Provider<ConfigurationSource> configuration;
101   
102    @Inject
103    private ExtensionFactory extensionFactory;
104   
105    // Cache
106   
107    /**
108    * @see DefaultExtensionManagerConfiguration#getLocalRepository()
109    */
110    private File localRepository;
111   
112    /**
113    * @return extension manage home folder
114    */
 
115  231 toggle public File getHome()
116    {
117  231 return new File(this.environment.getPermanentDirectory(), "extension/");
118    }
119   
 
120  231 toggle @Override
121    public File getLocalRepository()
122    {
123  231 if (this.localRepository == null) {
124  231 String localRepositoryPath = this.configuration.get().getProperty(CK_PREFIX + "localRepository");
125   
126  231 if (localRepositoryPath == null) {
127  231 this.localRepository = new File(getHome(), "repository/");
128    } else {
129  0 this.localRepository = new File(localRepositoryPath);
130    }
131    }
132   
133  231 return this.localRepository;
134    }
135   
 
136  263 toggle @Override
137    public Collection<ExtensionRepositoryDescriptor> getExtensionRepositoryDescriptors()
138    {
139  263 Collection<ExtensionRepositoryDescriptor> repositories;
140   
141  263 List<String> repositoryStrings =
142    this.configuration.get().getProperty(CK_PREFIX + "repositories", Collections.<String>emptyList());
143   
144  263 if (repositoryStrings.isEmpty()) {
145  75 repositories = null;
146    } else {
147  188 Map<String, ExtensionRepositoryDescriptor> repositoriesMap =
148    new LinkedHashMap<String, ExtensionRepositoryDescriptor>();
149  188 for (String repositoryString : repositoryStrings) {
150  193 if (StringUtils.isNotBlank(repositoryString)) {
151  15 try {
152  15 ExtensionRepositoryDescriptor extensionRepositoryId = parseRepository(repositoryString);
153  14 if (repositoriesMap.containsKey(extensionRepositoryId.getId())) {
154  0 this.logger.warn(
155    "Duplicated repository id in [{}] first found in [{}]. The last one will be used.",
156    extensionRepositoryId, repositoriesMap.get(extensionRepositoryId.getId()));
157    }
158  14 repositoriesMap.put(extensionRepositoryId.getId(), extensionRepositoryId);
159    } catch (Exception e) {
160  1 this.logger.warn("Ignoring invalid repository configuration [{}]. " + "Root cause [{}]",
161    repositoryString, ExceptionUtils.getRootCauseMessage(e));
162    }
163    } else {
164  178 this.logger.debug("Empty repository id found in the configuration");
165    }
166    }
167   
168  188 repositories = repositoriesMap.values();
169   
170    // Get extended properties
171   
172  188 for (ExtensionRepositoryDescriptor descriptor : repositories) {
173  14 setRepositoryProperties((DefaultExtensionRepositoryDescriptor) descriptor);
174    }
175    }
176   
177  263 return repositories;
178    }
179   
180    /**
181    * @param descriptor the repository descriptor to update with custom properties
182    */
 
183  14 toggle private void setRepositoryProperties(DefaultExtensionRepositoryDescriptor descriptor)
184    {
185  14 String id = descriptor.getId();
186   
187  14 String prefix = CK_REPOSITORIES_PREFIX + id + '.';
188   
189  14 ConfigurationSource configurationSource = this.configuration.get();
190   
191  14 for (String key : configurationSource.getKeys()) {
192  26 if (key.startsWith(prefix)) {
193  2 descriptor.putProperty(key.substring(prefix.length()),
194    configurationSource.getProperty(key, String.class));
195    }
196    }
197    }
198   
 
199  0 toggle @Override
200    public Collection<ExtensionRepositoryId> getRepositories()
201    {
202  0 Collection<ExtensionRepositoryId> repositories = new ArrayList<ExtensionRepositoryId>();
203   
204  0 for (ExtensionRepositoryDescriptor descriptor : getExtensionRepositoryDescriptors()) {
205  0 repositories.add(new ExtensionRepositoryId(descriptor));
206    }
207   
208  0 return repositories;
209    }
210   
211    /**
212    * Create a {@link DefaultExtensionRepositoryDescriptor} from a string entry.
213    *
214    * @param repositoryString the repository configuration entry
215    * @return the {@link DefaultExtensionRepositoryDescriptor}
216    * @throws URISyntaxException Failed to create an {@link URI} object from the configuration entry
217    * @throws ExtensionManagerConfigurationException Failed to parse configuration
218    */
 
219  15 toggle private ExtensionRepositoryDescriptor parseRepository(String repositoryString)
220    throws URISyntaxException, ExtensionManagerConfigurationException
221    {
222  15 Matcher matcher = REPOSITORYIDPATTERN.matcher(repositoryString);
223   
224  15 if (matcher.matches()) {
225  14 return this.extensionFactory.getExtensionRepositoryDescriptor(matcher.group(1), matcher.group(2),
226    new URI(matcher.group(3)));
227    }
228   
229  1 throw new ExtensionManagerConfigurationException(
230    String.format("Invalid repository configuration format for [%s]. Should have been matching [%s].",
231    repositoryString, REPOSITORYIDPATTERN.toString()));
232    }
233   
 
234  235 toggle @Override
235    public String getUserAgent()
236    {
237    // TODO: add version (need a way to get platform version first)
238  235 return this.configuration.get().getProperty(CK_PREFIX + "userAgent", DEFAULT_USERAGENT);
239    }
240   
 
241  235 toggle @Override
242    public boolean resolveCoreExtensions()
243    {
244  235 return this.configuration.get().getProperty(CK_CORE_PREFIX + "resolve", true);
245    }
246    }