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