1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URL;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.concurrent.ConcurrentHashMap;
27
28 import org.apache.maven.api.di.Inject;
29 import org.apache.maven.api.di.Named;
30 import org.apache.maven.api.di.Singleton;
31 import org.apache.maven.api.model.Model;
32 import org.apache.maven.api.services.SuperPomProvider;
33 import org.apache.maven.api.services.model.ModelProcessor;
34 import org.apache.maven.api.services.xml.XmlReaderRequest;
35
36 @Named
37 @Singleton
38 public class DefaultSuperPomProvider implements SuperPomProvider {
39
40 private final ModelProcessor modelProcessor;
41
42
43
44
45 private static final Map<String, Model> SUPER_MODELS = new ConcurrentHashMap<>();
46
47 @Inject
48 public DefaultSuperPomProvider(ModelProcessor modelProcessor) {
49 this.modelProcessor = modelProcessor;
50 }
51
52 @Override
53 public Model getSuperPom(String version) {
54 return SUPER_MODELS.computeIfAbsent(Objects.requireNonNull(version), v -> readModel(version, v));
55 }
56
57 private Model readModel(String version, String v) {
58 String resource = "/org/apache/maven/model/pom-" + v + ".xml";
59 URL url = getClass().getResource(resource);
60 if (url == null) {
61 throw new IllegalStateException("The super POM " + resource + " was not found"
62 + ", please verify the integrity of your Maven installation");
63 }
64 try (InputStream is = url.openStream()) {
65 String modelId = "org.apache.maven:maven-model-builder:" + version + "-"
66 + this.getClass().getPackage().getImplementationVersion() + ":super-pom";
67 return modelProcessor.read(XmlReaderRequest.builder()
68 .modelId(modelId)
69 .location(url.toExternalForm())
70 .inputStream(is)
71 .strict(false)
72 .build());
73 } catch (IOException e) {
74 throw new IllegalStateException(
75 "The super POM " + resource + " is damaged"
76 + ", please verify the integrity of your Maven installation",
77 e);
78 }
79 }
80 }