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.model.superpom;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.HashMap;
28  import java.util.Map;
29  import java.util.Objects;
30  import java.util.concurrent.ConcurrentHashMap;
31  
32  import org.apache.maven.api.model.InputSource;
33  import org.apache.maven.model.Model;
34  import org.apache.maven.model.building.ModelProcessor;
35  
36  /**
37   * Provides the super POM that all models implicitly inherit from.
38   *
39   * @deprecated use {@link org.apache.maven.api.services.ModelBuilder} instead
40   */
41  @Named
42  @Singleton
43  @Deprecated(since = "4.0.0")
44  public class DefaultSuperPomProvider implements SuperPomProvider {
45  
46      private final ModelProcessor modelProcessor;
47  
48      /**
49       * The cached super POM, lazily created.
50       */
51      private static final Map<String, Model> SUPER_MODELS = new ConcurrentHashMap<>();
52  
53      @Inject
54      public DefaultSuperPomProvider(ModelProcessor modelProcessor) {
55          this.modelProcessor = modelProcessor;
56      }
57  
58      @Override
59      public Model getSuperModel(String version) {
60          return SUPER_MODELS.computeIfAbsent(Objects.requireNonNull(version), v -> {
61              String resource = "/org/apache/maven/model/pom-" + v + ".xml";
62  
63              InputStream is = getClass().getResourceAsStream(resource);
64  
65              if (is == null) {
66                  throw new IllegalStateException("The super POM " + resource + " was not found"
67                          + ", please verify the integrity of your Maven installation");
68              }
69  
70              try {
71                  Map<String, Object> options = new HashMap<>(2);
72                  options.put("xml:" + version, "xml:" + version);
73  
74                  String modelId = "org.apache.maven:maven-model-builder:" + version + "-"
75                          + this.getClass().getPackage().getImplementationVersion() + ":super-pom";
76                  InputSource inputSource = new InputSource(
77                          modelId, getClass().getResource(resource).toExternalForm());
78                  options.put(ModelProcessor.INPUT_SOURCE, new org.apache.maven.model.InputSource(inputSource));
79  
80                  return modelProcessor.read(is, options);
81              } catch (IOException e) {
82                  throw new IllegalStateException(
83                          "The super POM " + resource + " is damaged"
84                                  + ", please verify the integrity of your Maven installation",
85                          e);
86              }
87          });
88      }
89  }