View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.toolchain.java;
20  
21  import java.io.File;
22  import org.apache.maven.toolchain.DefaultToolchain;
23  import org.apache.maven.toolchain.model.ToolchainModel;
24  import org.codehaus.plexus.util.FileUtils;
25  import org.codehaus.plexus.util.Os;
26  import org.slf4j.Logger;
27  
28  /**
29   * JDK toolchain implementation.
30   *
31   * @author Milos Kleint
32   * @since 2.0.9, renamed from DefaultJavaToolChain in 3.2.4
33   */
34  class JavaToolchainImpl extends DefaultToolchain implements JavaToolchain {
35      private String javaHome;
36  
37      public static final String KEY_JAVAHOME = "jdkHome"; // NOI18N
38  
39      JavaToolchainImpl(ToolchainModel model, Logger logger) {
40          super(model, "jdk", logger);
41      }
42  
43      public String getJavaHome() {
44          return javaHome;
45      }
46  
47      public void setJavaHome(String javaHome) {
48          this.javaHome = javaHome;
49      }
50  
51      public String toString() {
52          return "JDK[" + getJavaHome() + "]";
53      }
54  
55      public String findTool(String toolName) {
56          File toRet = findTool(toolName, new File(FileUtils.normalize(getJavaHome())));
57          if (toRet != null) {
58              return toRet.getAbsolutePath();
59          }
60          return null;
61      }
62  
63      private static File findTool(String toolName, File installFolder) {
64          File bin = new File(installFolder, "bin"); // NOI18N
65          if (bin.exists()) {
66              boolean isWindows = Os.isFamily("windows"); // NOI18N
67              if (isWindows) {
68                  File tool = new File(bin, toolName + ".exe");
69                  if (tool.exists()) {
70                      return tool;
71                  }
72              }
73              File tool = new File(bin, toolName);
74              if (tool.exists()) {
75                  return tool;
76              }
77          }
78          return null;
79      }
80  }