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;
023
024import org.apache.maven.toolchain.DefaultToolchain;
025import org.apache.maven.toolchain.model.ToolchainModel;
026import org.codehaus.plexus.component.annotations.Component;
027import org.codehaus.plexus.logging.Logger;
028import org.codehaus.plexus.util.FileUtils;
029import org.codehaus.plexus.util.Os;
030
031/**
032 * @author Milos Kleint
033 */
034@Component( role = JavaToolChain.class )
035public class DefaultJavaToolChain
036    extends DefaultToolchain
037    implements JavaToolChain
038{
039    private String javaHome;
040
041    public static final String KEY_JAVAHOME = "jdkHome"; //NOI18N
042
043    public DefaultJavaToolChain( ToolchainModel model, Logger logger )
044    {
045        super( model, "jdk", logger );
046    }
047
048    public String getJavaHome()
049    {
050        return javaHome;
051    }
052
053    public void setJavaHome( String javaHome )
054    {
055        this.javaHome = javaHome;
056    }
057
058    public String toString()
059    {
060        return "JDK[" + getJavaHome() + "]";
061    }
062
063    public String findTool( String toolName )
064    {
065        File toRet = findTool( toolName, new File( FileUtils.normalize( getJavaHome() ) ) );
066        if ( toRet != null )
067        {
068            return toRet.getAbsolutePath();
069        }
070        return null;
071    }
072
073    private static File findTool( String toolName, File installFolder )
074    {
075        File bin = new File( installFolder, "bin" ); //NOI18N
076        if ( bin.exists() )
077        {
078            File tool = new File( bin, toolName + ( Os.isFamily( "windows" ) ? ".exe" : "" ) ); // NOI18N
079            if ( tool.exists() )
080            {
081                return tool;
082            }
083        }
084        return null;
085   }
086}