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.converter; |
21 |
|
|
22 |
|
import java.lang.reflect.Type; |
23 |
|
import java.net.MalformedURLException; |
24 |
|
import java.net.URI; |
25 |
|
import java.net.URISyntaxException; |
26 |
|
import java.net.URL; |
27 |
|
import java.util.ArrayList; |
28 |
|
import java.util.Collection; |
29 |
|
import java.util.List; |
30 |
|
import java.util.Map; |
31 |
|
import java.util.Properties; |
32 |
|
|
33 |
|
import javax.inject.Inject; |
34 |
|
import javax.inject.Singleton; |
35 |
|
|
36 |
|
import org.apache.commons.lang3.StringUtils; |
37 |
|
import org.apache.maven.model.Dependency; |
38 |
|
import org.apache.maven.model.Developer; |
39 |
|
import org.apache.maven.model.IssueManagement; |
40 |
|
import org.apache.maven.model.License; |
41 |
|
import org.apache.maven.model.Model; |
42 |
|
import org.apache.maven.model.Repository; |
43 |
|
import org.apache.maven.model.Scm; |
44 |
|
import org.xwiki.component.annotation.Component; |
45 |
|
import org.xwiki.extension.DefaultExtensionScm; |
46 |
|
import org.xwiki.extension.Extension; |
47 |
|
import org.xwiki.extension.ExtensionDependency; |
48 |
|
import org.xwiki.extension.ExtensionLicense; |
49 |
|
import org.xwiki.extension.ExtensionLicenseManager; |
50 |
|
import org.xwiki.extension.ExtensionScmConnection; |
51 |
|
import org.xwiki.extension.internal.ExtensionFactory; |
52 |
|
import org.xwiki.extension.internal.ExtensionUtils; |
53 |
|
import org.xwiki.extension.internal.maven.DefaultMavenExtension; |
54 |
|
import org.xwiki.extension.internal.maven.DefaultMavenExtensionDependency; |
55 |
|
import org.xwiki.extension.internal.maven.MavenExtension; |
56 |
|
import org.xwiki.extension.internal.maven.MavenUtils; |
57 |
|
import org.xwiki.extension.repository.ExtensionRepositoryDescriptor; |
58 |
|
import org.xwiki.extension.version.Version; |
59 |
|
import org.xwiki.extension.version.internal.DefaultVersionConstraint; |
60 |
|
import org.xwiki.properties.converter.AbstractConverter; |
61 |
|
import org.xwiki.properties.converter.ConversionException; |
62 |
|
|
63 |
|
|
64 |
|
@link@link |
65 |
|
|
66 |
|
@version |
67 |
|
@since |
68 |
|
|
69 |
|
@Component |
70 |
|
@Singleton |
|
|
| 97.3% |
Uncovered Elements: 3 (110) |
Complexity: 30 |
Complexity Density: 0.4 |
|
71 |
|
public class ExtensionConverter extends AbstractConverter<Extension> |
72 |
|
{ |
73 |
|
@Inject |
74 |
|
private ExtensionLicenseManager licenseManager; |
75 |
|
|
76 |
|
@Inject |
77 |
|
private ExtensionFactory factory; |
78 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
79 |
24406 |
@Override... |
80 |
|
protected <G extends Extension> G convertToType(Type targetType, Object value) |
81 |
|
{ |
82 |
24406 |
if (value instanceof Model) { |
83 |
24406 |
return (G) convertToExtension((Model) value); |
84 |
|
} |
85 |
|
|
86 |
0 |
throw new ConversionException(String.format("Unsupported target type [%s]", targetType)); |
87 |
|
} |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (78) |
Complexity: 20 |
Complexity Density: 0.34 |
|
89 |
24406 |
private MavenExtension convertToExtension(Model model)... |
90 |
|
{ |
91 |
24406 |
Properties properties = (Properties) model.getProperties().clone(); |
92 |
|
|
93 |
24406 |
Version version = this.factory.getVersion(MavenUtils.resolveVersion(model)); |
94 |
24406 |
String groupId = MavenUtils.resolveGroupId(model); |
95 |
|
|
96 |
24406 |
DefaultMavenExtension extension = new DefaultMavenExtension(null, groupId, model.getArtifactId(), version, |
97 |
|
MavenUtils.packagingToType(model.getPackaging())); |
98 |
|
|
99 |
24406 |
extension.setName(getPropertyString(properties, Extension.FIELD_NAME, true, model.getName())); |
100 |
24406 |
extension.setSummary(getPropertyString(properties, Extension.FIELD_SUMMARY, true, model.getDescription())); |
101 |
24406 |
extension.setWebsite(getPropertyString(properties, Extension.FIELD_WEBSITE, true, model.getUrl())); |
102 |
|
|
103 |
|
|
104 |
24406 |
for (Developer developer : model.getDevelopers()) { |
105 |
60966 |
URL authorURL = null; |
106 |
60966 |
if (developer.getUrl() != null) { |
107 |
3507 |
try { |
108 |
3507 |
authorURL = new URL(developer.getUrl()); |
109 |
|
} catch (MalformedURLException e) { |
110 |
|
|
111 |
|
} |
112 |
|
} |
113 |
|
|
114 |
60966 |
extension.addAuthor(this.factory |
115 |
|
.getExtensionAuthor(StringUtils.defaultIfBlank(developer.getName(), developer.getId()), authorURL)); |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
24406 |
for (License license : model.getLicenses()) { |
120 |
15792 |
extension.addLicense(getExtensionLicense(license)); |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
24406 |
Scm scm = model.getScm(); |
125 |
24406 |
if (scm != null |
126 |
|
&& (scm.getConnection() != null || scm.getDeveloperConnection() != null || scm.getUrl() != null)) { |
127 |
15632 |
ExtensionScmConnection connection = MavenUtils.toExtensionScmConnection(scm.getConnection()); |
128 |
15632 |
ExtensionScmConnection developerConnection = |
129 |
|
MavenUtils.toExtensionScmConnection(scm.getDeveloperConnection()); |
130 |
|
|
131 |
15632 |
extension.setScm(new DefaultExtensionScm(scm.getUrl(), connection, developerConnection)); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
24406 |
IssueManagement issueManagement = model.getIssueManagement(); |
136 |
24406 |
if (issueManagement != null && issueManagement.getUrl() != null) { |
137 |
11331 |
extension.setIssueManagement( |
138 |
|
this.factory.getExtensionIssueManagement(issueManagement.getSystem(), issueManagement.getUrl())); |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
24406 |
String featuresString = getProperty(properties, Extension.FIELD_FEATURES, true); |
143 |
24406 |
if (StringUtils.isNotBlank(featuresString)) { |
144 |
979 |
Collection<String> features = ExtensionUtils.importPropertyStringList(featuresString, true); |
145 |
979 |
extension |
146 |
|
.setExtensionFeatures(ExtensionIdConverter.toExtensionIdList(features, extension.getId().getVersion())); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
24406 |
String categoryString = getProperty(properties, Extension.FIELD_CATEGORY, true); |
151 |
24406 |
if (StringUtils.isNotBlank(categoryString)) { |
152 |
12 |
extension.setCategory(categoryString); |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
24406 |
String namespacesString = getProperty(properties, Extension.FIELD_NAMESPACES, true); |
157 |
24406 |
if (StringUtils.isNotBlank(namespacesString)) { |
158 |
10 |
Collection<String> namespaces = ExtensionUtils.importPropertyStringList(namespacesString, true); |
159 |
10 |
extension.setAllowedNamespaces(namespaces); |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
24406 |
List<ExtensionRepositoryDescriptor> repositories; |
164 |
24406 |
List<Repository> mavenRepositories = model.getRepositories(); |
165 |
24406 |
if (!mavenRepositories.isEmpty()) { |
166 |
13861 |
repositories = new ArrayList<>(mavenRepositories.size()); |
167 |
|
|
168 |
13861 |
for (Repository mavenRepository : mavenRepositories) { |
169 |
46356 |
try { |
170 |
46356 |
ExtensionRepositoryDescriptor repositoryDescriptor = this.factory.getExtensionRepositoryDescriptor( |
171 |
|
mavenRepository.getId(), "maven", new URI(mavenRepository.getUrl())); |
172 |
|
|
173 |
46356 |
repositories.add(repositoryDescriptor); |
174 |
|
} catch (URISyntaxException e) { |
175 |
|
|
176 |
|
} |
177 |
|
} |
178 |
|
} else { |
179 |
10545 |
repositories = null; |
180 |
|
} |
181 |
24406 |
extension.setRepositories(repositories); |
182 |
|
|
183 |
|
|
184 |
24406 |
for (Dependency mavenDependency : model.getDependencies()) { |
185 |
121124 |
if (!mavenDependency.isOptional() && (mavenDependency.getScope() == null |
186 |
|
|| mavenDependency.getScope().equals("compile") || mavenDependency.getScope().equals("runtime"))) { |
187 |
71918 |
ExtensionDependency extensionDependency = toExtensionDependency(mavenDependency, model, repositories); |
188 |
|
|
189 |
71918 |
extension.addDependency(extensionDependency); |
190 |
|
} |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
24406 |
if (model.getDependencyManagement() != null) { |
195 |
7577 |
for (Dependency mavenDependency : model.getDependencyManagement().getDependencies()) { |
196 |
650501 |
ExtensionDependency extensionDependency = toExtensionDependency(mavenDependency, model, repositories); |
197 |
|
|
198 |
650501 |
extension.addManagedDependency(extensionDependency); |
199 |
|
} |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
24406 |
extension.putProperty(MavenUtils.PKEY_MAVEN_MODEL, model); |
205 |
|
|
206 |
24406 |
for (Map.Entry<Object, Object> entry : properties.entrySet()) { |
207 |
330193 |
String key = (String) entry.getKey(); |
208 |
330193 |
if (key.startsWith("xwiki.extension.")) { |
209 |
16 |
extension.putProperty(key, entry.getValue()); |
210 |
|
} |
211 |
|
} |
212 |
|
|
213 |
24406 |
return extension; |
214 |
|
} |
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
216 |
722419 |
private ExtensionDependency toExtensionDependency(Dependency mavenDependency, Model model,... |
217 |
|
List<ExtensionRepositoryDescriptor> repositories) |
218 |
|
{ |
219 |
722419 |
String dependencyGroupId = MavenUtils.resolveGroupId(mavenDependency.getGroupId(), model, true); |
220 |
722419 |
String dependencyArtifactId = mavenDependency.getArtifactId(); |
221 |
722419 |
String dependencyClassifier = mavenDependency.getClassifier(); |
222 |
722419 |
String dependencyVersion = MavenUtils.resolveVersion(mavenDependency.getVersion(), model, true); |
223 |
|
|
224 |
722419 |
DefaultMavenExtensionDependency dependency = new DefaultMavenExtensionDependency( |
225 |
|
MavenUtils.toExtensionId(dependencyGroupId, dependencyArtifactId, dependencyClassifier), |
226 |
|
new DefaultVersionConstraint(dependencyVersion), mavenDependency); |
227 |
|
|
228 |
722419 |
dependency.setRepositories(repositories); |
229 |
|
|
230 |
722419 |
return this.factory.getExtensionDependency(dependency); |
231 |
|
} |
232 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
233 |
146436 |
private String getProperty(Properties properties, String propertyName, boolean delete)... |
234 |
|
{ |
235 |
146436 |
return delete ? (String) properties.remove(Extension.IKEYPREFIX + propertyName) |
236 |
|
: properties.getProperty(Extension.IKEYPREFIX + propertyName); |
237 |
|
} |
238 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
239 |
73218 |
private String getPropertyString(Properties properties, String propertyName, boolean delete, String def)... |
240 |
|
{ |
241 |
73218 |
return StringUtils.defaultString(getProperty(properties, propertyName, delete), def); |
242 |
|
} |
243 |
|
|
244 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
245 |
15792 |
private ExtensionLicense getExtensionLicense(License license)... |
246 |
|
{ |
247 |
15792 |
if (license.getName() == null) { |
248 |
42 |
return new ExtensionLicense("noname", null); |
249 |
|
} |
250 |
|
|
251 |
15750 |
return createLicenseByName(license.getName()); |
252 |
|
} |
253 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
254 |
15750 |
private ExtensionLicense createLicenseByName(String name)... |
255 |
|
{ |
256 |
15750 |
ExtensionLicense extensionLicense = this.licenseManager.getLicense(name); |
257 |
|
|
258 |
15750 |
return extensionLicense != null ? extensionLicense : new ExtensionLicense(name, null); |
259 |
|
} |
260 |
|
} |