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