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