1 package org.apache.maven.plugins.enforcer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.List;
23
24 import org.apache.maven.artifact.versioning.ArtifactVersion;
25 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
26 import org.apache.maven.artifact.versioning.Restriction;
27 import org.apache.maven.artifact.versioning.VersionRange;
28 import org.apache.maven.enforcer.rule.api.EnforcerRule;
29 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugin.logging.Log;
32 import org.codehaus.plexus.util.StringUtils;
33
34
35
36
37
38
39
40 public abstract class AbstractVersionEnforcer
41 extends AbstractStandardEnforcerRule
42 {
43
44
45
46
47
48
49
50
51
52
53
54 public String version = null;
55
56
57
58
59
60
61
62
63
64
65
66 public void enforceVersion( Log log, String variableName, String requiredVersionRange, ArtifactVersion actualVersion )
67 throws EnforcerRuleException
68 {
69 if ( StringUtils.isEmpty( requiredVersionRange ) )
70 {
71 throw new EnforcerRuleException( variableName + " version can't be empty." );
72 }
73 else
74 {
75
76 VersionRange vr;
77 String msg = "Detected " + variableName + " Version: " + actualVersion;
78
79
80 if ( actualVersion.toString().equals( requiredVersionRange ) )
81 {
82 log.debug( msg + " is allowed in the range " + requiredVersionRange + "." );
83 }
84 else
85 {
86 try
87 {
88 vr = VersionRange.createFromVersionSpec( requiredVersionRange );
89
90 if ( containsVersion( vr, actualVersion ) )
91 {
92 log.debug( msg + " is allowed in the range " + requiredVersionRange + "." );
93 }
94 else
95 {
96 if ( StringUtils.isEmpty( message ) )
97 {
98 message = msg + " is not in the allowed range " + vr + ".";
99 }
100
101 throw new EnforcerRuleException( message );
102 }
103 }
104 catch ( InvalidVersionSpecificationException e )
105 {
106 throw new EnforcerRuleException( "The requested " + variableName + " version " +
107 requiredVersionRange + " is invalid.", e );
108 }
109 }
110 }
111 }
112
113
114
115
116
117
118
119
120
121
122 public static boolean containsVersion( VersionRange allowedRange, ArtifactVersion theVersion )
123 {
124 boolean matched = false;
125 ArtifactVersion recommendedVersion = allowedRange.getRecommendedVersion();
126 if ( recommendedVersion == null )
127 {
128 @SuppressWarnings( "unchecked" )
129 List<Restriction> restrictions = allowedRange.getRestrictions();
130 for ( Restriction restriction : restrictions )
131 {
132 if ( restriction.containsVersion( theVersion ) )
133 {
134 matched = true;
135 break;
136 }
137 }
138 }
139 else
140 {
141
142 @SuppressWarnings( "unchecked" )
143 int compareTo = recommendedVersion.compareTo( theVersion );
144 matched = ( compareTo <= 0 );
145 }
146 return matched;
147 }
148
149
150
151
152
153
154 public String getCacheId()
155 {
156 if ( StringUtils.isNotEmpty( version ) )
157 {
158
159 return "" + version.hashCode();
160 }
161 else
162 {
163 return "0";
164 }
165
166 }
167
168
169
170
171
172
173 public boolean isCacheable()
174 {
175
176 return true;
177 }
178
179
180
181
182
183
184 public boolean isResultValid( EnforcerRule theCachedRule )
185 {
186
187
188 return true;
189 }
190
191
192
193
194
195
196 public String getVersion()
197 {
198 return this.version;
199 }
200
201
202
203
204
205
206 public void setVersion( String theVersion )
207 {
208 this.version = theVersion;
209 }
210
211 }