1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.profile.activation;
20
21 import java.io.File;
22 import javax.inject.Inject;
23 import javax.inject.Named;
24 import javax.inject.Singleton;
25 import org.apache.maven.model.Activation;
26 import org.apache.maven.model.ActivationFile;
27 import org.apache.maven.model.Profile;
28 import org.apache.maven.model.building.ModelProblem.Severity;
29 import org.apache.maven.model.building.ModelProblem.Version;
30 import org.apache.maven.model.building.ModelProblemCollector;
31 import org.apache.maven.model.building.ModelProblemCollectorRequest;
32 import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
33 import org.apache.maven.model.profile.ProfileActivationContext;
34 import org.codehaus.plexus.interpolation.InterpolationException;
35 import org.codehaus.plexus.util.StringUtils;
36
37
38
39
40
41
42
43
44
45
46
47
48
49 @Named("file")
50 @Singleton
51 public class FileProfileActivator implements ProfileActivator {
52
53 private final ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
54
55 @Inject
56 public FileProfileActivator(ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator) {
57 this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
58 }
59
60 @Override
61 public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
62 Activation activation = profile.getActivation();
63
64 if (activation == null) {
65 return false;
66 }
67
68 ActivationFile file = activation.getFile();
69
70 if (file == null) {
71 return false;
72 }
73
74 String path;
75 boolean missing;
76
77 if (StringUtils.isNotEmpty(file.getExists())) {
78 path = file.getExists();
79 missing = false;
80 } else if (StringUtils.isNotEmpty(file.getMissing())) {
81 path = file.getMissing();
82 missing = true;
83 } else {
84 return false;
85 }
86
87 try {
88 path = profileActivationFilePathInterpolator.interpolate(path, context);
89 } catch (InterpolationException e) {
90 problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
91 .setMessage("Failed to interpolate file location " + path + " for profile " + profile.getId() + ": "
92 + e.getMessage())
93 .setLocation(file.getLocation(missing ? "missing" : "exists"))
94 .setException(e));
95 return false;
96 }
97
98 if (path == null) {
99 return false;
100 }
101
102 File f = new File(path);
103
104 if (!f.isAbsolute()) {
105 return false;
106 }
107
108 boolean fileExists = f.exists();
109
110 return missing != fileExists;
111 }
112
113 @Override
114 public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
115 Activation activation = profile.getActivation();
116
117 if (activation == null) {
118 return false;
119 }
120
121 ActivationFile file = activation.getFile();
122
123 return file != null;
124 }
125 }