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