Coverage Report - org.apache.maven.plugin.invoker.SelectorUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SelectorUtils
46%
37/79
51%
32/62
3,438
SelectorUtils$1
0%
0/2
N/A
3,438
 
 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.io.File;
 23  
 import java.io.FilenameFilter;
 24  
 import java.io.IOException;
 25  
 import java.net.MalformedURLException;
 26  
 import java.net.URL;
 27  
 import java.util.ArrayList;
 28  
 import java.util.Collection;
 29  
 import java.util.Iterator;
 30  
 import java.util.List;
 31  
 import java.util.Properties;
 32  
 
 33  
 import org.apache.maven.project.MavenProject;
 34  
 import org.codehaus.plexus.util.Os;
 35  
 import org.codehaus.plexus.util.StringUtils;
 36  
 
 37  
 /**
 38  
  * Provides utility methods for selecting build jobs based on environmental conditions.
 39  
  *
 40  
  * @author Benjamin Bentmann
 41  
  */
 42  0
 class SelectorUtils
 43  
 {
 44  
 
 45  
     static void parseList( String list, Collection<String> includes, Collection<String> excludes )
 46  
     {
 47  8
         String[] tokens = ( list != null ) ? StringUtils.split( list, "," ) : new String[0];
 48  
 
 49  14
         for ( int i = 0; i < tokens.length; i++ )
 50  
         {
 51  6
             String token = tokens[i].trim();
 52  
 
 53  6
             if ( token.startsWith( "!" ) )
 54  
             {
 55  2
                 excludes.add( token.substring( 1 ) );
 56  
             }
 57  
             else
 58  
             {
 59  4
                 includes.add( token );
 60  
             }
 61  
         }
 62  8
     }
 63  
 
 64  
     static boolean isOsFamily( String osSpec )
 65  
     {
 66  0
         List<String> includes = new ArrayList<String>();
 67  0
         List<String> excludes = new ArrayList<String>();
 68  0
         parseList( osSpec, includes, excludes );
 69  
 
 70  0
         return isOsFamily( includes, true ) && !isOsFamily( excludes, false );
 71  
     }
 72  
 
 73  
     static boolean isOsFamily( List<String> families, boolean defaultMatch )
 74  
     {
 75  0
         if ( families != null && !families.isEmpty() )
 76  
         {
 77  0
             for ( String family : families )
 78  
             {
 79  0
                 if ( Os.isFamily( family ) )
 80  
                 {
 81  0
                     return true;
 82  
                 }
 83  
             }
 84  
 
 85  0
             return false;
 86  
         }
 87  
         else
 88  
         {
 89  0
             return defaultMatch;
 90  
         }
 91  
     }
 92  
 
 93  
     /**
 94  
      * Retrieves the current Maven version.
 95  
      * @return The current Maven version.
 96  
      */
 97  
     static String getMavenVersion()
 98  
     {
 99  
         try
 100  
         {
 101  
             // This relies on the fact that MavenProject is the in core classloader
 102  
             // and that the core classloader is for the maven-core artifact
 103  
             // and that should have a pom.properties file
 104  
             // if this ever changes, we will have to revisit this code.
 105  0
             Properties properties = new Properties();
 106  0
             properties.load( MavenProject.class.getClassLoader().getResourceAsStream(
 107  
                 "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) );
 108  0
             return StringUtils.trim( properties.getProperty( "version" ) );
 109  
         }
 110  0
         catch ( Exception e )
 111  
         {
 112  0
             return null;
 113  
         }
 114  
     }
 115  
     
 116  
     static String getMavenVersion( File mavenHome )
 117  
     {
 118  0
         File mavenLib = new File( mavenHome, "lib" );
 119  0
         File[] jarFiles = mavenLib.listFiles( new FilenameFilter()
 120  0
         {
 121  
             public boolean accept( File dir, String name )
 122  
             {
 123  0
                 return name.endsWith( ".jar" );
 124  
             }
 125  
         } );
 126  
 
 127  0
         for ( File file : jarFiles )
 128  
         {
 129  
             try
 130  
             {
 131  
                 @SuppressWarnings( "deprecation" )
 132  0
                 URL url =
 133  
                     new URL( "jar:" + file.toURL().toExternalForm()
 134  
                         + "!/META-INF/maven/org.apache.maven/maven-core/pom.properties" );
 135  
 
 136  0
                 Properties properties = new Properties();
 137  0
                 properties.load( url.openStream() );
 138  0
                 String version = StringUtils.trim( properties.getProperty( "version" ) );
 139  0
                 if ( version != null )
 140  
                 {
 141  0
                     return version;
 142  
                 }
 143  
             }
 144  0
             catch ( MalformedURLException e )
 145  
             {
 146  
                 // ignore
 147  
             }
 148  0
             catch ( IOException e )
 149  
             {
 150  
                 // ignore
 151  0
             }
 152  
         }
 153  0
         return null;
 154  
     }
 155  
 
 156  
     static boolean isMavenVersion( String mavenSpec )
 157  
     {
 158  0
         return isMavenVersion( mavenSpec, getMavenVersion() );
 159  
     }
 160  
     
 161  
     static boolean isMavenVersion( String mavenSpec, String actualVersion )
 162  
     {
 163  0
         List<String> includes = new ArrayList<String>();
 164  0
         List<String> excludes = new ArrayList<String>();
 165  0
         parseList( mavenSpec, includes, excludes );
 166  
 
 167  0
         List<Integer> mavenVersionList = parseVersion( actualVersion );
 168  
 
 169  0
         return isJreVersion( mavenVersionList, includes, true ) && !isJreVersion( mavenVersionList, excludes, false );
 170  
     }
 171  
 
 172  
     static String getJreVersion()
 173  
     {
 174  0
         return System.getProperty( "java.version", "" );
 175  
     }
 176  
     
 177  
     static String getJreVersion( File javaHome )
 178  
     {
 179  
         //@todo detect actual version
 180  0
         return null;
 181  
     }
 182  
 
 183  
     static boolean isJreVersion( String jreSpec )
 184  
     {
 185  0
         return isJreVersion( jreSpec, getJreVersion() );
 186  
     }
 187  
     
 188  
     static boolean isJreVersion( String jreSpec, String actualJreVersion )
 189  
     {
 190  4
         List<String> includes = new ArrayList<String>();
 191  4
         List<String> excludes = new ArrayList<String>();
 192  4
         parseList( jreSpec, includes, excludes );
 193  
 
 194  4
         List<Integer> jreVersion = parseVersion( actualJreVersion );
 195  
 
 196  4
         return isJreVersion( jreVersion, includes, true ) && !isJreVersion( jreVersion, excludes, false );
 197  
     }
 198  
 
 199  
 
 200  
     static boolean isJreVersion( List<Integer> jreVersion, List<String> versionPatterns, boolean defaultMatch )
 201  
     {
 202  8
         if ( versionPatterns != null && !versionPatterns.isEmpty() )
 203  
         {
 204  0
             for ( String versionPattern : versionPatterns )
 205  
             {
 206  0
                 if ( isJreVersion( jreVersion, versionPattern ) )
 207  
                 {
 208  0
                     return true;
 209  
                 }
 210  
             }
 211  
 
 212  0
             return false;
 213  
         }
 214  
         else
 215  
         {
 216  8
             return defaultMatch;
 217  
         }
 218  
     }
 219  
 
 220  
     static boolean isJreVersion( List<Integer> jreVersion, String versionPattern )
 221  
     {
 222  24
         List<Integer> checkVersion = parseVersion( versionPattern );
 223  
 
 224  24
         if ( versionPattern.endsWith( "+" ) )
 225  
         {
 226  
             // 1.5+ <=> [1.5,)
 227  8
             return compareVersions( jreVersion, checkVersion ) >= 0;
 228  
         }
 229  16
         else if ( versionPattern.endsWith( "-" ) )
 230  
         {
 231  
             // 1.5- <=> (,1.5)
 232  8
             return compareVersions( jreVersion, checkVersion ) < 0;
 233  
         }
 234  
         else
 235  
         {
 236  
             // 1.5 <=> [1.5,1.6)
 237  8
             return checkVersion.size() <= jreVersion.size() && checkVersion.equals(
 238  
                 jreVersion.subList( 0, checkVersion.size() ) );
 239  
         }
 240  
     }
 241  
 
 242  
     static List<Integer> parseVersion( String version )
 243  
     {
 244  34
         version = version.replaceAll( "[^0-9]", "." );
 245  
 
 246  34
         String[] tokens = StringUtils.split( version, "." );
 247  
 
 248  34
         List<Integer> numbers = new ArrayList<Integer>();
 249  
 
 250  114
         for ( int i = 0; i < tokens.length; i++ )
 251  
         {
 252  80
             numbers.add( Integer.valueOf( tokens[i] ) );
 253  
         }
 254  
 
 255  34
         return numbers;
 256  
     }
 257  
 
 258  
     static int compareVersions( List<Integer> version1, List<Integer> version2 )
 259  
     {
 260  26
         for ( Iterator<Integer> it1 = version1.iterator(), it2 = version2.iterator(); ; )
 261  
         {
 262  62
             if ( !it1.hasNext() )
 263  
             {
 264  8
                 return it2.hasNext() ? -1 : 0;
 265  
             }
 266  54
             if ( !it2.hasNext() )
 267  
             {
 268  6
                 return it1.hasNext() ? 1 : 0;
 269  
             }
 270  
 
 271  48
             Integer num1 = it1.next();
 272  48
             Integer num2 = it2.next();
 273  
 
 274  48
             int rel = num1.compareTo( num2 );
 275  48
             if ( rel != 0 )
 276  
             {
 277  12
                 return rel;
 278  
             }
 279  36
         }
 280  
     }
 281  
 
 282  
 }