Coverage Report - org.apache.maven.plugin.invoker.SelectorUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SelectorUtils
48%
30/62
48%
28/58
4.4
 
 1  
 package org.apache.maven.plugin.invoker;
 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 org.apache.maven.project.MavenProject;
 23  
 import org.codehaus.plexus.util.Os;
 24  
 import org.codehaus.plexus.util.StringUtils;
 25  
 
 26  
 import java.util.ArrayList;
 27  
 import java.util.Collection;
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 import java.util.Properties;
 31  
 
 32  
 /**
 33  
  * Provides utility methods for selecting build jobs based on environmental conditions.
 34  
  *
 35  
  * @author Benjamin Bentmann
 36  
  */
 37  0
 class SelectorUtils
 38  
 {
 39  
 
 40  
     static void parseList( String list, Collection<String> includes, Collection<String> excludes )
 41  
     {
 42  2
         String[] tokens = ( list != null ) ? StringUtils.split( list, "," ) : new String[0];
 43  
 
 44  5
         for ( int i = 0; i < tokens.length; i++ )
 45  
         {
 46  3
             String token = tokens[i].trim();
 47  
 
 48  3
             if ( token.startsWith( "!" ) )
 49  
             {
 50  1
                 excludes.add( token.substring( 1 ) );
 51  
             }
 52  
             else
 53  
             {
 54  2
                 includes.add( token );
 55  
             }
 56  
         }
 57  2
     }
 58  
 
 59  
     static boolean isOsFamily( String osSpec )
 60  
     {
 61  0
         List<String> includes = new ArrayList<String>();
 62  0
         List<String> excludes = new ArrayList<String>();
 63  0
         parseList( osSpec, includes, excludes );
 64  
 
 65  0
         return isOsFamily( includes, true ) && !isOsFamily( excludes, false );
 66  
     }
 67  
 
 68  
     static boolean isOsFamily( List<String> families, boolean defaultMatch )
 69  
     {
 70  0
         if ( families != null && !families.isEmpty() )
 71  
         {
 72  0
             for ( String family : families )
 73  
             {
 74  0
                 if ( Os.isFamily( family ) )
 75  
                 {
 76  0
                     return true;
 77  
                 }
 78  
             }
 79  
 
 80  0
             return false;
 81  
         }
 82  
         else
 83  
         {
 84  0
             return defaultMatch;
 85  
         }
 86  
     }
 87  
 
 88  
     /**
 89  
      * Retrieves the current Maven version.
 90  
      * @return The current Maven version.
 91  
      */
 92  
     static String getMavenVersion()
 93  
     {
 94  
         try
 95  
         {
 96  
             // This relies on the fact that MavenProject is the in core classloader
 97  
             // and that the core classloader is for the maven-core artifact
 98  
             // and that should have a pom.properties file
 99  
             // if this ever changes, we will have to revisit this code.
 100  0
             Properties properties = new Properties();
 101  0
             properties.load( MavenProject.class.getClassLoader().getResourceAsStream(
 102  
                 "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) );
 103  0
             return StringUtils.trim( properties.getProperty( "version" ) );
 104  
         }
 105  0
         catch ( Exception e )
 106  
         {
 107  0
             return null;
 108  
         }
 109  
     }
 110  
 
 111  
     static boolean isMavenVersion( String mavenSpec )
 112  
     {
 113  0
         List<String> includes = new ArrayList<String>();
 114  0
         List<String> excludes = new ArrayList<String>();
 115  0
         parseList( mavenSpec, includes, excludes );
 116  
 
 117  0
         List<Integer> mavenVersionList = parseVersion( getMavenVersion() );
 118  
 
 119  0
         return isJreVersion( mavenVersionList, includes, true ) && !isJreVersion( mavenVersionList, excludes, false );
 120  
     }
 121  
 
 122  
     static boolean isJreVersion( String jreSpec )
 123  
     {
 124  0
         List<String> includes = new ArrayList<String>();
 125  0
         List<String> excludes = new ArrayList<String>();
 126  0
         parseList( jreSpec, includes, excludes );
 127  
 
 128  0
         List<Integer> jreVersion = parseVersion( System.getProperty( "java.version", "" ) );
 129  
 
 130  0
         return isJreVersion( jreVersion, includes, true ) && !isJreVersion( jreVersion, excludes, false );
 131  
     }
 132  
 
 133  
     static boolean isJreVersion( List<Integer> jreVersion, List<String> versionPatterns, boolean defaultMatch )
 134  
     {
 135  0
         if ( versionPatterns != null && !versionPatterns.isEmpty() )
 136  
         {
 137  0
             for ( String versionPattern : versionPatterns )
 138  
             {
 139  0
                 if ( isJreVersion( jreVersion, versionPattern ) )
 140  
                 {
 141  0
                     return true;
 142  
                 }
 143  
             }
 144  
 
 145  0
             return false;
 146  
         }
 147  
         else
 148  
         {
 149  0
             return defaultMatch;
 150  
         }
 151  
     }
 152  
 
 153  
     static boolean isJreVersion( List<Integer> jreVersion, String versionPattern )
 154  
     {
 155  12
         List<Integer> checkVersion = parseVersion( versionPattern );
 156  
 
 157  12
         if ( versionPattern.endsWith( "+" ) )
 158  
         {
 159  
             // 1.5+ <=> [1.5,)
 160  4
             return compareVersions( jreVersion, checkVersion ) >= 0;
 161  
         }
 162  8
         else if ( versionPattern.endsWith( "-" ) )
 163  
         {
 164  
             // 1.5- <=> (,1.5)
 165  4
             return compareVersions( jreVersion, checkVersion ) < 0;
 166  
         }
 167  
         else
 168  
         {
 169  
             // 1.5 <=> [1.5,1.6)
 170  4
             return checkVersion.size() <= jreVersion.size() && checkVersion.equals(
 171  
                 jreVersion.subList( 0, checkVersion.size() ) );
 172  
         }
 173  
     }
 174  
 
 175  
     static List<Integer> parseVersion( String version )
 176  
     {
 177  15
         version = version.replaceAll( "[^0-9]", "." );
 178  
 
 179  15
         String[] tokens = StringUtils.split( version, "." );
 180  
 
 181  15
         List<Integer> numbers = new ArrayList<Integer>();
 182  
 
 183  51
         for ( int i = 0; i < tokens.length; i++ )
 184  
         {
 185  36
             numbers.add( Integer.valueOf( tokens[i] ) );
 186  
         }
 187  
 
 188  15
         return numbers;
 189  
     }
 190  
 
 191  
     static int compareVersions( List<Integer> version1, List<Integer> version2 )
 192  
     {
 193  13
         for ( Iterator<Integer> it1 = version1.iterator(), it2 = version2.iterator(); ; )
 194  
         {
 195  31
             if ( !it1.hasNext() )
 196  
             {
 197  4
                 return it2.hasNext() ? -1 : 0;
 198  
             }
 199  27
             if ( !it2.hasNext() )
 200  
             {
 201  3
                 return it1.hasNext() ? 1 : 0;
 202  
             }
 203  
 
 204  24
             Integer num1 = it1.next();
 205  24
             Integer num2 = it2.next();
 206  
 
 207  24
             int rel = num1.compareTo( num2 );
 208  24
             if ( rel != 0 )
 209  
             {
 210  6
                 return rel;
 211  
             }
 212  18
         }
 213  
     }
 214  
 
 215  
 }