View Javadoc
1   package org.apache.maven.plugins.invoker;
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  /**
23   * 
24   * @author Robert Scholte
25   *
26   */
27  class Selector
28  {
29      static final int SELECTOR_MAVENVERSION = 1;
30  
31      static final int SELECTOR_JREVERSION = 2;
32  
33      static final int SELECTOR_OSFAMILY = 4;
34      
35      static final int SELECTOR_MULTI = 8;
36      
37      private final String actualMavenVersion;
38      
39      private final String actualJavaVersion;
40      
41      public Selector( String actualMavenVersion, String actualJavaVersion )
42      {
43          this.actualMavenVersion = actualMavenVersion;
44          this.actualJavaVersion = actualJavaVersion;
45      }
46      
47      public int getSelection( InvokerProperties invokerProperties ) 
48      {
49          if ( !invokerProperties.isSelectorDefined( 1 ) )
50          {
51              return getGlobal( invokerProperties );
52          }
53          
54          for ( int selectorIndex = 1;; selectorIndex++ )
55          {
56              if ( selectorIndex > 1 && !invokerProperties.isSelectorDefined( selectorIndex ) )
57              {
58                  break;
59              }
60              
61              int selection = 0;
62              if ( !SelectorUtils.isMavenVersion( invokerProperties.getMavenVersion( selectorIndex ),
63                                                  actualMavenVersion ) )
64              {
65                  selection |= SELECTOR_MAVENVERSION;
66              }
67  
68              if ( !SelectorUtils.isJreVersion( invokerProperties.getJreVersion( selectorIndex ), actualJavaVersion ) )
69              {
70                  selection |= SELECTOR_JREVERSION;
71              }
72  
73              if ( !SelectorUtils.isOsFamily( invokerProperties.getOsFamily( selectorIndex ) ) )
74              {
75                  selection |= SELECTOR_OSFAMILY;
76              }
77  
78              if ( selection == 0 )
79              {
80                  return 0;
81              }
82          }
83          return SELECTOR_MULTI;
84      }
85      
86      /**
87       * Determines whether selector conditions of the specified invoker properties match the current environment.
88       *
89       * @param invokerProperties The invoker properties to check, must not be <code>null</code>.
90       * @return <code>0</code> if the job corresponding to the properties should be run, otherwise a bitwise value
91       *         representing the reason why it should be skipped.
92       */
93      private int getGlobal( InvokerProperties invokerProperties )
94      {
95          int selection = 0;
96          if ( !SelectorUtils.isMavenVersion( invokerProperties.getMavenVersion(), actualMavenVersion ) )
97          {
98              selection |= SELECTOR_MAVENVERSION;
99          }
100 
101         if ( !SelectorUtils.isJreVersion( invokerProperties.getJreVersion(), actualJavaVersion.toString() ) )
102         {
103             selection |= SELECTOR_JREVERSION;
104         }
105 
106         if ( !SelectorUtils.isOsFamily( invokerProperties.getOsFamily() ) )
107         {
108             selection |= SELECTOR_OSFAMILY;
109         }
110 
111         return selection;
112     }
113 }