slawekjaranowski commented on code in PR #73:
URL: https://github.com/apache/maven-jar-plugin/pull/73#discussion_r1535215589


##########
src/main/java/org/apache/maven/plugins/jar/ToolchainsJdkSpecification.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugins.jar;
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+import org.apache.maven.toolchain.Toolchain;
+import org.codehaus.plexus.util.cli.CommandLineException;
+import org.codehaus.plexus.util.cli.CommandLineUtils;
+import org.codehaus.plexus.util.cli.Commandline;
+
+/**
+ * Component provided JDK specification based on toolchains.
+ */
+@Named
+@Singleton
+class ToolchainsJdkSpecification {
+
+    private final Map<Path, String> cache = new HashMap<>();
+
+    public synchronized Optional<String> getJDKSpecification(Toolchain 
toolchain) {
+        Optional<Path> javaCPath = getJavaCPath(toolchain);
+        return javaCPath.map(path -> cache.computeIfAbsent(path, 
this::getSpecForPath));
+    }
+
+    private Optional<Path> getJavaCPath(Toolchain toolchain) {
+        return 
Optional.ofNullable(toolchain.findTool("javac")).map(Paths::get).map(this::getCanonicalPath);
+    }
+
+    private Path getCanonicalPath(Path path) {
+        try {
+            return path.toRealPath();
+        } catch (IOException e) {
+            if (path.getParent() != null) {
+                return 
getCanonicalPath(path.getParent()).resolve(path.getFileName());
+            } else {
+                throw new UncheckedIOException(e);
+            }
+        }
+    }
+
+    private String getSpecForPath(Path path) {
+        try {
+            Commandline cl = new Commandline(path.toString());
+            cl.createArg().setValue("-version");
+            CommandLineUtils.StringStreamConsumer out = new 
CommandLineUtils.StringStreamConsumer();
+            CommandLineUtils.StringStreamConsumer err = new 
CommandLineUtils.StringStreamConsumer();
+            CommandLineUtils.executeCommandLine(cl, out, err);
+            String version = out.getOutput().trim();
+            if (version.isEmpty()) {
+                version = err.getOutput().trim();
+            }
+            if (version.startsWith("javac ")) {
+                version = version.substring(6);
+                if (version.startsWith("1.")) {
+                    version = version.substring(0, 3);
+                } else {
+                    version = version.substring(0, 2);
+                }
+                return version;
+            } else {
+                return null;
+            }
+        } catch (CommandLineException e) {
+            throw new RuntimeException(e);

Review Comment:
   introduce own class for unchecked exception
   it is used in: `Map.computeIfAbsent` instead of it I need to check map item 
and add new item ... code will be more complicated



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to