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