| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 51 |
|
|
| 52 |
|
@version |
| 53 |
|
@since |
| 54 |
|
|
| 55 |
|
@Component |
| 56 |
|
@Singleton |
| |
|
| 84.1% |
Uncovered Elements: 10 (63) |
Complexity: 16 |
Complexity Density: 0.39 |
|
| 57 |
|
public class DefaultExtensionManagerConfiguration implements ExtensionManagerConfiguration |
| 58 |
|
{ |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
private static final Pattern REPOSITORYIDPATTERN = Pattern.compile("([^:]+):([^:]+):(.+)"); |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
private static final String DEFAULT_USERAGENT = "XWikiExtensionManager"; |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
private static final String CK_PREFIX = "extension."; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
private static final String CK_CORE_PREFIX = CK_PREFIX + "core."; |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
private static final String CK_REPOSITORIES_PREFIX = CK_PREFIX + "repositories."; |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
@Inject |
| 88 |
|
private Logger logger; |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
@Inject |
| 94 |
|
private Environment environment; |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
@Inject |
| 100 |
|
private Provider<ConfigurationSource> configuration; |
| 101 |
|
|
| 102 |
|
@Inject |
| 103 |
|
private ExtensionFactory extensionFactory; |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
@see |
| 109 |
|
|
| 110 |
|
private File localRepository; |
| 111 |
|
|
| 112 |
|
|
| 113 |
|
@return |
| 114 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 115 |
231 |
public File getHome()... |
| 116 |
|
{ |
| 117 |
231 |
return new File(this.environment.getPermanentDirectory(), "extension/"); |
| 118 |
|
} |
| 119 |
|
|
| |
|
| 70% |
Uncovered Elements: 3 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 120 |
231 |
@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 |
|
|
| |
|
| 91.7% |
Uncovered Elements: 2 (24) |
Complexity: 5 |
Complexity Density: 0.28 |
|
| 136 |
263 |
@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 |
|
|
| 171 |
|
|
| 172 |
188 |
for (ExtensionRepositoryDescriptor descriptor : repositories) { |
| 173 |
14 |
setRepositoryProperties((DefaultExtensionRepositoryDescriptor) descriptor); |
| 174 |
|
} |
| 175 |
|
} |
| 176 |
|
|
| 177 |
263 |
return repositories; |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
|
| 181 |
|
@param |
| 182 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 183 |
14 |
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 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 199 |
0 |
@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 |
|
@link |
| 213 |
|
|
| 214 |
|
@param |
| 215 |
|
@return@link |
| 216 |
|
@throws@link |
| 217 |
|
@throws |
| 218 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 219 |
15 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 234 |
235 |
@Override... |
| 235 |
|
public String getUserAgent() |
| 236 |
|
{ |
| 237 |
|
|
| 238 |
235 |
return this.configuration.get().getProperty(CK_PREFIX + "userAgent", DEFAULT_USERAGENT); |
| 239 |
|
} |
| 240 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 241 |
235 |
@Override... |
| 242 |
|
public boolean resolveCoreExtensions() |
| 243 |
|
{ |
| 244 |
235 |
return this.configuration.get().getProperty(CK_CORE_PREFIX + "resolve", true); |
| 245 |
|
} |
| 246 |
|
} |