1 package org.apache.maven.profiles.activation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
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 }