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 org.apache.maven.model.Activation;
27  import org.apache.maven.model.Profile;
28  import org.apache.maven.model.building.ModelProblemCollector;
29  import org.apache.maven.model.building.ModelProblem.Severity;
30  import org.apache.maven.model.building.ModelProblem.Version;
31  import org.apache.maven.model.building.ModelProblemCollectorRequest;
32  import org.apache.maven.model.profile.ProfileActivationContext;
33  import org.codehaus.plexus.component.annotations.Component;
34  
35  /**
36   * Determines profile activation based on the version of the current Java runtime.
37   * 
38   * @author Benjamin Bentmann
39   * @see Activation#getJdk()
40   */
41  @Component( role = ProfileActivator.class, hint = "jdk-version" )
42  public class JdkVersionProfileActivator
43      implements ProfileActivator
44  {
45  
46      public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
47      {
48          Activation activation = profile.getActivation();
49  
50          if ( activation == null )
51          {
52              return false;
53          }
54  
55          String jdk = activation.getJdk();
56  
57          if ( jdk == null )
58          {
59              return false;
60          }
61  
62          String version = context.getSystemProperties().get( "java.version" );
63  
64          if ( version == null || version.length() <= 0 )
65          {
66              problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
67                      .setMessage( "Failed to determine Java version for profile " + profile.getId() )
68                      .setLocation( activation.getLocation( "jdk" ) ) );
69              return false;
70          }
71  
72          if ( jdk.startsWith( "!" ) )
73          {
74              return !version.startsWith( jdk.substring( 1 ) );
75          }
76          else if ( isRange( jdk ) )
77          {
78              return isInRange( version, getRange( jdk ) );
79          }
80          else
81          {
82              return version.startsWith( jdk );
83          }
84      }
85  
86      @Override
87      public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
88      {
89          Activation activation = profile.getActivation();
90  
91          if ( activation == null )
92          {
93              return false;
94          }
95  
96          String jdk = activation.getJdk();
97  
98          if ( jdk == null )
99          {
100             return false;
101         }
102         return true;
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<String>( Arrays.asList( value.split( "[\\.\\-\\_]" ) ) );
132         List<String> rangeValueTokens = new ArrayList<String>( 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<RangeValue>();
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         public String toString()
217         {
218             return value;
219         }
220     }
221 
222 }