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