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 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
39
40
41
42
43
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
57
58 @SuppressWarnings("deprecation")
59 JavaToolchainImpl jtc = new DefaultJavaToolChain(model, logger);
60
61
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
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
102
103 return null;
104 }
105
106 protected Logger getLogger() {
107 return logger;
108 }
109 }