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