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