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