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.building.ModelProblem.Version;
30 import org.apache.maven.model.building.ModelProblemCollectorRequest;
31 import org.apache.maven.model.path.PathTranslator;
32 import org.apache.maven.model.profile.ProfileActivationContext;
33 import org.codehaus.plexus.component.annotations.Component;
34 import org.codehaus.plexus.component.annotations.Requirement;
35 import org.codehaus.plexus.interpolation.AbstractValueSource;
36 import org.codehaus.plexus.interpolation.MapBasedValueSource;
37 import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
38 import org.codehaus.plexus.util.StringUtils;
39
40
41
42
43
44
45
46
47
48
49
50
51
52 @Component( role = ProfileActivator.class, hint = "file" )
53 public class FileProfileActivator
54 implements ProfileActivator
55 {
56
57 @Requirement
58 private PathTranslator pathTranslator;
59
60 public FileProfileActivator setPathTranslator( PathTranslator pathTranslator )
61 {
62 this.pathTranslator = pathTranslator;
63 return this;
64 }
65
66 @Override
67 public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
68 {
69 Activation activation = profile.getActivation();
70
71 if ( activation == null )
72 {
73 return false;
74 }
75
76 ActivationFile file = activation.getFile();
77
78 if ( file == null )
79 {
80 return false;
81 }
82
83 String path;
84 boolean missing;
85
86 if ( StringUtils.isNotEmpty( file.getExists() ) )
87 {
88 path = file.getExists();
89 missing = false;
90 }
91 else if ( StringUtils.isNotEmpty( file.getMissing() ) )
92 {
93 path = file.getMissing();
94 missing = true;
95 }
96 else
97 {
98 return false;
99 }
100
101 RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
102
103 final File basedir = context.getProjectDirectory();
104
105 if ( basedir != null )
106 {
107 interpolator.addValueSource( new AbstractValueSource( false )
108 {
109 @Override
110 public Object getValue( String expression )
111 {
112
113
114
115
116 if ( "basedir".equals( expression ) )
117 {
118 return basedir.getAbsolutePath();
119 }
120 return null;
121 }
122 } );
123 }
124 else if ( path.contains( "${basedir}" ) )
125 {
126 return false;
127 }
128
129 interpolator.addValueSource( new MapBasedValueSource( context.getProjectProperties() ) );
130
131 interpolator.addValueSource( new MapBasedValueSource( context.getUserProperties() ) );
132
133 interpolator.addValueSource( new MapBasedValueSource( context.getSystemProperties() ) );
134
135 try
136 {
137 path = interpolator.interpolate( path, "" );
138 }
139 catch ( Exception e )
140 {
141 problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
142 .setMessage( "Failed to interpolate file location " + path + " for profile " + profile.getId()
143 + ": " + e.getMessage() )
144 .setLocation( file.getLocation( missing ? "missing" : "exists" ) )
145 .setException( e ) );
146 return false;
147 }
148
149 path = pathTranslator.alignToBaseDirectory( path, basedir );
150
151
152 if ( missing )
153 {
154 file.setMissing( path );
155 }
156 else
157 {
158 file.setExists( path );
159 }
160
161 File f = new File( path );
162
163 if ( !f.isAbsolute() )
164 {
165 return false;
166 }
167
168 boolean fileExists = f.exists();
169
170 return missing ? !fileExists : fileExists;
171 }
172
173 @Override
174 public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
175 {
176 Activation activation = profile.getActivation();
177
178 if ( activation == null )
179 {
180 return false;
181 }
182
183 ActivationFile file = activation.getFile();
184
185 if ( file == null )
186 {
187 return false;
188 }
189 return true;
190 }
191
192 }