001package org.apache.maven.plugins.enforcer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Arrays;
023import java.util.Iterator;
024import java.util.List;
025
026import org.apache.commons.lang3.SystemUtils;
027import org.apache.maven.artifact.versioning.ArtifactVersion;
028import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
029import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
030import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
031import org.apache.maven.plugin.logging.Log;
032import org.codehaus.plexus.util.StringUtils;
033
034/**
035 * This rule checks that the Java version is allowed.
036 *
037 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
038 * @version $Id: RequireJavaVersion.java 1802595 2017-07-21 13:39:48Z rfscholte $
039 */
040public class RequireJavaVersion
041    extends AbstractVersionEnforcer
042{
043    @Override
044    public void execute( EnforcerRuleHelper helper )
045        throws EnforcerRuleException
046    {
047        String javaVersion = SystemUtils.JAVA_VERSION;
048        Log log = helper.getLog();
049
050        log.debug( "Detected Java String: '" + javaVersion + "'" );
051        javaVersion = normalizeJDKVersion( javaVersion );
052        log.debug( "Normalized Java String: '" + javaVersion + "'" );
053
054        ArtifactVersion detectedJdkVersion = new DefaultArtifactVersion( javaVersion );
055
056        log.debug( "Parsed Version: Major: " + detectedJdkVersion.getMajorVersion() + " Minor: "
057            + detectedJdkVersion.getMinorVersion() + " Incremental: " + detectedJdkVersion.getIncrementalVersion()
058            + " Build: " + detectedJdkVersion.getBuildNumber() + " Qualifier: " + detectedJdkVersion.getQualifier() );
059
060        enforceVersion( helper.getLog(), "JDK", getVersion(), detectedJdkVersion );
061    }
062
063    /**
064     * Converts a jdk string from 1.5.0-11b12 to a single 3 digit version like 1.5.0-11
065     *
066     * @param theJdkVersion to be converted.
067     * @return the converted string.
068     */
069    public static String normalizeJDKVersion( String theJdkVersion )
070    {
071
072        theJdkVersion = theJdkVersion.replaceAll( "_|-", "." );
073        String tokenArray[] = StringUtils.split( theJdkVersion, "." );
074        List<String> tokens = Arrays.asList( tokenArray );
075        StringBuffer buffer = new StringBuffer( theJdkVersion.length() );
076
077        Iterator<String> iter = tokens.iterator();
078        for ( int i = 0; i < tokens.size() && i < 4; i++ )
079        {
080            String section = (String) iter.next();
081            section = section.replaceAll( "[^0-9]", "" );
082
083            if ( StringUtils.isNotEmpty( section ) )
084            {
085                buffer.append( Integer.parseInt( section ) );
086
087                if ( i != 2 )
088                {
089                    buffer.append( '.' );
090                }
091                else
092                {
093                    buffer.append( '-' );
094                }
095            }
096        }
097
098        String version = buffer.toString();
099        version = StringUtils.stripEnd( version, "-" );
100        return StringUtils.stripEnd( version, "." );
101    }
102}