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    @Override
057    public Model getSuperModel( String version )
058    {
059        if ( superModel == null )
060        {
061            String resource = "/org/apache/maven/model/pom-" + version + ".xml";
062
063            InputStream is = getClass().getResourceAsStream( resource );
064
065            if ( is == null )
066            {
067                throw new IllegalStateException( "The super POM " + resource + " was not found"
068                    + ", please verify the integrity of your Maven installation" );
069            }
070
071            try
072            {
073                Map<String, String> options = new HashMap<String, String>();
074                options.put( "xml:4.0.0", "xml:4.0.0" );
075                superModel = modelProcessor.read( is, options );
076            }
077            catch ( IOException e )
078            {
079                throw new IllegalStateException( "The super POM " + resource + " is damaged"
080                    + ", please verify the integrity of your Maven installation", e );
081            }
082        }
083
084        return superModel;
085    }
086
087}