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