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      private final ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
56  
57      @Inject
58      public FileProfileActivator(ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator) {
59          this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
60      }
61  
62      @Override
63      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
64          Activation activation = profile.getActivation();
65  
66          if (activation == null) {
67              return false;
68          }
69  
70          ActivationFile file = activation.getFile();
71  
72          if (file == null) {
73              return false;
74          }
75  
76          String path;
77          boolean missing;
78  
79          if (StringUtils.isNotEmpty(file.getExists())) {
80              path = file.getExists();
81              missing = false;
82          } else if (StringUtils.isNotEmpty(file.getMissing())) {
83              path = file.getMissing();
84              missing = true;
85          } else {
86              return false;
87          }
88  
89          try {
90              path = profileActivationFilePathInterpolator.interpolate(path, context);
91          } catch (InterpolationException e) {
92              problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
93                      .setMessage("Failed to interpolate file location " + path + " for profile " + profile.getId() + ": "
94                              + e.getMessage())
95                      .setLocation(file.getLocation(missing ? "missing" : "exists"))
96                      .setException(e));
97              return false;
98          }
99  
100         if (path == null) {
101             return false;
102         }
103 
104         File f = new File(path);
105 
106         if (!f.isAbsolute()) {
107             return false;
108         }
109 
110         boolean fileExists = f.exists();
111 
112         return missing != fileExists;
113     }
114 
115     @Override
116     public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
117         Activation activation = profile.getActivation();
118 
119         if (activation == null) {
120             return false;
121         }
122 
123         ActivationFile file = activation.getFile();
124 
125         return file != null;
126     }
127 }