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.ModelProblem;
31  import org.apache.maven.api.services.ModelProblemCollector;
32  import org.apache.maven.api.services.model.ProfileActivationContext;
33  import org.apache.maven.api.services.model.ProfileActivator;
34  import org.apache.maven.internal.impl.model.ProfileActivationFilePathInterpolator;
35  import org.codehaus.plexus.interpolation.InterpolationException;
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          if (file.getExists() != null && !file.getExists().isEmpty()) {
74              path = file.getExists();
75              missing = false;
76          } else if (file.getMissing() != null && !file.getMissing().isEmpty()) {
77              path = file.getMissing();
78              missing = true;
79          } else {
80              return false;
81          }
82  
83          try {
84              path = profileActivationFilePathInterpolator.interpolate(path, context);
85          } catch (InterpolationException e) {
86              problems.add(
87                      BuilderProblem.Severity.ERROR,
88                      ModelProblem.Version.BASE,
89                      "Failed to interpolate file location " + path + " for profile " + profile.getId() + ": "
90                              + e.getMessage(),
91                      file.getLocation(missing ? "missing" : "exists"),
92                      e);
93              return false;
94          }
95  
96          if (path == null) {
97              return false;
98          }
99  
100         File f = new File(path);
101 
102         if (!f.isAbsolute()) {
103             return false;
104         }
105 
106         boolean fileExists = f.exists();
107 
108         return missing != fileExists;
109     }
110 
111     @Override
112     public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
113         Activation activation = profile.getActivation();
114 
115         if (activation == null) {
116             return false;
117         }
118 
119         ActivationFile file = activation.getFile();
120 
121         return file != null;
122     }
123 }