1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.toolchain.java;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.Map.Entry;
28 import java.util.Properties;
29
30 import org.apache.maven.toolchain.MisconfiguredToolchainException;
31 import org.apache.maven.toolchain.RequirementMatcher;
32 import org.apache.maven.toolchain.RequirementMatcherFactory;
33 import org.apache.maven.toolchain.ToolchainFactory;
34 import org.apache.maven.toolchain.ToolchainPrivate;
35 import org.apache.maven.toolchain.model.ToolchainModel;
36 import org.codehaus.plexus.util.xml.Xpp3Dom;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
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
58
59 @SuppressWarnings("deprecation")
60 JavaToolchainImpl jtc = new DefaultJavaToolChain(model, logger);
61
62
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
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 Path normal = Paths.get(javahome.getValue()).normalize();
91 if (Files.exists(normal)) {
92 jtc.setJavaHome(Paths.get(javahome.getValue()).normalize().toString());
93 } else {
94 throw new MisconfiguredToolchainException(
95 "Non-existing JDK home configuration at " + normal.toAbsolutePath());
96 }
97
98 return jtc;
99 }
100
101 public ToolchainPrivate createDefaultToolchain() {
102
103
104 return null;
105 }
106
107 protected Logger getLogger() {
108 return logger;
109 }
110 }