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