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