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