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 org.apache.maven.model.Activation;
25  import org.apache.maven.model.ActivationOS;
26  import org.apache.maven.model.Profile;
27  import org.apache.maven.model.building.ModelProblemCollector;
28  import org.apache.maven.model.profile.ProfileActivationContext;
29  import org.codehaus.plexus.util.Os;
30  
31  /**
32   * Determines profile activation based on the operating system of the current runtime platform.
33   *
34   * @author Benjamin Bentmann
35   * @see ActivationOS
36   */
37  @Named("os")
38  @Singleton
39  public class OperatingSystemProfileActivator implements ProfileActivator {
40  
41      @Override
42      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
43          Activation activation = profile.getActivation();
44  
45          if (activation == null) {
46              return false;
47          }
48  
49          ActivationOS os = activation.getOs();
50  
51          if (os == null) {
52              return false;
53          }
54  
55          boolean active = ensureAtLeastOneNonNull(os);
56  
57          if (active && os.getFamily() != null) {
58              active = determineFamilyMatch(os.getFamily());
59          }
60          if (active && os.getName() != null) {
61              active = determineNameMatch(os.getName());
62          }
63          if (active && os.getArch() != null) {
64              active = determineArchMatch(os.getArch());
65          }
66          if (active && os.getVersion() != null) {
67              active = determineVersionMatch(os.getVersion());
68          }
69  
70          return active;
71      }
72  
73      @Override
74      public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
75          Activation activation = profile.getActivation();
76  
77          if (activation == null) {
78              return false;
79          }
80  
81          ActivationOS os = activation.getOs();
82  
83          return os != null;
84      }
85  
86      private boolean ensureAtLeastOneNonNull(ActivationOS os) {
87          return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
88      }
89  
90      private boolean determineVersionMatch(String version) {
91          String test = version;
92          boolean reverse = false;
93  
94          if (test.startsWith("!")) {
95              reverse = true;
96              test = test.substring(1);
97          }
98  
99          boolean result = Os.isVersion(test);
100 
101         return reverse != result;
102     }
103 
104     private boolean determineArchMatch(String arch) {
105         String test = arch;
106         boolean reverse = false;
107 
108         if (test.startsWith("!")) {
109             reverse = true;
110             test = test.substring(1);
111         }
112 
113         boolean result = Os.isArch(test);
114 
115         return reverse != result;
116     }
117 
118     private boolean determineNameMatch(String name) {
119         String test = name;
120         boolean reverse = false;
121 
122         if (test.startsWith("!")) {
123             reverse = true;
124             test = test.substring(1);
125         }
126 
127         boolean result = Os.isName(test);
128 
129         return reverse != result;
130     }
131 
132     private boolean determineFamilyMatch(String family) {
133         String test = family;
134         boolean reverse = false;
135 
136         if (test.startsWith("!")) {
137             reverse = true;
138             test = test.substring(1);
139         }
140 
141         boolean result = Os.isFamily(test);
142 
143         return reverse != result;
144     }
145 }