View Javadoc
1   package org.apache.maven.model.superpom;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.inject.Inject;
28  import javax.inject.Named;
29  import javax.inject.Singleton;
30  
31  import org.apache.maven.api.model.InputSource;
32  import org.apache.maven.api.model.Model;
33  import org.apache.maven.model.building.ModelProcessor;
34  
35  /**
36   * Provides the super POM that all models implicitly inherit from.
37   *
38   * @author Benjamin Bentmann
39   */
40  @Named
41  @Singleton
42  public class DefaultSuperPomProvider
43      implements SuperPomProvider
44  {
45  
46      private final ModelProcessor modelProcessor;
47  
48      /**
49       * The cached super POM, lazily created.
50       */
51      private Model superModel;
52  
53      @Inject
54      public DefaultSuperPomProvider( ModelProcessor modelProcessor )
55      {
56          this.modelProcessor = modelProcessor;
57      }
58  
59      @Override
60      public Model getSuperModel( String version )
61      {
62          if ( superModel == null )
63          {
64              String resource = "/org/apache/maven/model/pom-" + version + ".xml";
65  
66              InputStream is = getClass().getResourceAsStream( resource );
67  
68              if ( is == null )
69              {
70                  throw new IllegalStateException( "The super POM " + resource + " was not found"
71                      + ", please verify the integrity of your Maven installation" );
72              }
73  
74              try
75              {
76                  Map<String, Object> options = new HashMap<>( 2 );
77                  options.put( "xml:4.0.0", "xml:4.0.0" );
78  
79                  String modelId = "org.apache.maven:maven-model-builder:"
80                      + this.getClass().getPackage().getImplementationVersion() + ":super-pom";
81                  InputSource inputSource = new InputSource(
82                          modelId, getClass().getResource( resource ).toExternalForm() );
83                  options.put( ModelProcessor.INPUT_SOURCE, inputSource );
84  
85                  superModel = modelProcessor.read( is, options );
86              }
87              catch ( IOException e )
88              {
89                  throw new IllegalStateException( "The super POM " + resource + " is damaged"
90                      + ", please verify the integrity of your Maven installation", e );
91              }
92          }
93  
94          return superModel;
95      }
96  
97  }