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.Arrays;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.lang3.SystemUtils;
27  import org.apache.maven.artifact.versioning.ArtifactVersion;
28  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
29  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
30  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
31  import org.apache.maven.plugin.logging.Log;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  /**
35   * This rule checks that the Java version is allowed.
36   *
37   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
38   * @version $Id$
39   */
40  public class RequireJavaVersion
41      extends AbstractVersionEnforcer
42  {
43      @Override
44      public void execute( EnforcerRuleHelper helper )
45          throws EnforcerRuleException
46      {
47          String javaVersion = SystemUtils.JAVA_VERSION;
48          Log log = helper.getLog();
49  
50          log.debug( "Detected Java String: '" + javaVersion + "'" );
51          javaVersion = normalizeJDKVersion( javaVersion );
52          log.debug( "Normalized Java String: '" + javaVersion + "'" );
53  
54          ArtifactVersion detectedJdkVersion = new DefaultArtifactVersion( javaVersion );
55  
56          log.debug( "Parsed Version: Major: " + detectedJdkVersion.getMajorVersion() + " Minor: "
57              + detectedJdkVersion.getMinorVersion() + " Incremental: " + detectedJdkVersion.getIncrementalVersion()
58              + " Build: " + detectedJdkVersion.getBuildNumber() + " Qualifier: " + detectedJdkVersion.getQualifier() );
59  
60          enforceVersion( helper.getLog(), "JDK", getVersion(), detectedJdkVersion );
61      }
62  
63      /**
64       * Converts a jdk string from 1.5.0-11b12 to a single 3 digit version like 1.5.0-11
65       *
66       * @param theJdkVersion to be converted.
67       * @return the converted string.
68       */
69      public static String normalizeJDKVersion( String theJdkVersion )
70      {
71  
72          theJdkVersion = theJdkVersion.replaceAll( "_|-", "." );
73          String tokenArray[] = StringUtils.split( theJdkVersion, "." );
74          List<String> tokens = Arrays.asList( tokenArray );
75          StringBuffer buffer = new StringBuffer( theJdkVersion.length() );
76  
77          Iterator<String> iter = tokens.iterator();
78          for ( int i = 0; i < tokens.size() && i < 4; i++ )
79          {
80              String section = (String) iter.next();
81              section = section.replaceAll( "[^0-9]", "" );
82  
83              if ( StringUtils.isNotEmpty( section ) )
84              {
85                  buffer.append( Integer.parseInt( section ) );
86  
87                  if ( i != 2 )
88                  {
89                      buffer.append( '.' );
90                  }
91                  else
92                  {
93                      buffer.append( '-' );
94                  }
95              }
96          }
97  
98          String version = buffer.toString();
99          version = StringUtils.stripEnd( version, "-" );
100         return StringUtils.stripEnd( version, "." );
101     }
102 }