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