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