View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Contains the common code to compare a version against a version range.
35   *
36   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
37   */
38  public abstract class AbstractVersionEnforcer
39      extends AbstractStandardEnforcerRule
40  {
41  
42      /**
43       * Specify the required version. Some examples are:
44       * <ul>
45       * <li><code>2.0.4</code> Version 2.0.4 and higher (different from Maven meaning)</li>
46       * <li><code>[2.0,2.1)</code> Versions 2.0 (included) to 2.1 (not included)</li>
47       * <li><code>[2.0,2.1]</code> Versions 2.0 to 2.1 (both included)</li>
48       * <li><code>[2.0.5,)</code> Versions 2.0.5 and higher</li>
49       * <li><code>(,2.0.5],[2.1.1,)</code> Versions up to 2.0.5 (included) and 2.1.1 or higher</li>
50       * </ul>
51       * 
52       * @see {@link #setVersion(String)}
53       * @see {@link #getVersion()}
54       */
55      private String version;
56  
57      /**
58       * Compares the specified version to see if it is allowed by the defined version range.
59       *
60       * @param log the log
61       * @param variableName name of variable to use in messages (Example: "Maven" or "Java" etc).
62       * @param requiredVersionRange range of allowed versions.
63       * @param actualVersion the version to be checked.
64       * @throws EnforcerRuleException the enforcer rule exception
65       */
66      // CHECKSTYLE_OFF: LineLength
67      public void enforceVersion( Log log, String variableName, String requiredVersionRange, ArtifactVersion actualVersion )
68          throws EnforcerRuleException
69      // CHECKSTYLE_ON: LineLength
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              // short circuit check if the strings are exactly equal
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      * Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
119      * containsVersion method assumes a singular version means allow everything. This method assumes that "2.0.4" ==
120      * "[2.0.4,)"
121      *
122      * @param allowedRange range of allowed versions.
123      * @param theVersion the version to be checked.
124      * @return true if the version is contained by the range.
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             // only singular versions ever have a recommendedVersion
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             // return the hashcodes of the parameter that matters
158             return "" + version.hashCode();
159         }
160         else
161         {
162             return "0";
163         }
164 
165     }
166 
167     @Override
168     public boolean isCacheable()
169     {
170         // the maven version is not going to change between projects in the same build.
171         return true;
172     }
173 
174     @Override
175     public boolean isResultValid( EnforcerRule theCachedRule )
176     {
177         // i will always return the hash of the parameters as my id. If my parameters are the same, this
178         // rule must always have the same result.
179         return true;
180     }
181 
182     /**
183      * Gets the required version.
184      *
185      * @return the required version
186      */
187     public final String getVersion()
188     {
189         return this.version;
190     }
191 
192     /**
193      * Specify the required version. Some examples are:
194      * <ul>
195      * <li><code>2.0.4</code> Version 2.0.4 and higher (different from Maven meaning)</li>
196      * <li><code>[2.0,2.1)</code> Versions 2.0 (included) to 2.1 (not included)</li>
197      * <li><code>[2.0,2.1]</code> Versions 2.0 to 2.1 (both included)</li>
198      * <li><code>[2.0.5,)</code> Versions 2.0.5 and higher</li>
199      * <li><code>(,2.0.5],[2.1.1,)</code> Versions up to 2.0.5 (included) and 2.1.1 or higher</li>
200      * </ul>
201      *
202      * @param theVersion the required version to set
203      */
204     public final void setVersion( String theVersion )
205     {
206         this.version = theVersion;
207     }
208 
209 }