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