001package org.apache.maven.toolchain.java;
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.util.Map.Entry;
024import java.util.Properties;
025
026import org.apache.maven.toolchain.MisconfiguredToolchainException;
027import org.apache.maven.toolchain.RequirementMatcher;
028import org.apache.maven.toolchain.RequirementMatcherFactory;
029import org.apache.maven.toolchain.ToolchainFactory;
030import org.apache.maven.toolchain.ToolchainPrivate;
031import org.apache.maven.toolchain.model.ToolchainModel;
032import org.codehaus.plexus.component.annotations.Component;
033import org.codehaus.plexus.component.annotations.Requirement;
034import org.codehaus.plexus.logging.Logger;
035import org.codehaus.plexus.util.FileUtils;
036import org.codehaus.plexus.util.xml.Xpp3Dom;
037
038/**
039 * JDK toolchain factory.
040 * This is a <code>ToolchainFactory</code> Plexus component registered with
041 * <code>jdk</code> hint.
042 *
043 * @author mkleint
044 * @since 2.0.9, renamed from <code>DefaultJavaToolchainFactory</code> in 3.2.4
045 */
046@Component( role = ToolchainFactory.class, hint = "jdk" )
047public class JavaToolchainFactory
048    implements ToolchainFactory
049{
050
051    @Requirement
052    private Logger logger;
053
054    public ToolchainPrivate createToolchain( ToolchainModel model )
055        throws MisconfiguredToolchainException
056    {
057        if ( model == null )
058        {
059            return null;
060        }
061
062        // use DefaultJavaToolChain for compatibility with maven 3.2.3 and earlier
063
064        @SuppressWarnings( "deprecation" )
065        JavaToolchainImpl jtc = new DefaultJavaToolChain( model, logger );
066
067        // populate the provides section
068        Properties provides = model.getProvides();
069        for ( Entry<Object, Object> provide : provides.entrySet() )
070        {
071            String key = (String) provide.getKey();
072            String value = (String) provide.getValue();
073
074            if ( value == null )
075            {
076                throw new MisconfiguredToolchainException(
077                    "Provides token '" + key + "' doesn't have any value configured." );
078            }
079
080            RequirementMatcher matcher;
081            if ( "version".equals( key ) )
082            {
083                matcher = RequirementMatcherFactory.createVersionMatcher( value );
084            }
085            else
086            {
087                matcher = RequirementMatcherFactory.createExactMatcher( value );
088            }
089
090            jtc.addProvideToken( key, matcher );
091        }
092
093        // populate the configuration section
094        Xpp3Dom dom = (Xpp3Dom) model.getConfiguration();
095        Xpp3Dom javahome = dom.getChild( JavaToolchainImpl.KEY_JAVAHOME );
096        if ( javahome == null )
097        {
098            throw new MisconfiguredToolchainException( "Java toolchain without the "
099                + JavaToolchainImpl.KEY_JAVAHOME + " configuration element." );
100        }
101        File normal = new File( FileUtils.normalize( javahome.getValue() ) );
102        if ( normal.exists() )
103        {
104            jtc.setJavaHome( FileUtils.normalize( javahome.getValue() ) );
105        }
106        else
107        {
108            throw new MisconfiguredToolchainException( "Non-existing JDK home configuration at "
109                + normal.getAbsolutePath() );
110        }
111
112        return jtc;
113    }
114
115    public ToolchainPrivate createDefaultToolchain()
116    {
117        //not sure it's necessary to provide a default toolchain here.
118        //only version can be eventually supplied, and
119        return null;
120    }
121
122    protected Logger getLogger()
123    {
124        return logger;
125    }
126
127}