1 package org.apache.maven.model.profile.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.File;
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.apache.maven.model.building.ModelProblemCollector;
28 import org.apache.maven.model.building.ModelProblem.Severity;
29 import org.apache.maven.model.path.PathTranslator;
30 import org.apache.maven.model.profile.ProfileActivationContext;
31 import org.codehaus.plexus.component.annotations.Component;
32 import org.codehaus.plexus.component.annotations.Requirement;
33 import org.codehaus.plexus.interpolation.AbstractValueSource;
34 import org.codehaus.plexus.interpolation.MapBasedValueSource;
35 import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
36 import org.codehaus.plexus.util.StringUtils;
37
38
39
40
41
42
43 @Component( role = ProfileActivator.class, hint = "file" )
44 public class FileProfileActivator
45 implements ProfileActivator
46 {
47
48 @Requirement
49 private PathTranslator pathTranslator;
50
51 public FileProfileActivator setPathTranslator( PathTranslator pathTranslator )
52 {
53 this.pathTranslator = pathTranslator;
54 return this;
55 }
56
57 public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
58 {
59 Activation activation = profile.getActivation();
60
61 if ( activation == null )
62 {
63 return false;
64 }
65
66 ActivationFile file = activation.getFile();
67
68 if ( file == null )
69 {
70 return false;
71 }
72
73 String path;
74 boolean missing;
75
76 if ( StringUtils.isNotEmpty( file.getExists() ) )
77 {
78 path = file.getExists();
79 missing = false;
80 }
81 else if ( StringUtils.isNotEmpty( file.getMissing() ) )
82 {
83 path = file.getMissing();
84 missing = true;
85 }
86 else
87 {
88 return false;
89 }
90
91 RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
92
93 final File basedir = context.getProjectDirectory();
94
95 if ( basedir != null )
96 {
97 interpolator.addValueSource( new AbstractValueSource( false )
98 {
99 public Object getValue( String expression )
100 {
101
102
103
104
105 if ( "basedir".equals( expression ) )
106 {
107 return basedir.getAbsolutePath();
108 }
109 return null;
110 }
111 } );
112 }
113 else if ( path.indexOf( "${basedir}" ) >= 0 )
114 {
115 return false;
116 }
117
118 interpolator.addValueSource( new MapBasedValueSource( context.getUserProperties() ) );
119
120 interpolator.addValueSource( new MapBasedValueSource( context.getSystemProperties() ) );
121
122 try
123 {
124 path = interpolator.interpolate( path, "" );
125 }
126 catch ( Exception e )
127 {
128 problems.add( Severity.ERROR, "Failed to interpolate file location " + path + " for profile "
129 + profile.getId() + ": " + e.getMessage(), file.getLocation( missing ? "missing" : "exists" ), e );
130 return false;
131 }
132
133 path = pathTranslator.alignToBaseDirectory( path, basedir );
134
135 File f = new File( path );
136
137 if ( !f.isAbsolute() )
138 {
139 return false;
140 }
141
142 boolean fileExists = f.exists();
143
144 return missing ? !fileExists : fileExists;
145 }
146
147 }