Coverage Report - org.apache.maven.plugin.invoker.SelectorUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SelectorUtils
55%
29/53
52%
28/54
3,875
 
 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 java.util.ArrayList;
 23  
 import java.util.Collection;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 import org.codehaus.plexus.util.Os;
 28  
 import org.codehaus.plexus.util.StringUtils;
 29  
 
 30  
 /**
 31  
  * Provides utility methods for selecting build jobs based on environmental conditions.
 32  
  * 
 33  
  * @author Benjamin Bentmann
 34  
  */
 35  0
 class SelectorUtils
 36  
 {
 37  
 
 38  
     static void parseList( String list, Collection includes, Collection excludes )
 39  
     {
 40  2
         String[] tokens = ( list != null ) ? StringUtils.split( list, "," ) : new String[0];
 41  
 
 42  5
         for ( int i = 0; i < tokens.length; i++ )
 43  
         {
 44  3
             String token = tokens[i].trim();
 45  
 
 46  3
             if ( token.startsWith( "!" ) )
 47  
             {
 48  1
                 excludes.add( token.substring( 1 ) );
 49  
             }
 50  
             else
 51  
             {
 52  2
                 includes.add( token );
 53  
             }
 54  
         }
 55  2
     }
 56  
 
 57  
     static boolean isOsFamily( String osSpec )
 58  
     {
 59  0
         List includes = new ArrayList();
 60  0
         List excludes = new ArrayList();
 61  0
         parseList( osSpec, includes, excludes );
 62  
 
 63  0
         return isOsFamily( includes, true ) && !isOsFamily( excludes, false );
 64  
     }
 65  
 
 66  
     static boolean isOsFamily( List families, boolean defaultMatch )
 67  
     {
 68  0
         if ( families != null && !families.isEmpty() )
 69  
         {
 70  0
             for ( Iterator it = families.iterator(); it.hasNext(); )
 71  
             {
 72  0
                 String family = (String) it.next();
 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  
     static boolean isJreVersion( String jreSpec )
 89  
     {
 90  0
         List includes = new ArrayList();
 91  0
         List excludes = new ArrayList();
 92  0
         parseList( jreSpec, includes, excludes );
 93  
 
 94  0
         List jreVersion = parseVersion( System.getProperty( "java.version", "" ) );
 95  
 
 96  0
         return isJreVersion( jreVersion, includes, true ) && !isJreVersion( jreVersion, excludes, false );
 97  
     }
 98  
 
 99  
     static boolean isJreVersion( List jreVersion, List versionPatterns, boolean defaultMatch )
 100  
     {
 101  0
         if ( versionPatterns != null && !versionPatterns.isEmpty() )
 102  
         {
 103  0
             for ( Iterator it = versionPatterns.iterator(); it.hasNext(); )
 104  
             {
 105  0
                 String versionPattern = (String) it.next();
 106  
 
 107  0
                 if ( isJreVersion( jreVersion, versionPattern ) )
 108  
                 {
 109  0
                     return true;
 110  
                 }
 111  
             }
 112  
 
 113  0
             return false;
 114  
         }
 115  
         else
 116  
         {
 117  0
             return defaultMatch;
 118  
         }
 119  
     }
 120  
 
 121  
     static boolean isJreVersion( List jreVersion, String versionPattern )
 122  
     {
 123  12
         List checkVersion = parseVersion( versionPattern );
 124  
 
 125  12
         if ( versionPattern.endsWith( "+" ) )
 126  
         {
 127  
             // 1.5+ <=> [1.5,)
 128  4
             return compareVersions( jreVersion, checkVersion ) >= 0;
 129  
         }
 130  8
         else if ( versionPattern.endsWith( "-" ) )
 131  
         {
 132  
             // 1.5- <=> (,1.5)
 133  4
             return compareVersions( jreVersion, checkVersion ) < 0;
 134  
         }
 135  
         else
 136  
         {
 137  
             // 1.5 <=> [1.5,1.6)
 138  4
             return checkVersion.size() <= jreVersion.size()
 139  
                 && checkVersion.equals( jreVersion.subList( 0, checkVersion.size() ) );
 140  
         }
 141  
     }
 142  
 
 143  
     static List parseVersion( String version )
 144  
     {
 145  15
         version = version.replaceAll( "[^0-9]", "." );
 146  
 
 147  15
         String[] tokens = StringUtils.split( version, "." );
 148  
 
 149  15
         List numbers = new ArrayList();
 150  
 
 151  51
         for ( int i = 0; i < tokens.length; i++ )
 152  
         {
 153  36
             numbers.add( Integer.valueOf( tokens[i] ) );
 154  
         }
 155  
 
 156  15
         return numbers;
 157  
     }
 158  
 
 159  
     static int compareVersions( List version1, List version2 )
 160  
     {
 161  13
         for ( Iterator it1 = version1.iterator(), it2 = version2.iterator();; )
 162  
         {
 163  31
             if ( !it1.hasNext() )
 164  
             {
 165  4
                 return it2.hasNext() ? -1 : 0;
 166  
             }
 167  27
             if ( !it2.hasNext() )
 168  
             {
 169  3
                 return it1.hasNext() ? 1 : 0;
 170  
             }
 171  
 
 172  24
             Integer num1 = (Integer) it1.next();
 173  24
             Integer num2 = (Integer) it2.next();
 174  
 
 175  24
             int rel = num1.compareTo( num2 );
 176  24
             if ( rel != 0 )
 177  
             {
 178  6
                 return rel;
 179  
             }
 180  
         }
 181  
     }
 182  
 
 183  
 }