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          if (os == null) {
84              return false;
85          }
86          return true;
87      }
88  
89      private boolean ensureAtLeastOneNonNull(ActivationOS os) {
90          return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
91      }
92  
93      private boolean determineVersionMatch(String version) {
94          String test = version;
95          boolean reverse = false;
96  
97          if (test.startsWith("!")) {
98              reverse = true;
99              test = test.substring(1);
100         }
101 
102         boolean result = Os.isVersion(test);
103 
104         return reverse ? !result : result;
105     }
106 
107     private boolean determineArchMatch(String arch) {
108         String test = arch;
109         boolean reverse = false;
110 
111         if (test.startsWith("!")) {
112             reverse = true;
113             test = test.substring(1);
114         }
115 
116         boolean result = Os.isArch(test);
117 
118         return reverse ? !result : result;
119     }
120 
121     private boolean determineNameMatch(String name) {
122         String test = name;
123         boolean reverse = false;
124 
125         if (test.startsWith("!")) {
126             reverse = true;
127             test = test.substring(1);
128         }
129 
130         boolean result = Os.isName(test);
131 
132         return reverse ? !result : result;
133     }
134 
135     private boolean determineFamilyMatch(String family) {
136         String test = family;
137         boolean reverse = false;
138 
139         if (test.startsWith("!")) {
140             reverse = true;
141             test = test.substring(1);
142         }
143 
144         boolean result = Os.isFamily(test);
145 
146         return reverse ? !result : result;
147     }
148 }