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.profiles.activation;
20  
21  import java.io.IOException;
22  import org.apache.maven.model.Activation;
23  import org.apache.maven.model.ActivationFile;
24  import org.apache.maven.model.Profile;
25  import org.codehaus.plexus.interpolation.EnvarBasedValueSource;
26  import org.codehaus.plexus.interpolation.InterpolationException;
27  import org.codehaus.plexus.interpolation.MapBasedValueSource;
28  import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
29  import org.codehaus.plexus.logging.LogEnabled;
30  import org.codehaus.plexus.logging.Logger;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  /**
35   * FileProfileActivator
36   */
37  @Deprecated
38  public class FileProfileActivator extends DetectedProfileActivator implements LogEnabled {
39      private Logger logger;
40  
41      protected boolean canDetectActivation(Profile profile) {
42          return profile.getActivation() != null && profile.getActivation().getFile() != null;
43      }
44  
45      public boolean isActive(Profile profile) {
46          Activation activation = profile.getActivation();
47  
48          ActivationFile actFile = activation.getFile();
49  
50          if (actFile != null) {
51              // check if the file exists, if it does then the profile will be active
52              String fileString = actFile.getExists();
53  
54              RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
55              try {
56                  interpolator.addValueSource(new EnvarBasedValueSource());
57              } catch (IOException e) {
58                  // ignored
59              }
60              interpolator.addValueSource(new MapBasedValueSource(System.getProperties()));
61  
62              try {
63                  if (StringUtils.isNotEmpty(fileString)) {
64                      fileString = StringUtils.replace(interpolator.interpolate(fileString, ""), "\\", "/");
65                      return FileUtils.fileExists(fileString);
66                  }
67  
68                  // check if the file is missing, if it is then the profile will be active
69                  fileString = actFile.getMissing();
70  
71                  if (StringUtils.isNotEmpty(fileString)) {
72                      fileString = StringUtils.replace(interpolator.interpolate(fileString, ""), "\\", "/");
73                      return !FileUtils.fileExists(fileString);
74                  }
75              } catch (InterpolationException e) {
76                  if (logger.isDebugEnabled()) {
77                      logger.debug("Failed to interpolate missing file location for profile activator: " + fileString, e);
78                  } else {
79                      logger.warn("Failed to interpolate missing file location for profile activator: " + fileString
80                              + ", enable verbose output (-X) for more details");
81                  }
82              }
83          }
84  
85          return false;
86      }
87  
88      public void enableLogging(Logger logger) {
89          this.logger = logger;
90      }
91  }