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         return jdk != null;
103     }
104 
105     private static boolean isInRange( String value, List<RangeValue> range )
106     {
107         int leftRelation = getRelationOrder( value, range.get( 0 ), true );
108 
109         if ( leftRelation == 0 )
110         {
111             return true;
112         }
113 
114         if ( leftRelation < 0 )
115         {
116             return false;
117         }
118 
119         return getRelationOrder( value, range.get( 1 ), false ) <= 0;
120     }
121 
122     private static int getRelationOrder( String value, RangeValue rangeValue, boolean isLeft )
123     {
124         if ( rangeValue.value.length() <= 0 )
125         {
126             return isLeft ? 1 : -1;
127         }
128 
129         value = value.replaceAll( "[^0-9\\.\\-\\_]", "" );
130 
131         List<String> valueTokens = new ArrayList<>( Arrays.asList( value.split( "[\\.\\-\\_]" ) ) );
132         List<String> rangeValueTokens = new ArrayList<>( Arrays.asList( rangeValue.value.split( "\\." ) ) );
133 
134         addZeroTokens( valueTokens, 3 );
135         addZeroTokens( rangeValueTokens, 3 );
136 
137         for ( int i = 0; i < 3; i++ )
138         {
139             int x = Integer.parseInt( valueTokens.get( i ) );
140             int y = Integer.parseInt( rangeValueTokens.get( i ) );
141             if ( x < y )
142             {
143                 return -1;
144             }
145             else if ( x > y )
146             {
147                 return 1;
148             }
149         }
150         if ( !rangeValue.closed )
151         {
152             return isLeft ? -1 : 1;
153         }
154         return 0;
155     }
156 
157     private static void addZeroTokens( List<String> tokens, int max )
158     {
159         while ( tokens.size() < max )
160         {
161             tokens.add( "0" );
162         }
163     }
164 
165     private static boolean isRange( String value )
166     {
167         return value.startsWith( "[" ) || value.startsWith( "(" );
168     }
169 
170     private static List<RangeValue> getRange( String range )
171     {
172         List<RangeValue> ranges = new ArrayList<>();
173 
174         for ( String token : range.split( "," ) )
175         {
176             if ( token.startsWith( "[" ) )
177             {
178                 ranges.add( new RangeValue( token.replace( "[", "" ), true ) );
179             }
180             else if ( token.startsWith( "(" ) )
181             {
182                 ranges.add( new RangeValue( token.replace( "(", "" ), false ) );
183             }
184             else if ( token.endsWith( "]" ) )
185             {
186                 ranges.add( new RangeValue( token.replace( "]", "" ), true ) );
187             }
188             else if ( token.endsWith( ")" ) )
189             {
190                 ranges.add( new RangeValue( token.replace( ")", "" ), false ) );
191             }
192             else if ( token.length() <= 0 )
193             {
194                 ranges.add( new RangeValue( "", false ) );
195             }
196         }
197         if ( ranges.size() < 2 )
198         {
199             ranges.add( new RangeValue( "99999999", false ) );
200         }
201         return ranges;
202     }
203 
204     private static class RangeValue
205     {
206         private String value;
207 
208         private boolean closed;
209 
210         RangeValue( String value, boolean closed )
211         {
212             this.value = value.trim();
213             this.closed = closed;
214         }
215 
216         @Override
217         public String toString()
218         {
219             return value;
220         }
221     }
222 
223 }