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.model.profile.activation;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import org.apache.maven.model.Activation;
25  import org.apache.maven.model.ActivationOS;
26  import org.apache.maven.model.Profile;
27  import org.apache.maven.model.building.ModelProblemCollector;
28  import org.apache.maven.model.profile.ProfileActivationContext;
29  import org.apache.maven.utils.Os;
30  
31  /**
32   * Determines profile activation based on the operating system of the current runtime platform.
33   *
34   * @see ActivationOS
35   */
36  @Named("os")
37  @Singleton
38  public class OperatingSystemProfileActivator implements ProfileActivator {
39  
40      @Override
41      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
42          Activation activation = profile.getActivation();
43  
44          if (activation == null) {
45              return false;
46          }
47  
48          ActivationOS os = activation.getOs();
49  
50          if (os == null) {
51              return false;
52          }
53  
54          boolean active = ensureAtLeastOneNonNull(os);
55  
56          if (active && os.getFamily() != null) {
57              active = determineFamilyMatch(os.getFamily());
58          }
59          if (active && os.getName() != null) {
60              active = determineNameMatch(os.getName());
61          }
62          if (active && os.getArch() != null) {
63              active = determineArchMatch(os.getArch());
64          }
65          if (active && os.getVersion() != null) {
66              active = determineVersionMatch(os.getVersion());
67          }
68  
69          return active;
70      }
71  
72      @Override
73      public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
74          Activation activation = profile.getActivation();
75  
76          if (activation == null) {
77              return false;
78          }
79  
80          ActivationOS os = activation.getOs();
81  
82          return os != null;
83      }
84  
85      private boolean ensureAtLeastOneNonNull(ActivationOS os) {
86          return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
87      }
88  
89      private boolean determineVersionMatch(String version) {
90          String test = version;
91          boolean reverse = false;
92  
93          if (test.startsWith("!")) {
94              reverse = true;
95              test = test.substring(1);
96          }
97  
98          boolean result = Os.OS_VERSION.equals(test);
99  
100         return reverse != result;
101     }
102 
103     private boolean determineArchMatch(String arch) {
104         String test = arch;
105         boolean reverse = false;
106 
107         if (test.startsWith("!")) {
108             reverse = true;
109             test = test.substring(1);
110         }
111 
112         boolean result = Os.OS_ARCH.equals(test);
113 
114         return reverse != result;
115     }
116 
117     private boolean determineNameMatch(String name) {
118         String test = name;
119         boolean reverse = false;
120 
121         if (test.startsWith("!")) {
122             reverse = true;
123             test = test.substring(1);
124         }
125 
126         boolean result = Os.OS_NAME.equals(test);
127 
128         return reverse != result;
129     }
130 
131     private boolean determineFamilyMatch(String family) {
132         String test = family;
133         boolean reverse = false;
134 
135         if (test.startsWith("!")) {
136             reverse = true;
137             test = test.substring(1);
138         }
139 
140         boolean result = Os.isFamily(test);
141 
142         return reverse != result;
143     }
144 }