-
Notifications
You must be signed in to change notification settings - Fork 125
/
AetherExtensionRepository.java
303 lines (248 loc) · 12.5 KB
/
AetherExtensionRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.extension.repository.aether.internal;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Developer;
import org.apache.maven.model.License;
import org.apache.maven.model.Model;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.artifact.ArtifactTypeRegistry;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.impl.ArtifactDescriptorReader;
import org.sonatype.aether.impl.VersionRangeResolver;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.resolution.ArtifactDescriptorRequest;
import org.sonatype.aether.resolution.ArtifactDescriptorResult;
import org.sonatype.aether.resolution.VersionRangeRequest;
import org.sonatype.aether.resolution.VersionRangeResolutionException;
import org.sonatype.aether.resolution.VersionRangeResult;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.version.Version;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.extension.DefaultExtensionAuthor;
import org.xwiki.extension.DefaultExtensionDependency;
import org.xwiki.extension.Extension;
import org.xwiki.extension.ExtensionDependency;
import org.xwiki.extension.ExtensionId;
import org.xwiki.extension.ExtensionLicense;
import org.xwiki.extension.ExtensionLicenseManager;
import org.xwiki.extension.ResolveException;
import org.xwiki.extension.repository.AbstractExtensionRepository;
import org.xwiki.extension.repository.ExtensionRepositoryId;
import org.xwiki.extension.repository.aether.internal.plexus.PlexusComponentManager;
import org.xwiki.properties.ConverterManager;
public class AetherExtensionRepository extends AbstractExtensionRepository
{
public static final String MPKEYPREFIX = "xwiki.extension.";
public static final String MPKEY_FEATURES = MPKEYPREFIX + "features";
private ComponentManager componentManager;
private PlexusComponentManager plexusComponentManager;
private RemoteRepository remoteRepository;
private ArtifactDescriptorReader mavenDescriptorReader;
private VersionRangeResolver versionRangeResolver;
private static Method loadPomMethod;
private static Method convertMethod;
private ConverterManager converter;
private ExtensionLicenseManager licenseManager;
private AetherExtensionRepositoryFactory repositoryFactory;
public AetherExtensionRepository(ExtensionRepositoryId repositoryId,
AetherExtensionRepositoryFactory repositoryFactory, PlexusComponentManager mavenComponentManager,
ComponentManager componentManager) throws Exception
{
super(repositoryId);
this.repositoryFactory = repositoryFactory;
this.componentManager = componentManager;
this.plexusComponentManager = mavenComponentManager;
this.remoteRepository = new RemoteRepository(repositoryId.getId(), "default", repositoryId.getURI().toString());
this.converter = this.componentManager.lookup(ConverterManager.class);
this.licenseManager = this.componentManager.lookup(ExtensionLicenseManager.class);
this.versionRangeResolver = this.plexusComponentManager.getPlexus().lookup(VersionRangeResolver.class);
try {
this.mavenDescriptorReader = this.plexusComponentManager.getPlexus().lookup(ArtifactDescriptorReader.class);
if (loadPomMethod == null) {
// FIXME: not very nice
// * use a private method of a library we don't control is not the nicest thing. But it's a big and very
// uselfull method. A shame is not a bit more public.
// * having to parse the pom.xml since we are supposed to support anything supported by aether is not
// very
// clean
// either. But Aether almost resolve nothing, not even the type of the artifact, we pretty much get only
// dependencies and licenses.
loadPomMethod =
this.mavenDescriptorReader.getClass().getDeclaredMethod("loadPom", RepositorySystemSession.class,
ArtifactDescriptorRequest.class, ArtifactDescriptorResult.class);
loadPomMethod.setAccessible(true);
convertMethod =
this.mavenDescriptorReader.getClass().getDeclaredMethod("convert",
org.apache.maven.model.Dependency.class, ArtifactTypeRegistry.class);
convertMethod.setAccessible(true);
}
} catch (ComponentLookupException e) {
// Maven handler not found
}
}
public RepositorySystemSession createRepositorySystemSession()
{
return this.repositoryFactory.createRepositorySystemSession();
}
@Override
public Extension resolve(ExtensionId extensionId) throws ResolveException
{
return resolve(new DefaultExtensionDependency(extensionId.getId(), extensionId.getVersion()));
}
@Override
public Extension resolve(ExtensionDependency extensionDependency) throws ResolveException
{
if (getId().getType().equals("maven") && this.mavenDescriptorReader != null) {
return resolveMaven(extensionDependency);
} else {
// FIXME: impossible to resolve extension type as well as most of the information with pure Aether API
throw new ResolveException("Unsupported");
}
}
public Version resolveVersionRange(Artifact artifact, RepositorySystemSession session) throws ResolveException
{
VersionRangeResult rangeResult;
try {
VersionRangeRequest rangeRequest = new VersionRangeRequest();
rangeRequest.setArtifact(artifact);
rangeRequest.addRepository(this.remoteRepository);
rangeResult = this.versionRangeResolver.resolveVersionRange(session, rangeRequest);
if (rangeResult.getVersions().isEmpty()) {
throw new VersionRangeResolutionException(rangeResult, "No versions available for " + artifact
+ " within specified range");
}
} catch (VersionRangeResolutionException e) {
throw new ResolveException("Failed to resolve version range", e);
}
return rangeResult.getVersions().get(rangeResult.getVersions().size() - 1);
}
public AetherExtension resolveMaven(ExtensionDependency extensionDependency) throws ResolveException
{
RepositorySystemSession session = createRepositorySystemSession();
Artifact artifact;
if (extensionDependency instanceof AetherExtensionDependency) {
artifact = ((AetherExtensionDependency) extensionDependency).getAetherDependency().getArtifact();
} else {
artifact = AetherUtils.createArtifact(extensionDependency.getId(), extensionDependency.getVersion());
}
// Resolve version range
Version version = resolveVersionRange(artifact, session);
artifact = artifact.setVersion(version.toString());
// Get descriptor
Model model;
try {
model = loadPom(artifact, session);
} catch (Exception e) {
throw new ResolveException("Failed to resolve extension [" + extensionDependency + "] descriptor", e);
}
// Set type
String artifactExtension;
if (extensionDependency instanceof AetherExtensionDependency) {
artifactExtension =
((AetherExtensionDependency) extensionDependency).getAetherDependency().getArtifact().getExtension();
} else {
// See bundle as jar packages since bundle are actually store as jar files
artifactExtension = model.getPackaging().equals("bundle") ? "jar" : model.getPackaging();
}
artifact =
new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(),
artifactExtension, artifact.getVersion());
AetherExtension extension = new AetherExtension(artifact, model, this, this.plexusComponentManager);
extension.setName(model.getName());
extension.setSummary(model.getDescription());
for (Developer developer : model.getDevelopers()) {
URL authorURL = null;
if (developer.getUrl() != null) {
try {
authorURL = new URL(developer.getUrl());
} catch (MalformedURLException e) {
// TODO: log ?
}
}
extension.addAuthor(new DefaultExtensionAuthor(StringUtils.defaultIfBlank(developer.getName(),
developer.getId()), authorURL));
}
extension.setWebsite(model.getUrl());
// licenses
for (License license : model.getLicenses()) {
extension.addLicense(getExtensionLicense(license));
}
// features
String featuresString = model.getProperties().getProperty(MPKEY_FEATURES);
if (StringUtils.isNotBlank(featuresString)) {
extension.setFeatures(this.converter.<Collection<String>> convert(List.class, featuresString));
}
// dependencies
try {
ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
for (org.apache.maven.model.Dependency mavenDependency : model.getDependencies()) {
if (!mavenDependency.isOptional()
&& (mavenDependency.getScope().equals("compile") || mavenDependency.getScope().equals("runtime"))) {
extension
.addDependency(new AetherExtensionDependency(convertToAether(mavenDependency, stereotypes)));
}
}
} catch (Exception e) {
throw new ResolveException("Failed to resolve dependencies", e);
}
return extension;
}
private Dependency convertToAether(org.apache.maven.model.Dependency dependency, ArtifactTypeRegistry stereotypes)
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
{
return (Dependency) convertMethod.invoke(this.mavenDescriptorReader, dependency, stereotypes);
}
// TODO: download custom licenses content
private ExtensionLicense getExtensionLicense(License license)
{
if (license.getName() == null) {
return new ExtensionLicense("noname", null);
}
return createLicenseByName(license.getName());
}
private ExtensionLicense createLicenseByName(String name)
{
ExtensionLicense extensionLicense = this.licenseManager.getLicense(name);
return extensionLicense != null ? extensionLicense : new ExtensionLicense(name, null);
}
private Model loadPom(Artifact artifact, RepositorySystemSession session) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException
{
ArtifactDescriptorRequest artifactDescriptorRequest = new ArtifactDescriptorRequest();
artifactDescriptorRequest.setArtifact(artifact);
artifactDescriptorRequest.addRepository(this.remoteRepository);
ArtifactDescriptorResult artifactDescriptorResult = new ArtifactDescriptorResult(artifactDescriptorRequest);
return (Model) loadPomMethod.invoke(this.mavenDescriptorReader, session, artifactDescriptorRequest,
artifactDescriptorResult);
}
public RemoteRepository getRemoteRepository()
{
return this.remoteRepository;
}
}