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.nio.file.Files;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  import java.util.Properties;
27  
28  import org.apache.maven.artifact.versioning.ArtifactVersion;
29  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
30  import org.apache.maven.toolchain.MisconfiguredToolchainException;
31  import org.apache.maven.toolchain.RequirementMatcher;
32  import org.apache.maven.toolchain.RequirementMatcherFactory;
33  import org.apache.maven.toolchain.ToolchainFactory;
34  import org.apache.maven.toolchain.ToolchainPrivate;
35  import org.apache.maven.toolchain.model.ToolchainModel;
36  import org.codehaus.plexus.util.xml.Xpp3Dom;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * JDK toolchain factory.
42   * This is a <code>ToolchainFactory</code> Plexus component registered with
43   * <code>jdk</code> hint.
44   *
45   * @since 2.0.9, renamed from <code>DefaultJavaToolchainFactory</code> in 3.2.4
46   * @deprecated Use {@link org.apache.maven.api.services.ToolchainFactory} instead.
47   */
48  @Deprecated(since = "4.0.0")
49  public class JavaToolchainFactory implements ToolchainFactory {
50      private final Logger logger = LoggerFactory.getLogger(getClass());
51  
52      @Override
53      public ToolchainPrivate createToolchain(ToolchainModel model) throws MisconfiguredToolchainException {
54          if (model == null) {
55              return null;
56          }
57  
58          // use DefaultJavaToolChain for compatibility with maven 3.2.3 and earlier
59  
60          @SuppressWarnings("deprecation")
61          JavaToolchainImpl jtc = new DefaultJavaToolChain(model, logger);
62  
63          // populate the provides section
64          Properties provides = model.getProvides();
65          for (Entry<Object, Object> provide : provides.entrySet()) {
66              String key = (String) provide.getKey();
67              String value = (String) provide.getValue();
68  
69              if (value == null) {
70                  throw new MisconfiguredToolchainException(
71                          "Provides token '" + key + "' doesn't have any value configured.");
72              }
73  
74              RequirementMatcher matcher;
75              if ("version".equals(key)) {
76                  matcher = RequirementMatcherFactory.createVersionMatcher(value);
77              } else {
78                  matcher = RequirementMatcherFactory.createExactMatcher(value);
79              }
80  
81              jtc.addProvideToken(key, matcher);
82          }
83  
84          // populate the configuration section
85          Xpp3Dom dom = (Xpp3Dom) model.getConfiguration();
86          Xpp3Dom javahome = dom != null ? dom.getChild(JavaToolchainImpl.KEY_JAVAHOME) : null;
87          if (javahome == null) {
88              throw new MisconfiguredToolchainException(
89                      "Java toolchain without the " + JavaToolchainImpl.KEY_JAVAHOME + " configuration element.");
90          }
91          Path normal = Paths.get(javahome.getValue()).normalize();
92          if (Files.exists(normal)) {
93              jtc.setJavaHome(Paths.get(javahome.getValue()).normalize().toString());
94          } else {
95              throw new MisconfiguredToolchainException(
96                      "Non-existing JDK home configuration at " + normal.toAbsolutePath());
97          }
98  
99          ArtifactVersion javaVersion = model.getProvides().entrySet().stream()
100                 .filter(entry -> "version".equals(entry.getKey()))
101                 .map(Map.Entry::getValue)
102                 .map(v -> new DefaultArtifactVersion((String) v))
103                 .findAny()
104                 .orElse(null);
105 
106         jtc.setJavaVersion(javaVersion);
107         return jtc;
108     }
109 
110     @Override
111     public ToolchainPrivate createDefaultToolchain() {
112         // not sure it's necessary to provide a default toolchain here.
113         // only version can be eventually supplied.
114         return null;
115     }
116 
117     protected Logger getLogger() {
118         return logger;
119     }
120 }