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