View Javadoc
1   package org.apache.maven.model.profile.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.File;
23  
24  import javax.inject.Inject;
25  import javax.inject.Named;
26  import javax.inject.Singleton;
27  
28  import org.apache.maven.model.Activation;
29  import org.apache.maven.model.ActivationFile;
30  import org.apache.maven.model.Profile;
31  import org.apache.maven.model.building.ModelProblem.Severity;
32  import org.apache.maven.model.building.ModelProblem.Version;
33  import org.apache.maven.model.building.ModelProblemCollector;
34  import org.apache.maven.model.building.ModelProblemCollectorRequest;
35  import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
36  import org.apache.maven.model.profile.ProfileActivationContext;
37  import org.codehaus.plexus.interpolation.InterpolationException;
38  import org.codehaus.plexus.util.StringUtils;
39  
40  /**
41   * Determines profile activation based on the existence/absence of some file.
42   * File name interpolation support is limited to <code>${project.basedir}</code>,
43   * system properties and user properties.
44   *
45   * @author Benjamin Bentmann
46   * @see ActivationFile
47   * @see org.apache.maven.model.validation.DefaultModelValidator#validateRawModel
48   */
49  @Named( "file" )
50  @Singleton
51  public class FileProfileActivator
52      implements ProfileActivator
53  {
54  
55      @Inject
56      private ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
57  
58      public FileProfileActivator setProfileActivationFilePathInterpolator(
59              ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator )
60      {
61          this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
62          return this;
63      }
64  
65      @Override
66      public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
67      {
68          Activation activation = profile.getActivation();
69  
70          if ( activation == null )
71          {
72              return false;
73          }
74  
75          ActivationFile file = activation.getFile();
76  
77          if ( file == null )
78          {
79              return false;
80          }
81  
82          String path;
83          boolean missing;
84  
85          if ( StringUtils.isNotEmpty( file.getExists() ) )
86          {
87              path = file.getExists();
88              missing = false;
89          }
90          else if ( StringUtils.isNotEmpty( file.getMissing() ) )
91          {
92              path = file.getMissing();
93              missing = true;
94          }
95          else
96          {
97              return false;
98          }
99  
100         try
101         {
102             path = profileActivationFilePathInterpolator.interpolate( path, context );
103         }
104         catch ( InterpolationException e )
105         {
106             problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
107                     .setMessage( "Failed to interpolate file location " + path + " for profile " + profile.getId()
108                             + ": " + e.getMessage() )
109                     .setLocation( file.getLocation( missing ? "missing" : "exists" ) )
110                     .setException( e ) );
111             return false;
112         }
113 
114         if ( path == null )
115         {
116             return false;
117         }
118 
119         File f = new File( path );
120 
121         if ( !f.isAbsolute() )
122         {
123             return false;
124         }
125 
126         boolean fileExists = f.exists();
127 
128         return missing ? !fileExists : fileExists;
129     }
130 
131     @Override
132     public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
133     {
134         Activation activation = profile.getActivation();
135 
136         if ( activation == null )
137         {
138             return false;
139         }
140 
141         ActivationFile file = activation.getFile();
142 
143         if ( file == null )
144         {
145             return false;
146         }
147         return true;
148     }
149 
150 }