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