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.lang.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: RequireJavaVersion.java 1493575 2013-06-16 19:23:51Z rfscholte $
39   */
40  public class RequireJavaVersion
41      extends AbstractVersionEnforcer
42  {
43  
44      /*
45       * (non-Javadoc)
46       *
47       * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
48       */
49      public void execute( EnforcerRuleHelper helper )
50          throws EnforcerRuleException
51      {
52          String javaVersion = SystemUtils.JAVA_VERSION_TRIMMED;
53          Log log = helper.getLog();
54  
55          log.debug( "Detected Java String: " + javaVersion );
56          javaVersion = normalizeJDKVersion( javaVersion );
57          log.debug( "Normalized Java String: " + javaVersion );
58  
59          ArtifactVersion detectedJdkVersion = new DefaultArtifactVersion( javaVersion );
60  
61          log.debug( "Parsed Version: Major: " + detectedJdkVersion.getMajorVersion() + " Minor: "
62              + detectedJdkVersion.getMinorVersion() + " Incremental: " + detectedJdkVersion.getIncrementalVersion()
63              + " Build: " + detectedJdkVersion.getBuildNumber() + " Qualifier: " + detectedJdkVersion.getQualifier() );
64  
65          enforceVersion( helper.getLog(), "JDK", getVersion(), detectedJdkVersion );
66      }
67  
68      /**
69       * Converts a jdk string from 1.5.0-11b12 to a single 3 digit version like 1.5.0-11
70       *
71       * @param theJdkVersion to be converted.
72       * @return the converted string.
73       */
74      public static String normalizeJDKVersion( String theJdkVersion )
75      {
76  
77          theJdkVersion = theJdkVersion.replaceAll( "_|-", "." );
78          String tokenArray[] = StringUtils.split( theJdkVersion, "." );
79          List<String> tokens = Arrays.asList( tokenArray );
80          StringBuffer buffer = new StringBuffer( theJdkVersion.length() );
81  
82          Iterator<String> iter = tokens.iterator();
83          for ( int i = 0; i < tokens.size() && i < 4; i++ )
84          {
85              String section = (String) iter.next();
86              section = section.replaceAll( "[^0-9]", "" );
87  
88              if ( StringUtils.isNotEmpty( section ) )
89              {
90                  buffer.append( Integer.parseInt( section ) );
91  
92                  if ( i != 2 )
93                  {
94                      buffer.append( '.' );
95                  }
96                  else
97                  {
98                      buffer.append( '-' );
99                  }
100             }
101         }
102 
103         String version = buffer.toString();
104         version = StringUtils.stripEnd( version, "-" );
105         return StringUtils.stripEnd( version, "." );
106     }
107 }