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.util.FileUtils;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  public class FileProfileActivator
35      extends DetectedProfileActivator
36  {
37      protected boolean canDetectActivation( Profile profile )
38      {
39          return profile.getActivation() != null && profile.getActivation().getFile() != null;
40      }
41  
42      public boolean isActive( Profile profile )
43      {
44          Activation activation = profile.getActivation();
45  
46          ActivationFile actFile = activation.getFile();
47  
48          if ( actFile != null )
49          {
50              // check if the file exists, if it does then the profile will be active
51              String fileString = actFile.getExists();
52  
53              RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
54              try
55              {
56                  interpolator.addValueSource( new EnvarBasedValueSource() );
57              }
58              catch ( IOException e )
59              {
60                  // ignored
61              }
62              interpolator.addValueSource( new MapBasedValueSource( System.getProperties() ) );
63  
64              if ( StringUtils.isNotEmpty( fileString ) )
65              {
66                  try
67                  {
68                      fileString = interpolator.interpolate( fileString, "" );
69                  }
70                  catch ( InterpolationException e )
71                  {
72                      getLogger().debug( "Failed to interpolate path in file profile activator: " + fileString, e );
73                  }
74                  
75                  fileString = StringUtils.replace( fileString, "\\", "/" );
76                  return FileUtils.fileExists( fileString );
77              }
78  
79              // check if the file is missing, if it is then the profile will be active
80              fileString = actFile.getMissing();
81  
82              if ( StringUtils.isNotEmpty( fileString ) )
83              {
84                  try
85                  {
86                      fileString = interpolator.interpolate( fileString, "" );
87                  }
88                  catch ( InterpolationException e )
89                  {
90                      getLogger().debug( "Failed to interpolate path in file profile activator: " + fileString, e );
91                  }
92                  
93                  fileString = StringUtils.replace( fileString, "\\", "/" );
94                  return !FileUtils.fileExists( fileString );
95              }
96          }
97  
98          return false;
99      }
100 }