View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules.version;
20  
21  import org.apache.maven.artifact.versioning.ArtifactVersion;
22  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
23  import org.apache.maven.artifact.versioning.VersionRange;
24  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
25  import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule;
26  
27  import static org.apache.maven.enforcer.rules.utils.ArtifactMatcher.containsVersion;
28  
29  /**
30   * Contains the common code to compare a version against a version range.
31   *
32   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
33   */
34  abstract class AbstractVersionEnforcer extends AbstractStandardEnforcerRule {
35  
36      /**
37       * Specify the required version. Some examples are:
38       * <ul>
39       * <li><code>2.0.4</code> Version 2.0.4 and higher (different from Maven meaning)</li>
40       * <li><code>[2.0,2.1)</code> Versions 2.0 (included) to 2.1 (not included)</li>
41       * <li><code>[2.0,2.1]</code> Versions 2.0 to 2.1 (both included)</li>
42       * <li><code>[2.0.5,)</code> Versions 2.0.5 and higher</li>
43       * <li><code>(,2.0.5],[2.1.1,)</code> Versions up to 2.0.5 (included) and 2.1.1 or higher</li>
44       * </ul>
45       *
46       * @see {@link #setVersion(String)}
47       * @see {@link #getVersion()}
48       */
49      private String version;
50  
51      /**
52       * Compares the specified version to see if it is allowed by the defined version range.
53       *
54       * @param variableName         name of variable to use in messages (Example: "Maven" or "Java" etc).
55       * @param requiredVersionRange range of allowed versions.
56       * @param actualVersion        the version to be checked.
57       * @throws EnforcerRuleException the enforcer rule exception
58       */
59      // CHECKSTYLE_OFF: LineLength
60      public void enforceVersion(String variableName, String requiredVersionRange, ArtifactVersion actualVersion)
61              throws EnforcerRuleException
62                  // CHECKSTYLE_ON: LineLength
63              {
64          if (requiredVersionRange == null || requiredVersionRange.isEmpty()) {
65              throw new EnforcerRuleException(variableName + " version can't be empty.");
66          } else {
67  
68              VersionRange vr;
69              String msg = "Detected " + variableName + " Version: " + actualVersion;
70  
71              // short circuit check if the strings are exactly equal
72              if (actualVersion.toString().equals(requiredVersionRange)) {
73                  getLog().debug(msg + " is allowed in the range " + requiredVersionRange + ".");
74              } else {
75                  try {
76                      vr = VersionRange.createFromVersionSpec(requiredVersionRange);
77  
78                      if (containsVersion(vr, actualVersion)) {
79                          getLog().debug(msg + " is allowed in the range " + toString(vr) + ".");
80                      } else {
81                          String message = getMessage();
82  
83                          if (message == null || message.isEmpty()) {
84                              message = msg + " is not in the allowed range " + toString(vr) + ".";
85                          }
86  
87                          throw new EnforcerRuleException(message);
88                      }
89                  } catch (InvalidVersionSpecificationException e) {
90                      throw new EnforcerRuleException(
91                              "The requested " + variableName + " version " + requiredVersionRange + " is invalid.", e);
92                  }
93              }
94          }
95      }
96  
97      protected static String toString(VersionRange vr) {
98          // as recommended version is used as lower bound in this context modify the string representation
99          if (vr.getRecommendedVersion() != null) {
100             return "[" + vr.getRecommendedVersion().toString() + ",)";
101         } else {
102             return vr.toString();
103         }
104     }
105 
106     @Override
107     public String getCacheId() {
108         if (version != null && !version.isEmpty()) {
109             // return the hashcodes of the parameter that matters
110             return "" + version.hashCode();
111         } else {
112             return "0";
113         }
114     }
115 
116     /**
117      * Gets the required version.
118      *
119      * @return the required version
120      */
121     public final String getVersion() {
122         return this.version;
123     }
124 
125     /**
126      * Specify the required version. Some examples are:
127      * <ul>
128      * <li><code>2.0.4</code> Version 2.0.4 and higher (different from Maven meaning)</li>
129      * <li><code>[2.0,2.1)</code> Versions 2.0 (included) to 2.1 (not included)</li>
130      * <li><code>[2.0,2.1]</code> Versions 2.0 to 2.1 (both included)</li>
131      * <li><code>[2.0.5,)</code> Versions 2.0.5 and higher</li>
132      * <li><code>(,2.0.5],[2.1.1,)</code> Versions up to 2.0.5 (included) and 2.1.1 or higher</li>
133      * </ul>
134      *
135      * @param theVersion the required version to set
136      */
137     public void setVersion(String theVersion) {
138         this.version = theVersion;
139     }
140 }