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.MojoExecutionException;
31  import org.apache.maven.plugin.logging.Log;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  /**
35   * Contains the common code to compare a version against a version range.
36   *
37   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
38   * @version $Id: AbstractVersionEnforcer.java 1345332 2012-06-01 20:14:13Z rfscholte $
39   */
40  public abstract class AbstractVersionEnforcer
41      extends AbstractStandardEnforcerRule
42  {
43  
44      /**
45       * Specify the required version. Some examples are:
46       * <ul>
47       * <li><code>2.0.4</code> Version 2.0.4 and higher (different from Maven meaning)</li>
48       * <li><code>[2.0,2.1)</code> Versions 2.0 (included) to 2.1 (not included)</li>
49       * <li><code>[2.0,2.1]</code> Versions 2.0 to 2.1 (both included)</li>
50       * <li><code>[2.0.5,)</code> Versions 2.0.5 and higher</li>
51       * <li><code>(,2.0.5],[2.1.1,)</code> Versions up to 2.0.5 (included) and 2.1.1 or higher</li>
52       * </ul>
53       */
54      public String version = null;
55  
56      /**
57       * Compares the specified version to see if it is allowed by the defined version range.
58       *
59       * @param log the log
60       * @param variableName name of variable to use in messages (Example: "Maven" or "Java" etc).
61       * @param requiredVersionRange range of allowed versions.
62       * @param actualVersion the version to be checked.
63       * @throws MojoExecutionException if the version is not allowed.
64       * @throws EnforcerRuleException the enforcer rule exception
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              // short circuit check if the strings are exactly equal
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      * Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
115      * containsVersion method assumes a singular version means allow everything. This method assumes that "2.0.4" ==
116      * "[2.0.4,)"
117      *
118      * @param allowedRange range of allowed versions.
119      * @param theVersion the version to be checked.
120      * @return true if the version is contained by the range.
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             // only singular versions ever have a recommendedVersion
142             @SuppressWarnings( "unchecked" )
143             int compareTo = recommendedVersion.compareTo( theVersion );
144             matched = ( compareTo <= 0 );
145         }
146         return matched;
147     }
148 
149     /*
150      * (non-Javadoc)
151      *
152      * @see org.apache.maven.enforcer.rule.api.EnforcerRule#getCacheId()
153      */
154     public String getCacheId()
155     {
156         if ( StringUtils.isNotEmpty( version ) )
157         {
158             // return the hashcodes of the parameter that matters
159             return "" + version.hashCode();
160         }
161         else
162         {
163             return "0";
164         }
165 
166     }
167 
168     /*
169      * (non-Javadoc)
170      *
171      * @see org.apache.maven.enforcer.rule.api.EnforcerRule#isCacheable()
172      */
173     public boolean isCacheable()
174     {
175         // the maven version is not going to change between projects in the same build.
176         return true;
177     }
178 
179     /*
180      * (non-Javadoc)
181      *
182      * @see org.apache.maven.enforcer.rule.api.EnforcerRule#isResultValid(org.apache.maven.enforcer.rule.api.EnforcerRule)
183      */
184     public boolean isResultValid( EnforcerRule theCachedRule )
185     {
186         // i will always return the hash of the parameters as my id. If my parameters are the same, this
187         // rule must always have the same result.
188         return true;
189     }
190 
191     /**
192      * Gets the required version.
193      *
194      * @return the required version
195      */
196     public String getVersion()
197     {
198         return this.version;
199     }
200 
201     /**
202      * Sets the required version.
203      *
204      * @param theVersion the required version to set
205      */
206     public void setVersion( String theVersion )
207     {
208         this.version = theVersion;
209     }
210 
211 }