001package org.apache.maven.toolchain;
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.File;
023import java.io.Reader;
024
025import org.apache.maven.toolchain.model.PersistedToolchains;
026import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
027import org.codehaus.plexus.component.annotations.Component;
028import org.codehaus.plexus.component.annotations.Requirement;
029import org.codehaus.plexus.logging.Logger;
030import org.codehaus.plexus.util.IOUtil;
031import org.codehaus.plexus.util.ReaderFactory;
032
033/**
034 * @author Benjamin Bentmann
035 */
036@Component( role = ToolchainsBuilder.class, hint = "default" )
037public class DefaultToolchainsBuilder
038    implements ToolchainsBuilder
039{
040
041    @Requirement
042    private Logger logger;
043
044    public PersistedToolchains build( File userToolchainsFile )
045        throws MisconfiguredToolchainException
046    {
047        PersistedToolchains toolchains = null;
048
049        if ( userToolchainsFile != null && userToolchainsFile.isFile() )
050        {
051            Reader in = null;
052            try
053            {
054                in = ReaderFactory.newXmlReader( userToolchainsFile );
055                toolchains = new MavenToolchainsXpp3Reader().read( in );
056            }
057            catch ( Exception e )
058            {
059                throw new MisconfiguredToolchainException( "Cannot read toolchains file at "
060                    + userToolchainsFile.getAbsolutePath(), e );
061            }
062            finally
063            {
064                IOUtil.close( in );
065            }
066        }
067        else if ( userToolchainsFile != null )
068        {
069            logger.debug( "Toolchains configuration was not found at " + userToolchainsFile );
070        }
071
072        return toolchains;
073    }
074
075}