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