View Javadoc
1   package org.apache.maven.profiles.activation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
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  import org.codehaus.plexus.util.FileUtils;
34  import org.codehaus.plexus.util.StringUtils;
35  
36  /**
37   * FileProfileActivator
38   */
39  @Deprecated
40  public class FileProfileActivator
41      extends DetectedProfileActivator
42      implements LogEnabled
43  {
44      private Logger logger;
45  
46      protected boolean canDetectActivation( Profile profile )
47      {
48          return profile.getActivation() != null && profile.getActivation().getFile() != null;
49      }
50  
51      public boolean isActive( Profile profile )
52      {
53          Activation activation = profile.getActivation();
54  
55          ActivationFile actFile = activation.getFile();
56  
57          if ( actFile != null )
58          {
59              // check if the file exists, if it does then the profile will be active
60              String fileString = actFile.getExists();
61  
62              RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
63              try
64              {
65                  interpolator.addValueSource( new EnvarBasedValueSource() );
66              }
67              catch ( IOException e )
68              {
69                  // ignored
70              }
71              interpolator.addValueSource( new MapBasedValueSource( System.getProperties() ) );
72  
73              try
74              {
75                  if ( StringUtils.isNotEmpty( fileString ) )
76                  {
77                      fileString = StringUtils.replace( interpolator.interpolate( fileString, "" ), "\\", "/" );
78                      return FileUtils.fileExists( fileString );
79                  }
80  
81                  // check if the file is missing, if it is then the profile will be active
82                  fileString = actFile.getMissing();
83  
84                  if ( StringUtils.isNotEmpty( fileString ) )
85                  {
86                      fileString = StringUtils.replace( interpolator.interpolate( fileString, "" ), "\\", "/" );
87                      return !FileUtils.fileExists( fileString );
88                  }
89              }
90              catch ( InterpolationException e )
91              {
92                  if ( logger.isDebugEnabled() )
93                  {
94                      logger.debug( "Failed to interpolate missing file location for profile activator: " + fileString,
95                                    e );
96                  }
97                  else
98                  {
99                      logger.warn( "Failed to interpolate missing file location for profile activator: " + fileString
100                         + ". Run in debug mode (-X) for more information." );
101                 }
102             }
103         }
104 
105         return false;
106     }
107 
108     public void enableLogging( Logger logger )
109     {
110         this.logger = logger;
111     }
112 }