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