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