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 javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  
27  import org.apache.maven.model.Activation;
28  import org.apache.maven.model.ActivationFile;
29  import org.apache.maven.model.Profile;
30  import org.apache.maven.model.building.ModelProblem.Severity;
31  import org.apache.maven.model.building.ModelProblem.Version;
32  import org.apache.maven.model.building.ModelProblemCollector;
33  import org.apache.maven.model.building.ModelProblemCollectorRequest;
34  import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
35  import org.apache.maven.model.profile.ProfileActivationContext;
36  import org.codehaus.plexus.interpolation.InterpolationException;
37  import org.codehaus.plexus.util.StringUtils;
38  
39  /**
40   * Determines profile activation based on the existence/absence of some file.
41   * File name interpolation support is limited to <code>${project.basedir}</code>
42   * system properties and user properties.
43   *
44   * @author Benjamin Bentmann
45   * @see ActivationFile
46   * @see org.apache.maven.model.validation.DefaultModelValidator#validateRawModel
47   */
48  @Named("file")
49  @Singleton
50  public class FileProfileActivator implements ProfileActivator {
51  
52      @Inject
53      private ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
54  
55      public FileProfileActivator setProfileActivationFilePathInterpolator(
56              ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator) {
57          this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
58          return this;
59      }
60  
61      @Override
62      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
63          Activation activation = profile.getActivation();
64  
65          if (activation == null) {
66              return false;
67          }
68  
69          ActivationFile file = activation.getFile();
70  
71          if (file == null) {
72              return false;
73          }
74  
75          String path;
76          boolean missing;
77  
78          if (StringUtils.isNotEmpty(file.getExists())) {
79              path = file.getExists();
80              missing = false;
81          } else if (StringUtils.isNotEmpty(file.getMissing())) {
82              path = file.getMissing();
83              missing = true;
84          } else {
85              return false;
86          }
87  
88          try {
89              path = profileActivationFilePathInterpolator.interpolate(path, context);
90          } catch (InterpolationException e) {
91              problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
92                      .setMessage("Failed to interpolate file location " + path + " for profile " + profile.getId() + ": "
93                              + e.getMessage())
94                      .setLocation(file.getLocation(missing ? "missing" : "exists"))
95                      .setException(e));
96              return false;
97          }
98  
99          if (path == null) {
100             return false;
101         }
102 
103         File f = new File(path);
104 
105         if (!f.isAbsolute()) {
106             return false;
107         }
108 
109         boolean fileExists = f.exists();
110 
111         return missing ? !fileExists : fileExists;
112     }
113 
114     @Override
115     public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
116         Activation activation = profile.getActivation();
117 
118         if (activation == null) {
119             return false;
120         }
121 
122         ActivationFile file = activation.getFile();
123 
124         if (file == null) {
125             return false;
126         }
127         return true;
128     }
129 }