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.pmd.exec;
20  
21  import javax.inject.Provider;
22  
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.toolchain.Toolchain;
28  import org.apache.maven.toolchain.ToolchainManager;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Abstract service executor for CPD and PMD.
34   */
35  abstract class ServiceExecutor {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(ServiceExecutor.class);
38  
39      private final ToolchainManager toolchainManager;
40  
41      private final Provider<MavenSession> sessionProvider;
42  
43      protected ServiceExecutor(ToolchainManager toolchainManager, Provider<MavenSession> sessionProvider) {
44          this.toolchainManager = toolchainManager;
45          this.sessionProvider = sessionProvider;
46      }
47  
48      protected final Toolchain getToolchain(Map<String, String> jdkToolchain) {
49          Toolchain tc = null;
50  
51          if (jdkToolchain != null) {
52              List<Toolchain> tcs = toolchainManager.getToolchains(sessionProvider.get(), "jdk", jdkToolchain);
53              if (tcs != null && !tcs.isEmpty()) {
54                  tc = tcs.get(0);
55              }
56          }
57  
58          if (tc == null) {
59              tc = toolchainManager.getToolchainFromBuildContext("jdk", sessionProvider.get());
60          }
61  
62          return tc;
63      }
64  
65      protected String getJavaExecutable(Map<String, String> jdkToolchain) {
66          Toolchain tc = getToolchain(jdkToolchain);
67          if (tc != null) {
68              LOG.info("Toolchain in maven-pmd-plugin: {}", tc);
69              return tc.findTool("java"); // NOI18N
70          }
71          return null;
72      }
73  }