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.plugins.jar;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.BufferedReader;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.InputStreamReader;
28  import java.io.UncheckedIOException;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  import java.util.HashMap;
32  import java.util.Map;
33  import java.util.Optional;
34  
35  import org.apache.maven.toolchain.Toolchain;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * Component provided JDK specification based on toolchains.
41   */
42  @Named
43  @Singleton
44  class ToolchainsJdkSpecification {
45  
46      private final Logger logger = LoggerFactory.getLogger(ToolchainsJdkSpecification.class);
47  
48      private final Map<Path, String> cache = new HashMap<>();
49  
50      public synchronized Optional<String> getJDKSpecification(Toolchain toolchain) {
51          Optional<Path> javacPath = getJavacPath(toolchain);
52          return javacPath.map(path -> cache.computeIfAbsent(path, this::getSpecForPath));
53      }
54  
55      private Optional<Path> getJavacPath(Toolchain toolchain) {
56          return Optional.ofNullable(toolchain.findTool("javac")).map(Paths::get).map(this::getCanonicalPath);
57      }
58  
59      private Path getCanonicalPath(Path path) {
60          try {
61              return path.toRealPath();
62          } catch (IOException e) {
63              if (path.getParent() != null) {
64                  return getCanonicalPath(path.getParent()).resolve(path.getFileName());
65              } else {
66                  throw new UncheckedIOException(e);
67              }
68          }
69      }
70  
71      private String getSpecForPath(Path path) {
72          try {
73              ProcessBuilder processBuilder = new ProcessBuilder(path.toString(), "-version");
74              processBuilder.redirectErrorStream(true);
75              Process process = processBuilder.start();
76              String version = readOutput(process.getInputStream()).trim();
77              process.waitFor();
78  
79              if (version.startsWith("javac ")) {
80                  version = version.substring(6);
81                  if (version.startsWith("1.")) {
82                      version = version.substring(0, 3);
83                  } else {
84                      version = version.substring(0, 2);
85                  }
86                  return version;
87              } else {
88                  logger.warn("Unrecognized output from {}: {}", processBuilder.command(), version);
89              }
90          } catch (IndexOutOfBoundsException | IOException e) {
91              logger.warn("Failed to execute: {} - {}", path, e.getMessage());
92          } catch (InterruptedException e) {
93              Thread.currentThread().interrupt();
94          }
95  
96          return null;
97      }
98  
99      private String readOutput(InputStream inputstream) throws IOException {
100         BufferedReader br = new BufferedReader(new InputStreamReader(inputstream));
101 
102         StringBuilder output = new StringBuilder();
103         String line;
104         while ((line = br.readLine()) != null) {
105             output.append(line + System.lineSeparator());
106         }
107 
108         return output.toString();
109     }
110 }