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 */
039public class RequireJavaVersion
040    extends AbstractVersionEnforcer
041{
042    @Override
043    public void execute( EnforcerRuleHelper helper )
044        throws EnforcerRuleException
045    {
046        String javaVersion = SystemUtils.JAVA_VERSION;
047        Log log = helper.getLog();
048
049        log.debug( "Detected Java String: '" + javaVersion + "'" );
050        javaVersion = normalizeJDKVersion( javaVersion );
051        log.debug( "Normalized Java String: '" + javaVersion + "'" );
052
053        ArtifactVersion detectedJdkVersion = new DefaultArtifactVersion( javaVersion );
054
055        log.debug( "Parsed Version: Major: " + detectedJdkVersion.getMajorVersion() + " Minor: "
056            + detectedJdkVersion.getMinorVersion() + " Incremental: " + detectedJdkVersion.getIncrementalVersion()
057            + " Build: " + detectedJdkVersion.getBuildNumber() + " Qualifier: " + detectedJdkVersion.getQualifier() );
058
059        enforceVersion( helper.getLog(), "JDK", getVersion(), detectedJdkVersion );
060    }
061
062    /**
063     * Converts a jdk string from 1.5.0-11b12 to a single 3 digit version like 1.5.0-11
064     *
065     * @param theJdkVersion to be converted.
066     * @return the converted string.
067     */
068    public static String normalizeJDKVersion( String theJdkVersion )
069    {
070
071        theJdkVersion = theJdkVersion.replaceAll( "_|-", "." );
072        String tokenArray[] = StringUtils.split( theJdkVersion, "." );
073        List<String> tokens = Arrays.asList( tokenArray );
074        StringBuilder buffer = new StringBuilder( theJdkVersion.length() );
075
076        Iterator<String> iter = tokens.iterator();
077        for ( int i = 0; i < tokens.size() && i < 4; i++ )
078        {
079            String section = iter.next();
080            section = section.replaceAll( "[^0-9]", "" );
081
082            if ( StringUtils.isNotEmpty( section ) )
083            {
084                buffer.append( Integer.parseInt( section ) );
085
086                if ( i != 2 )
087                {
088                    buffer.append( '.' );
089                }
090                else
091                {
092                    buffer.append( '-' );
093                }
094            }
095        }
096
097        String version = buffer.toString();
098        version = StringUtils.stripEnd( version, "-" );
099        return StringUtils.stripEnd( version, "." );
100    }
101}