View Javadoc
1   package org.apache.maven.model.profile.activation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Determines profile activation based on the existence/absence of some file.
42   * File name interpolation support is limited to <code>${basedir}</code> (since Maven 3,
43   * see <a href="https://issues.apache.org/jira/browse/MNG-2363">MNG-2363</a>),
44   * System properties and request properties.
45   * <code>${project.basedir}</code> is intentionally not supported as this form would suggest that other
46   * <code>${project.*}</code> expressions can be used, which is however beyond the design.
47   *
48   * @author Benjamin Bentmann
49   * @see ActivationFile
50   * @see org.apache.maven.model.validation.DefaultModelValidator#validateRawModel
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 }