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      public ToolchainPrivate createToolchain(ToolchainModel model) throws MisconfiguredToolchainException {
50          if (model == null) {
51              return null;
52          }
53  
54          // use DefaultJavaToolChain for compatibility with maven 3.2.3 and earlier
55  
56          @SuppressWarnings("deprecation")
57          JavaToolchainImpl jtc = new DefaultJavaToolChain(model, logger);
58  
59          // populate the provides section
60          Properties provides = model.getProvides();
61          for (Entry<Object, Object> provide : provides.entrySet()) {
62              String key = (String) provide.getKey();
63              String value = (String) provide.getValue();
64  
65              if (value == null) {
66                  throw new MisconfiguredToolchainException(
67                          "Provides token '" + key + "' doesn't have any value configured.");
68              }
69  
70              RequirementMatcher matcher;
71              if ("version".equals(key)) {
72                  matcher = RequirementMatcherFactory.createVersionMatcher(value);
73              } else {
74                  matcher = RequirementMatcherFactory.createExactMatcher(value);
75              }
76  
77              jtc.addProvideToken(key, matcher);
78          }
79  
80          // populate the configuration section
81          Xpp3Dom dom = (Xpp3Dom) model.getConfiguration();
82          Xpp3Dom javahome = dom != null ? dom.getChild(JavaToolchainImpl.KEY_JAVAHOME) : null;
83          if (javahome == null) {
84              throw new MisconfiguredToolchainException(
85                      "Java toolchain without the " + JavaToolchainImpl.KEY_JAVAHOME + " configuration element.");
86          }
87          Path normal = Paths.get(javahome.getValue()).normalize();
88          if (Files.exists(normal)) {
89              jtc.setJavaHome(Paths.get(javahome.getValue()).normalize().toString());
90          } else {
91              throw new MisconfiguredToolchainException(
92                      "Non-existing JDK home configuration at " + normal.toAbsolutePath());
93          }
94  
95          return jtc;
96      }
97  
98      public ToolchainPrivate createDefaultToolchain() {
99          // not sure it's necessary to provide a default toolchain here.
100         // only version can be eventually supplied, and
101         return null;
102     }
103 
104     protected Logger getLogger() {
105         return logger;
106     }
107 }