View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.invoker;
20  
21  import org.apache.maven.plugins.invoker.AbstractInvokerMojo.ToolchainPrivateManager;
22  
23  /**
24   *
25   * @author Robert Scholte
26   *
27   */
28  class Selector {
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_TOOLCHAIN = 8;
36  
37      static final int SELECTOR_MULTI = 16;
38  
39      private final String actualMavenVersion;
40  
41      private final String actualJavaVersion;
42  
43      private final ToolchainPrivateManager toolchainPrivateManager;
44  
45      Selector(String actualMavenVersion, String actualJavaVersion, ToolchainPrivateManager toolchainPrivateManager) {
46          this.actualMavenVersion = actualMavenVersion;
47          this.actualJavaVersion = actualJavaVersion;
48          this.toolchainPrivateManager = toolchainPrivateManager;
49      }
50  
51      public int getSelection(InvokerProperties invokerProperties) {
52          if (!invokerProperties.isSelectorDefined(1)) {
53              return getGlobal(invokerProperties);
54          }
55  
56          for (int selectorIndex = 1; ; selectorIndex++) {
57              if (selectorIndex > 1 && !invokerProperties.isSelectorDefined(selectorIndex)) {
58                  break;
59              }
60  
61              int selection = 0;
62              if (!SelectorUtils.isMavenVersion(invokerProperties.getMavenVersion(selectorIndex), actualMavenVersion)) {
63                  selection |= SELECTOR_MAVENVERSION;
64              }
65  
66              if (!SelectorUtils.isJreVersion(invokerProperties.getJreVersion(selectorIndex), actualJavaVersion)) {
67                  selection |= SELECTOR_JREVERSION;
68              }
69  
70              if (!SelectorUtils.isOsFamily(invokerProperties.getOsFamily(selectorIndex))) {
71                  selection |= SELECTOR_OSFAMILY;
72              }
73  
74              if (!SelectorUtils.isToolchain(toolchainPrivateManager, invokerProperties.getToolchains(selectorIndex))) {
75                  selection |= SELECTOR_TOOLCHAIN;
76              }
77  
78              if (selection == 0) {
79                  return 0;
80              }
81          }
82          return SELECTOR_MULTI;
83      }
84  
85      /**
86       * Determines whether selector conditions of the specified invoker properties match the current environment.
87       *
88       * @param invokerProperties The invoker properties to check, must not be <code>null</code>.
89       * @return <code>0</code> if the job corresponding to the properties should be run, otherwise a bitwise value
90       *         representing the reason why it should be skipped.
91       */
92      private int getGlobal(InvokerProperties invokerProperties) {
93          int selection = 0;
94          if (!SelectorUtils.isMavenVersion(invokerProperties.getMavenVersion(), actualMavenVersion)) {
95              selection |= SELECTOR_MAVENVERSION;
96          }
97  
98          if (!SelectorUtils.isJreVersion(invokerProperties.getJreVersion(), actualJavaVersion)) {
99              selection |= SELECTOR_JREVERSION;
100         }
101 
102         if (!SelectorUtils.isOsFamily(invokerProperties.getOsFamily())) {
103             selection |= SELECTOR_OSFAMILY;
104         }
105 
106         if (!SelectorUtils.isToolchain(toolchainPrivateManager, invokerProperties.getToolchains())) {
107             selection |= SELECTOR_TOOLCHAIN;
108         }
109 
110         return selection;
111     }
112 }