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.io.File;
22  import java.util.Map.Entry;
23  import java.util.Properties;
24  import javax.inject.Named;
25  import javax.inject.Singleton;
26  import org.apache.maven.toolchain.MisconfiguredToolchainException;
27  import org.apache.maven.toolchain.RequirementMatcher;
28  import org.apache.maven.toolchain.RequirementMatcherFactory;
29  import org.apache.maven.toolchain.ToolchainFactory;
30  import org.apache.maven.toolchain.ToolchainPrivate;
31  import org.apache.maven.toolchain.model.ToolchainModel;
32  import org.codehaus.plexus.util.FileUtils;
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   * @author mkleint
43   * @since 2.0.9, renamed from <code>DefaultJavaToolchainFactory</code> in 3.2.4
44   */
45  @Named("jdk")
46  @Singleton
47  public class JavaToolchainFactory implements ToolchainFactory {
48      private final Logger logger = LoggerFactory.getLogger(getClass());
49  
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          File normal = new File(FileUtils.normalize(javahome.getValue()));
89          if (normal.exists()) {
90              jtc.setJavaHome(FileUtils.normalize(javahome.getValue()));
91          } else {
92              throw new MisconfiguredToolchainException(
93                      "Non-existing JDK home configuration at " + normal.getAbsolutePath());
94          }
95  
96          return jtc;
97      }
98  
99      public ToolchainPrivate createDefaultToolchain() {
100         // not sure it's necessary to provide a default toolchain here.
101         // only version can be eventually supplied, and
102         return null;
103     }
104 
105     protected Logger getLogger() {
106         return logger;
107     }
108 }