View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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       * The cached super POM, lazily created.
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  }