View Javadoc
1   package org.apache.maven.plugin.eclipse.it;
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   * From org.apache.maven.plugin.invoker.SelectorUtils
41   *
42   * @author Benjamin Bentmann
43   */
44  class SelectorUtils
45  {
46  
47      static void parseList( String list, Collection<String> includes, Collection<String> excludes )
48      {
49          String[] tokens = ( list != null ) ? StringUtils.split( list, "," ) : new String[0];
50  
51          for ( int i = 0; i < tokens.length; i++ )
52          {
53              String token = tokens[i].trim();
54  
55              if ( token.startsWith( "!" ) )
56              {
57                  excludes.add( token.substring( 1 ) );
58              }
59              else
60              {
61                  includes.add( token );
62              }
63          }
64      }
65  
66      static boolean isOsFamily( String osSpec )
67      {
68          List<String> includes = new ArrayList<String>();
69          List<String> excludes = new ArrayList<String>();
70          parseList( osSpec, includes, excludes );
71  
72          return isOsFamily( includes, true ) && !isOsFamily( excludes, false );
73      }
74  
75      static boolean isOsFamily( List<String> families, boolean defaultMatch )
76      {
77          if ( families != null && !families.isEmpty() )
78          {
79              for ( String family : families )
80              {
81                  if ( Os.isFamily( family ) )
82                  {
83                      return true;
84                  }
85              }
86  
87              return false;
88          }
89          else
90          {
91              return defaultMatch;
92          }
93      }
94  
95      /**
96       * Retrieves the current Maven version.
97       * @return The current Maven version.
98       */
99      static String getMavenVersion()
100     {
101         try
102         {
103             // This relies on the fact that MavenProject is the in core classloader
104             // and that the core classloader is for the maven-core artifact
105             // and that should have a pom.properties file
106             // if this ever changes, we will have to revisit this code.
107             Properties properties = new Properties();
108             properties.load( MavenProject.class.getClassLoader().getResourceAsStream(
109                 "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) );
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 
202     static boolean isJreVersion( List<Integer> jreVersion, List<String> versionPatterns, boolean defaultMatch )
203     {
204         if ( versionPatterns != null && !versionPatterns.isEmpty() )
205         {
206             for ( String versionPattern : versionPatterns )
207             {
208                 if ( isJreVersion( jreVersion, versionPattern ) )
209                 {
210                     return true;
211                 }
212             }
213 
214             return false;
215         }
216         else
217         {
218             return defaultMatch;
219         }
220     }
221 
222     static boolean isJreVersion( List<Integer> jreVersion, String versionPattern )
223     {
224         List<Integer> checkVersion = parseVersion( versionPattern );
225 
226         if ( versionPattern.endsWith( "+" ) )
227         {
228             // 1.5+ <=> [1.5,)
229             return compareVersions( jreVersion, checkVersion ) >= 0;
230         }
231         else if ( versionPattern.endsWith( "-" ) )
232         {
233             // 1.5- <=> (,1.5)
234             return compareVersions( jreVersion, checkVersion ) < 0;
235         }
236         else
237         {
238             // 1.5 <=> [1.5,1.6)
239             return checkVersion.size() <= jreVersion.size() && checkVersion.equals(
240                 jreVersion.subList( 0, checkVersion.size() ) );
241         }
242     }
243 
244     static List<Integer> parseVersion( String version )
245     {
246         version = version.replaceAll( "[^0-9]", "." );
247 
248         String[] tokens = StringUtils.split( version, "." );
249 
250         List<Integer> numbers = new ArrayList<Integer>();
251 
252         for ( int i = 0; i < tokens.length; i++ )
253         {
254             numbers.add( Integer.valueOf( tokens[i] ) );
255         }
256 
257         return numbers;
258     }
259 
260     static int compareVersions( List<Integer> version1, List<Integer> version2 )
261     {
262         for ( Iterator<Integer> it1 = version1.iterator(), it2 = version2.iterator(); ; )
263         {
264             if ( !it1.hasNext() )
265             {
266                 return it2.hasNext() ? -1 : 0;
267             }
268             if ( !it2.hasNext() )
269             {
270                 return it1.hasNext() ? 1 : 0;
271             }
272 
273             Integer num1 = it1.next();
274             Integer num2 = it2.next();
275 
276             int rel = num1.compareTo( num2 );
277             if ( rel != 0 )
278             {
279                 return rel;
280             }
281         }
282     }
283 
284 }