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