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      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 (fileString != null && !fileString.isEmpty()) {
64                      fileString = interpolator.interpolate(fileString, "").replace("\\", "/");
65                      File file = new File(fileString);
66                      return file.exists();
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 (fileString != null && !fileString.isEmpty()) {
73                      fileString = interpolator.interpolate(fileString, "").replace("\\", "/");
74                      File file = new File(fileString);
75                      return !file.exists();
76                  }
77              } catch (InterpolationException e) {
78                  if (logger.isDebugEnabled()) {
79                      logger.debug("Failed to interpolate missing file location for profile activator: " + fileString, e);
80                  } else {
81                      logger.warn("Failed to interpolate missing file location for profile activator: " + fileString
82                              + ", enable verbose output (-X) for more details");
83                  }
84              }
85          }
86  
87          return false;
88      }
89  
90      public void enableLogging(Logger logger) {
91          this.logger = logger;
92      }
93  }