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.internal.impl.model.profile;
20  
21  import java.io.File;
22  
23  import org.apache.maven.api.di.Inject;
24  import org.apache.maven.api.di.Named;
25  import org.apache.maven.api.di.Singleton;
26  import org.apache.maven.api.model.Activation;
27  import org.apache.maven.api.model.ActivationFile;
28  import org.apache.maven.api.model.Profile;
29  import org.apache.maven.api.services.BuilderProblem;
30  import org.apache.maven.api.services.InterpolatorException;
31  import org.apache.maven.api.services.ModelProblem;
32  import org.apache.maven.api.services.ModelProblemCollector;
33  import org.apache.maven.api.services.model.ProfileActivationContext;
34  import org.apache.maven.api.services.model.ProfileActivator;
35  import org.apache.maven.internal.impl.model.ProfileActivationFilePathInterpolator;
36  
37  /**
38   * Determines profile activation based on the existence/absence of some file.
39   * File name interpolation support is limited to <code>${project.basedir}</code>
40   * system properties and user properties.
41   *
42   * @see ActivationFile
43   * @see org.apache.maven.internal.impl.model.DefaultModelValidator#validateRawModel
44   */
45  @Named("file")
46  @Singleton
47  public class FileProfileActivator implements ProfileActivator {
48  
49      private final ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
50  
51      @Inject
52      public FileProfileActivator(ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator) {
53          this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
54      }
55  
56      @Override
57      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
58          Activation activation = profile.getActivation();
59  
60          if (activation == null) {
61              return false;
62          }
63  
64          ActivationFile file = activation.getFile();
65  
66          if (file == null) {
67              return false;
68          }
69  
70          String path;
71          boolean missing;
72  
73          boolean hasExists = file.getExists() != null && !file.getExists().isEmpty();
74          boolean hasMissing = file.getMissing() != null && !file.getMissing().isEmpty();
75          if (hasExists) {
76              if (hasMissing) {
77                  problems.add(
78                          BuilderProblem.Severity.WARNING,
79                          ModelProblem.Version.BASE,
80                          String.format(
81                                  "Profile '%s' file activation conflict: Both 'missing' (%s) and 'exists' assertions are defined. "
82                                          + "The 'missing' assertion will be ignored. Please remove one assertion to resolve this conflict.",
83                                  profile.getId(), file.getMissing()),
84                          file.getLocation("missing"));
85              }
86              path = file.getExists();
87              missing = false;
88          } else if (hasMissing) {
89              path = file.getMissing();
90              missing = true;
91          } else {
92              return false;
93          }
94  
95          try {
96              path = profileActivationFilePathInterpolator.interpolate(path, context);
97          } catch (InterpolatorException e) {
98              problems.add(
99                      BuilderProblem.Severity.ERROR,
100                     ModelProblem.Version.BASE,
101                     "Failed to interpolate file location " + path + " for profile " + profile.getId() + ": "
102                             + e.getMessage(),
103                     file.getLocation(missing ? "missing" : "exists"),
104                     e);
105             return false;
106         }
107 
108         if (path == null) {
109             return false;
110         }
111 
112         File f = new File(path);
113 
114         if (!f.isAbsolute()) {
115             return false;
116         }
117 
118         boolean fileExists = f.exists();
119 
120         return missing != fileExists;
121     }
122 
123     @Override
124     public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
125         Activation activation = profile.getActivation();
126 
127         if (activation == null) {
128             return false;
129         }
130 
131         ActivationFile file = activation.getFile();
132 
133         return file != null;
134     }
135 }