View Javadoc
1   package org.apache.maven.model.profile.activation;
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.Arrays;
24  import java.util.List;
25  
26  import javax.inject.Named;
27  import javax.inject.Singleton;
28  
29  import org.apache.maven.model.Activation;
30  import org.apache.maven.model.Profile;
31  import org.apache.maven.model.building.ModelProblemCollector;
32  import org.apache.maven.model.building.ModelProblem.Severity;
33  import org.apache.maven.model.building.ModelProblem.Version;
34  import org.apache.maven.model.building.ModelProblemCollectorRequest;
35  import org.apache.maven.model.profile.ProfileActivationContext;
36  
37  /**
38   * Determines profile activation based on the version of the current Java runtime.
39   *
40   * @author Benjamin Bentmann
41   * @see Activation#getJdk()
42   */
43  @Named( "jdk-version" )
44  @Singleton
45  public class JdkVersionProfileActivator
46      implements ProfileActivator
47  {
48  
49      @Override
50      public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
51      {
52          Activation activation = profile.getActivation();
53  
54          if ( activation == null )
55          {
56              return false;
57          }
58  
59          String jdk = activation.getJdk();
60  
61          if ( jdk == null )
62          {
63              return false;
64          }
65  
66          String version = context.getSystemProperties().get( "java.version" );
67  
68          if ( version == null || version.length() <= 0 )
69          {
70              problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
71                      .setMessage( "Failed to determine Java version for profile " + profile.getId() )
72                      .setLocation( activation.getLocation( "jdk" ) ) );
73              return false;
74          }
75  
76          if ( jdk.startsWith( "!" ) )
77          {
78              return !version.startsWith( jdk.substring( 1 ) );
79          }
80          else if ( isRange( jdk ) )
81          {
82              return isInRange( version, getRange( jdk ) );
83          }
84          else
85          {
86              return version.startsWith( jdk );
87          }
88      }
89  
90      @Override
91      public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
92      {
93          Activation activation = profile.getActivation();
94  
95          if ( activation == null )
96          {
97              return false;
98          }
99  
100         String jdk = activation.getJdk();
101 
102         if ( jdk == null )
103         {
104             return false;
105         }
106         return true;
107     }
108 
109     private static boolean isInRange( String value, List<RangeValue> range )
110     {
111         int leftRelation = getRelationOrder( value, range.get( 0 ), true );
112 
113         if ( leftRelation == 0 )
114         {
115             return true;
116         }
117 
118         if ( leftRelation < 0 )
119         {
120             return false;
121         }
122 
123         return getRelationOrder( value, range.get( 1 ), false ) <= 0;
124     }
125 
126     private static int getRelationOrder( String value, RangeValue rangeValue, boolean isLeft )
127     {
128         if ( rangeValue.value.length() <= 0 )
129         {
130             return isLeft ? 1 : -1;
131         }
132 
133         value = value.replaceAll( "[^0-9\\.\\-\\_]", "" );
134 
135         List<String> valueTokens = new ArrayList<>( Arrays.asList( value.split( "[\\.\\-\\_]" ) ) );
136         List<String> rangeValueTokens = new ArrayList<>( Arrays.asList( rangeValue.value.split( "\\." ) ) );
137 
138         addZeroTokens( valueTokens, 3 );
139         addZeroTokens( rangeValueTokens, 3 );
140 
141         for ( int i = 0; i < 3; i++ )
142         {
143             int x = Integer.parseInt( valueTokens.get( i ) );
144             int y = Integer.parseInt( rangeValueTokens.get( i ) );
145             if ( x < y )
146             {
147                 return -1;
148             }
149             else if ( x > y )
150             {
151                 return 1;
152             }
153         }
154         if ( !rangeValue.closed )
155         {
156             return isLeft ? -1 : 1;
157         }
158         return 0;
159     }
160 
161     private static void addZeroTokens( List<String> tokens, int max )
162     {
163         while ( tokens.size() < max )
164         {
165             tokens.add( "0" );
166         }
167     }
168 
169     private static boolean isRange( String value )
170     {
171         return value.startsWith( "[" ) || value.startsWith( "(" );
172     }
173 
174     private static List<RangeValue> getRange( String range )
175     {
176         List<RangeValue> ranges = new ArrayList<>();
177 
178         for ( String token : range.split( "," ) )
179         {
180             if ( token.startsWith( "[" ) )
181             {
182                 ranges.add( new RangeValue( token.replace( "[", "" ), true ) );
183             }
184             else if ( token.startsWith( "(" ) )
185             {
186                 ranges.add( new RangeValue( token.replace( "(", "" ), false ) );
187             }
188             else if ( token.endsWith( "]" ) )
189             {
190                 ranges.add( new RangeValue( token.replace( "]", "" ), true ) );
191             }
192             else if ( token.endsWith( ")" ) )
193             {
194                 ranges.add( new RangeValue( token.replace( ")", "" ), false ) );
195             }
196             else if ( token.length() <= 0 )
197             {
198                 ranges.add( new RangeValue( "", false ) );
199             }
200         }
201         if ( ranges.size() < 2 )
202         {
203             ranges.add( new RangeValue( "99999999", false ) );
204         }
205         return ranges;
206     }
207 
208     private static class RangeValue
209     {
210         private String value;
211 
212         private boolean closed;
213 
214         RangeValue( String value, boolean closed )
215         {
216             this.value = value.trim();
217             this.closed = closed;
218         }
219 
220         @Override
221         public String toString()
222         {
223             return value;
224         }
225     }
226 
227 }