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