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