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 java.util.Locale;
25  
26  import org.apache.maven.model.Activation;
27  import org.apache.maven.model.ActivationOS;
28  import org.apache.maven.model.Profile;
29  import org.apache.maven.model.building.ModelProblemCollector;
30  import org.apache.maven.model.profile.ProfileActivationContext;
31  import org.apache.maven.utils.Os;
32  
33  /**
34   * Determines profile activation based on the operating system of the current runtime platform.
35   *
36   * @author Benjamin Bentmann
37   * @see ActivationOS
38   */
39  @Named("os")
40  @Singleton
41  public class OperatingSystemProfileActivator implements ProfileActivator {
42  
43      private static final String REGEX_PREFIX = "regex:";
44  
45      @Override
46      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
47          Activation activation = profile.getActivation();
48  
49          if (activation == null) {
50              return false;
51          }
52  
53          ActivationOS os = activation.getOs();
54  
55          if (os == null) {
56              return false;
57          }
58  
59          boolean active = ensureAtLeastOneNonNull(os);
60  
61          String actualOsName = context.getSystemProperties()
62                  .getOrDefault("os.name", Os.OS_NAME)
63                  .toLowerCase(Locale.ENGLISH);
64          String actualOsArch = context.getSystemProperties()
65                  .getOrDefault("os.arch", Os.OS_ARCH)
66                  .toLowerCase(Locale.ENGLISH);
67          String actualOsVersion = context.getSystemProperties()
68                  .getOrDefault("os.version", Os.OS_VERSION)
69                  .toLowerCase(Locale.ENGLISH);
70  
71          if (active && os.getFamily() != null) {
72              active = determineFamilyMatch(os.getFamily(), actualOsName);
73          }
74          if (active && os.getName() != null) {
75              active = determineNameMatch(os.getName(), actualOsName);
76          }
77          if (active && os.getArch() != null) {
78              active = determineArchMatch(os.getArch(), actualOsArch);
79          }
80          if (active && os.getVersion() != null) {
81              active = determineVersionMatch(os.getVersion(), actualOsVersion);
82          }
83  
84          return active;
85      }
86  
87      @Override
88      public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
89          Activation activation = profile.getActivation();
90  
91          if (activation == null) {
92              return false;
93          }
94  
95          ActivationOS os = activation.getOs();
96  
97          if (os == null) {
98              return false;
99          }
100         return true;
101     }
102 
103     private boolean ensureAtLeastOneNonNull(ActivationOS os) {
104         return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
105     }
106 
107     private boolean determineVersionMatch(String expectedVersion, String actualVersion) {
108         String test = expectedVersion;
109         boolean reverse = false;
110         final boolean result;
111         if (test.startsWith(REGEX_PREFIX)) {
112             result = actualVersion.matches(test.substring(REGEX_PREFIX.length()));
113         } else {
114             if (test.startsWith("!")) {
115                 reverse = true;
116                 test = test.substring(1);
117             }
118             result = actualVersion.equalsIgnoreCase(test);
119         }
120 
121         return reverse != result;
122     }
123 
124     private boolean determineArchMatch(String expectedArch, String actualArch) {
125         String test = expectedArch.toLowerCase(Locale.ENGLISH);
126         boolean reverse = false;
127 
128         if (test.startsWith("!")) {
129             reverse = true;
130             test = test.substring(1);
131         }
132 
133         boolean result = actualArch.equals(test);
134 
135         return reverse ? !result : result;
136     }
137 
138     private boolean determineNameMatch(String expectedName, String actualName) {
139         String test = expectedName.toLowerCase(Locale.ENGLISH);
140         boolean reverse = false;
141 
142         if (test.startsWith("!")) {
143             reverse = true;
144             test = test.substring(1);
145         }
146 
147         boolean result = actualName.equals(test);
148 
149         return reverse ? !result : result;
150     }
151 
152     private boolean determineFamilyMatch(String family, String actualName) {
153         String test = family.toLowerCase(Locale.ENGLISH);
154         boolean reverse = false;
155 
156         if (test.startsWith("!")) {
157             reverse = true;
158             test = test.substring(1);
159         }
160 
161         boolean result = Os.isFamily(test, actualName);
162 
163         return reverse ? !result : result;
164     }
165 }