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