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 javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.inject.Singleton;
27
28 import org.apache.maven.model.Activation;
29 import org.apache.maven.model.ActivationFile;
30 import org.apache.maven.model.Profile;
31 import org.apache.maven.model.building.ModelProblem.Severity;
32 import org.apache.maven.model.building.ModelProblem.Version;
33 import org.apache.maven.model.building.ModelProblemCollector;
34 import org.apache.maven.model.building.ModelProblemCollectorRequest;
35 import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
36 import org.apache.maven.model.profile.ProfileActivationContext;
37 import org.codehaus.plexus.interpolation.InterpolationException;
38 import org.codehaus.plexus.util.StringUtils;
39
40
41
42
43
44
45
46
47
48
49 @Named( "file" )
50 @Singleton
51 public class FileProfileActivator
52 implements ProfileActivator
53 {
54
55 @Inject
56 private ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
57
58 public FileProfileActivator setProfileActivationFilePathInterpolator(
59 ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator )
60 {
61 this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
62 return this;
63 }
64
65 @Override
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 try
101 {
102 path = profileActivationFilePathInterpolator.interpolate( path, context );
103 }
104 catch ( InterpolationException e )
105 {
106 problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
107 .setMessage( "Failed to interpolate file location " + path + " for profile " + profile.getId()
108 + ": " + e.getMessage() )
109 .setLocation( file.getLocation( missing ? "missing" : "exists" ) )
110 .setException( e ) );
111 return false;
112 }
113
114 if ( path == null )
115 {
116 return false;
117 }
118
119 File f = new File( path );
120
121 if ( !f.isAbsolute() )
122 {
123 return false;
124 }
125
126 boolean fileExists = f.exists();
127
128 return missing ? !fileExists : fileExists;
129 }
130
131 @Override
132 public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
133 {
134 Activation activation = profile.getActivation();
135
136 if ( activation == null )
137 {
138 return false;
139 }
140
141 ActivationFile file = activation.getFile();
142
143 if ( file == null )
144 {
145 return false;
146 }
147 return true;
148 }
149
150 }