001package org.apache.maven.model.superpom;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.IOException;
023import java.io.InputStream;
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.maven.model.Model;
028import org.apache.maven.model.building.ModelProcessor;
029import org.codehaus.plexus.component.annotations.Component;
030import org.codehaus.plexus.component.annotations.Requirement;
031
032/**
033 * Provides the super POM that all models implicitly inherit from.
034 *
035 * @author Benjamin Bentmann
036 */
037@Component( role = SuperPomProvider.class )
038public class DefaultSuperPomProvider
039    implements SuperPomProvider
040{
041
042    /**
043     * The cached super POM, lazily created.
044     */
045    private Model superModel;
046
047    @Requirement
048    private ModelProcessor modelProcessor;
049
050    public DefaultSuperPomProvider setModelProcessor( ModelProcessor modelProcessor )
051    {
052        this.modelProcessor = modelProcessor;
053        return this;
054    }
055
056    public Model getSuperModel( String version )
057    {
058        if ( superModel == null )
059        {
060            String resource = "/org/apache/maven/model/pom-" + version + ".xml";
061
062            InputStream is = getClass().getResourceAsStream( resource );
063
064            if ( is == null )
065            {
066                throw new IllegalStateException( "The super POM " + resource + " was not found"
067                    + ", please verify the integrity of your Maven installation" );
068            }
069
070            try
071            {
072                Map<String, String> options = new HashMap<String, String>();
073                options.put( "xml:4.0.0", "xml:4.0.0" );
074                superModel = modelProcessor.read( is, options );
075            }
076            catch ( IOException e )
077            {
078                throw new IllegalStateException( "The super POM " + resource + " is damaged"
079                    + ", please verify the integrity of your Maven installation", e );
080            }
081        }
082
083        return superModel;
084    }
085
086}