~ubuntu-branches/ubuntu/utopic/maven-enforcer/utopic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package org.apache.maven.plugins.enforcer;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.SystemUtils;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.util.StringUtils;

/**
 * This rule checks that the Java version is allowed.
 *
 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
 * @version $Id: RequireJavaVersion.java 805190 2009-08-17 22:30:49Z hboutemy $
 */
public class RequireJavaVersion
    extends AbstractVersionEnforcer
{

    /*
     * (non-Javadoc)
     *
     * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
     */
    public void execute( EnforcerRuleHelper helper )
        throws EnforcerRuleException
    {
        String java_version = SystemUtils.JAVA_VERSION_TRIMMED;
        Log log = helper.getLog();

        log.debug( "Detected Java String: " + java_version );
        java_version = normalizeJDKVersion( java_version );
        log.debug( "Normalized Java String: " + java_version );

        ArtifactVersion detectedJdkVersion = new DefaultArtifactVersion( java_version );

        log.debug( "Parsed Version: Major: " + detectedJdkVersion.getMajorVersion() + " Minor: " +
            detectedJdkVersion.getMinorVersion() + " Incremental: " + detectedJdkVersion.getIncrementalVersion() +
            " Build: " + detectedJdkVersion.getBuildNumber() + " Qualifier: " + detectedJdkVersion.getQualifier() );

        enforceVersion( helper.getLog(), "JDK", version, detectedJdkVersion );
    }

    /**
     * Converts a jdk string from 1.5.0-11b12 to a single 3 digit version like 1.5.0-11
     *
     * @param theJdkVersion to be converted.
     * @return the converted string.
     */
    public static String normalizeJDKVersion( String theJdkVersion )
    {

        theJdkVersion = theJdkVersion.replaceAll( "_|-", "." );
        String tokenArray[] = StringUtils.split( theJdkVersion, "." );
        List tokens = Arrays.asList( tokenArray );
        StringBuffer buffer = new StringBuffer( theJdkVersion.length() );

        Iterator iter = tokens.iterator();
        for ( int i = 0; i < tokens.size() && i < 4; i++ )
        {
            String section = (String) iter.next();
            section = section.replaceAll( "[^0-9]", "" );

            if ( StringUtils.isNotEmpty( section ) )
            {
                buffer.append( Integer.parseInt( section ) );

                if ( i != 2 )
                {
                    buffer.append( '.' );
                }
                else
                {
                    buffer.append( '-' );
                }
            }
        }

        String version = buffer.toString();
        version = StringUtils.stripEnd( version, "-" );
        return StringUtils.stripEnd( version, "." );
    }
}