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 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 RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
101
102 final File basedir = context.getProjectDirectory();
103
104 if ( basedir != null )
105 {
106 interpolator.addValueSource( new AbstractValueSource( false )
107 {
108 public Object getValue( String expression )
109 {
110
111
112
113
114 if ( "basedir".equals( expression ) )
115 {
116 return basedir.getAbsolutePath();
117 }
118 return null;
119 }
120 } );
121 }
122 else if ( path.contains( "${basedir}" ) )
123 {
124 return false;
125 }
126
127 interpolator.addValueSource( new MapBasedValueSource( context.getProjectProperties() ) );
128
129 interpolator.addValueSource( new MapBasedValueSource( context.getUserProperties() ) );
130
131 interpolator.addValueSource( new MapBasedValueSource( context.getSystemProperties() ) );
132
133 try
134 {
135 path = interpolator.interpolate( path, "" );
136 }
137 catch ( Exception e )
138 {
139 problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
140 .setMessage( "Failed to interpolate file location " + path + " for profile " + profile.getId()
141 + ": " + e.getMessage() )
142 .setLocation( file.getLocation( missing ? "missing" : "exists" ) )
143 .setException( e ) );
144 return false;
145 }
146
147 path = pathTranslator.alignToBaseDirectory( path, basedir );
148
149
150 if ( missing )
151 {
152 file.setMissing( path );
153 }
154 else
155 {
156 file.setExists( path );
157 }
158
159 File f = new File( path );
160
161 if ( !f.isAbsolute() )
162 {
163 return false;
164 }
165
166 boolean fileExists = f.exists();
167
168 return missing ? !fileExists : fileExists;
169 }
170
171 @Override
172 public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
173 {
174 Activation activation = profile.getActivation();
175
176 if ( activation == null )
177 {
178 return false;
179 }
180
181 ActivationFile file = activation.getFile();
182
183 if ( file == null )
184 {
185 return false;
186 }
187 return true;
188 }
189
190 }