001    package 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    
022    import java.io.File;
023    
024    import org.apache.maven.toolchain.MisconfiguredToolchainException;
025    import org.apache.maven.toolchain.RequirementMatcherFactory;
026    import org.apache.maven.toolchain.ToolchainFactory;
027    import org.apache.maven.toolchain.ToolchainPrivate;
028    import org.apache.maven.toolchain.model.ToolchainModel;
029    import org.codehaus.plexus.component.annotations.Component;
030    import org.codehaus.plexus.component.annotations.Requirement;
031    import org.codehaus.plexus.logging.Logger;
032    import org.codehaus.plexus.util.FileUtils;
033    import org.codehaus.plexus.util.xml.Xpp3Dom;
034    
035    /**
036     *
037     * @author mkleint
038     */
039    @Component( role = ToolchainFactory.class, hint = "jdk" )
040    public class DefaultJavaToolchainFactory
041        implements ToolchainFactory
042    {
043    
044        @Requirement
045        private Logger logger;
046    
047        public DefaultJavaToolchainFactory()
048        {
049        }
050    
051        public ToolchainPrivate createToolchain( ToolchainModel model )
052            throws MisconfiguredToolchainException
053        {
054            if ( model == null )
055            {
056                return null;
057            }
058            DefaultJavaToolChain jtc = new DefaultJavaToolChain( model, logger );
059            Xpp3Dom dom = (Xpp3Dom) model.getConfiguration();
060            Xpp3Dom javahome = dom.getChild( DefaultJavaToolChain.KEY_JAVAHOME );
061            if ( javahome == null )
062            {
063                throw new MisconfiguredToolchainException( "Java toolchain without the "
064                    + DefaultJavaToolChain.KEY_JAVAHOME + " configuration element." );
065            }
066            File normal = new File( FileUtils.normalize( javahome.getValue() ) );
067            if ( normal.exists() )
068            {
069                jtc.setJavaHome( FileUtils.normalize( javahome.getValue() ) );
070            }
071            else
072            {
073                throw new MisconfiguredToolchainException( "Non-existing JDK home configuration at "
074                    + normal.getAbsolutePath() );
075            }
076    
077            //now populate the provides section.
078            //TODO possibly move at least parts to a utility method or abstract implementation.
079            dom = (Xpp3Dom) model.getProvides();
080            Xpp3Dom[] provides = dom.getChildren();
081            for ( Xpp3Dom provide : provides )
082            {
083                String key = provide.getName();
084                String value = provide.getValue();
085                if ( value == null )
086                {
087                    throw new MisconfiguredToolchainException(
088                        "Provides token '" + key + "' doesn't have any value configured." );
089                }
090                if ( "version".equals( key ) )
091                {
092                    jtc.addProvideToken( key, RequirementMatcherFactory.createVersionMatcher( value ) );
093                }
094                else
095                {
096                    jtc.addProvideToken( key, RequirementMatcherFactory.createExactMatcher( value ) );
097                }
098            }
099            return jtc;
100        }
101    
102        public ToolchainPrivate createDefaultToolchain()
103        {
104            //not sure it's necessary to provide a default toolchain here.
105            //only version can be eventually supplied, and
106            return null;
107        }
108    
109        protected Logger getLogger()
110        {
111            return logger;
112        }
113    
114    }