View Javadoc

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  class SelectorUtils
36  {
37  
38      static void parseList( String list, Collection includes, Collection excludes )
39      {
40          String[] tokens = ( list != null ) ? StringUtils.split( list, "," ) : new String[0];
41  
42          for ( int i = 0; i < tokens.length; i++ )
43          {
44              String token = tokens[i].trim();
45  
46              if ( token.startsWith( "!" ) )
47              {
48                  excludes.add( token.substring( 1 ) );
49              }
50              else
51              {
52                  includes.add( token );
53              }
54          }
55      }
56  
57      static boolean isOsFamily( String osSpec )
58      {
59          List includes = new ArrayList();
60          List excludes = new ArrayList();
61          parseList( osSpec, includes, excludes );
62  
63          return isOsFamily( includes, true ) && !isOsFamily( excludes, false );
64      }
65  
66      static boolean isOsFamily( List families, boolean defaultMatch )
67      {
68          if ( families != null && !families.isEmpty() )
69          {
70              for ( Iterator it = families.iterator(); it.hasNext(); )
71              {
72                  String family = (String) it.next();
73  
74                  if ( Os.isFamily( family ) )
75                  {
76                      return true;
77                  }
78              }
79  
80              return false;
81          }
82          else
83          {
84              return defaultMatch;
85          }
86      }
87  
88      static boolean isJreVersion( String jreSpec )
89      {
90          List includes = new ArrayList();
91          List excludes = new ArrayList();
92          parseList( jreSpec, includes, excludes );
93  
94          List jreVersion = parseVersion( System.getProperty( "java.version", "" ) );
95  
96          return isJreVersion( jreVersion, includes, true ) && !isJreVersion( jreVersion, excludes, false );
97      }
98  
99      static boolean isJreVersion( List jreVersion, List versionPatterns, boolean defaultMatch )
100     {
101         if ( versionPatterns != null && !versionPatterns.isEmpty() )
102         {
103             for ( Iterator it = versionPatterns.iterator(); it.hasNext(); )
104             {
105                 String versionPattern = (String) it.next();
106 
107                 if ( isJreVersion( jreVersion, versionPattern ) )
108                 {
109                     return true;
110                 }
111             }
112 
113             return false;
114         }
115         else
116         {
117             return defaultMatch;
118         }
119     }
120 
121     static boolean isJreVersion( List jreVersion, String versionPattern )
122     {
123         List checkVersion = parseVersion( versionPattern );
124 
125         if ( versionPattern.endsWith( "+" ) )
126         {
127             // 1.5+ <=> [1.5,)
128             return compareVersions( jreVersion, checkVersion ) >= 0;
129         }
130         else if ( versionPattern.endsWith( "-" ) )
131         {
132             // 1.5- <=> (,1.5)
133             return compareVersions( jreVersion, checkVersion ) < 0;
134         }
135         else
136         {
137             // 1.5 <=> [1.5,1.6)
138             return checkVersion.size() <= jreVersion.size()
139                 && checkVersion.equals( jreVersion.subList( 0, checkVersion.size() ) );
140         }
141     }
142 
143     static List parseVersion( String version )
144     {
145         version = version.replaceAll( "[^0-9]", "." );
146 
147         String[] tokens = StringUtils.split( version, "." );
148 
149         List numbers = new ArrayList();
150 
151         for ( int i = 0; i < tokens.length; i++ )
152         {
153             numbers.add( Integer.valueOf( tokens[i] ) );
154         }
155 
156         return numbers;
157     }
158 
159     static int compareVersions( List version1, List version2 )
160     {
161         for ( Iterator it1 = version1.iterator(), it2 = version2.iterator();; )
162         {
163             if ( !it1.hasNext() )
164             {
165                 return it2.hasNext() ? -1 : 0;
166             }
167             if ( !it2.hasNext() )
168             {
169                 return it1.hasNext() ? 1 : 0;
170             }
171 
172             Integer num1 = (Integer) it1.next();
173             Integer num2 = (Integer) it2.next();
174 
175             int rel = num1.compareTo( num2 );
176             if ( rel != 0 )
177             {
178                 return rel;
179             }
180         }
181     }
182 
183 }