View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.profile.activation;
20  
21  import java.io.File;
22  import javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  import org.apache.maven.model.Activation;
26  import org.apache.maven.model.ActivationFile;
27  import org.apache.maven.model.Profile;
28  import org.apache.maven.model.building.ModelProblem.Severity;
29  import org.apache.maven.model.building.ModelProblem.Version;
30  import org.apache.maven.model.building.ModelProblemCollector;
31  import org.apache.maven.model.building.ModelProblemCollectorRequest;
32  import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
33  import org.apache.maven.model.profile.ProfileActivationContext;
34  import org.codehaus.plexus.interpolation.InterpolationException;
35  import org.codehaus.plexus.util.StringUtils;
36  
37  /**
38   * Determines profile activation based on the existence/absence of some file.
39   * File name interpolation support is limited to <code>${basedir}</code> (since Maven 3,
40   * see <a href="https://issues.apache.org/jira/browse/MNG-2363">MNG-2363</a>),
41   * system properties and user properties.
42   * <code>${project.basedir}</code> is intentionally not supported as this form would suggest that other
43   * <code>${project.*}</code> expressions can be used, which is however beyond the design.
44   *
45   * @author Benjamin Bentmann
46   * @see ActivationFile
47   * @see org.apache.maven.model.validation.DefaultModelValidator#validateRawModel
48   */
49  @Named("file")
50  @Singleton
51  public class FileProfileActivator implements ProfileActivator {
52  
53      private final ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
54  
55      @Inject
56      public FileProfileActivator(ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator) {
57          this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
58      }
59  
60      @Override
61      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
62          Activation activation = profile.getActivation();
63  
64          if (activation == null) {
65              return false;
66          }
67  
68          ActivationFile file = activation.getFile();
69  
70          if (file == null) {
71              return false;
72          }
73  
74          String path;
75          boolean missing;
76  
77          if (StringUtils.isNotEmpty(file.getExists())) {
78              path = file.getExists();
79              missing = false;
80          } else if (StringUtils.isNotEmpty(file.getMissing())) {
81              path = file.getMissing();
82              missing = true;
83          } else {
84              return false;
85          }
86  
87          try {
88              path = profileActivationFilePathInterpolator.interpolate(path, context);
89          } catch (InterpolationException e) {
90              problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
91                      .setMessage("Failed to interpolate file location " + path + " for profile " + profile.getId() + ": "
92                              + e.getMessage())
93                      .setLocation(file.getLocation(missing ? "missing" : "exists"))
94                      .setException(e));
95              return false;
96          }
97  
98          if (path == null) {
99              return false;
100         }
101 
102         File f = new File(path);
103 
104         if (!f.isAbsolute()) {
105             return false;
106         }
107 
108         boolean fileExists = f.exists();
109 
110         return missing != fileExists;
111     }
112 
113     @Override
114     public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
115         Activation activation = profile.getActivation();
116 
117         if (activation == null) {
118             return false;
119         }
120 
121         ActivationFile file = activation.getFile();
122 
123         return file != null;
124     }
125 }