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 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  class SelectorUtils
38  {
39  
40      static void parseList( String list, Collection includes, Collection excludes )
41      {
42          String[] tokens = ( list != null ) ? StringUtils.split( list, "," ) : new String[0];
43  
44          for ( int i = 0; i < tokens.length; i++ )
45          {
46              String token = tokens[i].trim();
47  
48              if ( token.startsWith( "!" ) )
49              {
50                  excludes.add( token.substring( 1 ) );
51              }
52              else
53              {
54                  includes.add( token );
55              }
56          }
57      }
58  
59      static boolean isOsFamily( String osSpec )
60      {
61          List includes = new ArrayList();
62          List excludes = new ArrayList();
63          parseList( osSpec, includes, excludes );
64  
65          return isOsFamily( includes, true ) && !isOsFamily( excludes, false );
66      }
67  
68      static boolean isOsFamily( List families, boolean defaultMatch )
69      {
70          if ( families != null && !families.isEmpty() )
71          {
72              for ( Iterator it = families.iterator(); it.hasNext(); )
73              {
74                  String family = (String) it.next();
75  
76                  if ( Os.isFamily( family ) )
77                  {
78                      return true;
79                  }
80              }
81  
82              return false;
83          }
84          else
85          {
86              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             Properties properties = new Properties();
103             properties.load( MavenProject.class.getClassLoader().getResourceAsStream(
104                 "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) );
105             return StringUtils.trim( properties.getProperty( "version" ) );
106         }
107         catch ( Exception e )
108         {
109             return null;
110         }
111     }
112 
113     static boolean isMavenVersion( String mavenSpec )
114     {
115         List includes = new ArrayList();
116         List excludes = new ArrayList();
117         parseList( mavenSpec, includes, excludes );
118 
119         List mavenVersionList = parseVersion( getMavenVersion() );
120 
121         return isJreVersion( mavenVersionList, includes, true ) && !isJreVersion( mavenVersionList, excludes, false );
122     }
123 
124     static boolean isJreVersion( String jreSpec )
125     {
126         List includes = new ArrayList();
127         List excludes = new ArrayList();
128         parseList( jreSpec, includes, excludes );
129 
130         List jreVersion = parseVersion( System.getProperty( "java.version", "" ) );
131 
132         return isJreVersion( jreVersion, includes, true ) && !isJreVersion( jreVersion, excludes, false );
133     }
134 
135     static boolean isJreVersion( List jreVersion, List versionPatterns, boolean defaultMatch )
136     {
137         if ( versionPatterns != null && !versionPatterns.isEmpty() )
138         {
139             for ( Iterator it = versionPatterns.iterator(); it.hasNext(); )
140             {
141                 String versionPattern = (String) it.next();
142 
143                 if ( isJreVersion( jreVersion, versionPattern ) )
144                 {
145                     return true;
146                 }
147             }
148 
149             return false;
150         }
151         else
152         {
153             return defaultMatch;
154         }
155     }
156 
157     static boolean isJreVersion( List jreVersion, String versionPattern )
158     {
159         List checkVersion = parseVersion( versionPattern );
160 
161         if ( versionPattern.endsWith( "+" ) )
162         {
163             // 1.5+ <=> [1.5,)
164             return compareVersions( jreVersion, checkVersion ) >= 0;
165         }
166         else if ( versionPattern.endsWith( "-" ) )
167         {
168             // 1.5- <=> (,1.5)
169             return compareVersions( jreVersion, checkVersion ) < 0;
170         }
171         else
172         {
173             // 1.5 <=> [1.5,1.6)
174             return checkVersion.size() <= jreVersion.size() && checkVersion.equals(
175                 jreVersion.subList( 0, checkVersion.size() ) );
176         }
177     }
178 
179     static List parseVersion( String version )
180     {
181         version = version.replaceAll( "[^0-9]", "." );
182 
183         String[] tokens = StringUtils.split( version, "." );
184 
185         List numbers = new ArrayList();
186 
187         for ( int i = 0; i < tokens.length; i++ )
188         {
189             numbers.add( Integer.valueOf( tokens[i] ) );
190         }
191 
192         return numbers;
193     }
194 
195     static int compareVersions( List version1, List version2 )
196     {
197         for ( Iterator it1 = version1.iterator(), it2 = version2.iterator(); ; )
198         {
199             if ( !it1.hasNext() )
200             {
201                 return it2.hasNext() ? -1 : 0;
202             }
203             if ( !it2.hasNext() )
204             {
205                 return it1.hasNext() ? 1 : 0;
206             }
207 
208             Integer num1 = (Integer) it1.next();
209             Integer num2 = (Integer) it2.next();
210 
211             int rel = num1.compareTo( num2 );
212             if ( rel != 0 )
213             {
214                 return rel;
215             }
216         }
217     }
218 
219 }