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      @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 }