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().get("os.name").toLowerCase(Locale.ENGLISH);
61          String actualOsArch = context.getSystemProperties().get("os.arch").toLowerCase(Locale.ENGLISH);
62          String actualOsVersion = context.getSystemProperties().get("os.version").toLowerCase(Locale.ENGLISH);
63  
64          if (active && os.getFamily() != null) {
65              active = determineFamilyMatch(os.getFamily(), actualOsName);
66          }
67          if (active && os.getName() != null) {
68              active = determineNameMatch(os.getName(), actualOsName);
69          }
70          if (active && os.getArch() != null) {
71              active = determineArchMatch(os.getArch(), actualOsArch);
72          }
73          if (active && os.getVersion() != null) {
74              active = determineVersionMatch(os.getVersion(), actualOsVersion);
75          }
76  
77          return active;
78      }
79  
80      @Override
81      public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
82          Activation activation = profile.getActivation();
83  
84          if (activation == null) {
85              return false;
86          }
87  
88          ActivationOS os = activation.getOs();
89  
90          return os != null;
91      }
92  
93      private boolean ensureAtLeastOneNonNull(ActivationOS os) {
94          return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
95      }
96  
97      private boolean determineVersionMatch(String expectedVersion, String actualVersion) {
98          String test = expectedVersion;
99          boolean reverse = false;
100         final boolean result;
101         if (test.startsWith(REGEX_PREFIX)) {
102             result = actualVersion.matches(test.substring(REGEX_PREFIX.length()));
103         } else {
104             if (test.startsWith("!")) {
105                 reverse = true;
106                 test = test.substring(1);
107             }
108             result = actualVersion.equals(test);
109         }
110 
111         return reverse != result;
112     }
113 
114     private boolean determineArchMatch(String expectedArch, String actualArch) {
115         String test = expectedArch;
116         boolean reverse = false;
117 
118         if (test.startsWith("!")) {
119             reverse = true;
120             test = test.substring(1);
121         }
122 
123         boolean result = actualArch.equals(test);
124 
125         return reverse != result;
126     }
127 
128     private boolean determineNameMatch(String expectedName, String actualName) {
129         String test = expectedName;
130         boolean reverse = false;
131 
132         if (test.startsWith("!")) {
133             reverse = true;
134             test = test.substring(1);
135         }
136 
137         boolean result = actualName.equals(test);
138 
139         return reverse != result;
140     }
141 
142     private boolean determineFamilyMatch(String family, String actualName) {
143         String test = family;
144         boolean reverse = false;
145 
146         if (test.startsWith("!")) {
147             reverse = true;
148             test = test.substring(1);
149         }
150 
151         boolean result = Os.isFamily(test, actualName);
152 
153         return reverse != result;
154     }
155 }