View Javadoc
1   package org.apache.maven.model.profile.activation;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.model.Activation;
23  import org.apache.maven.model.ActivationOS;
24  import org.apache.maven.model.Profile;
25  import org.apache.maven.model.building.ModelProblemCollector;
26  import org.apache.maven.model.profile.ProfileActivationContext;
27  import org.codehaus.plexus.component.annotations.Component;
28  import org.codehaus.plexus.util.Os;
29  
30  /**
31   * Determines profile activation based on the operating system of the current runtime platform.
32   *
33   * @author Benjamin Bentmann
34   * @see ActivationOS
35   */
36  @Component( role = ProfileActivator.class, hint = "os" )
37  public class OperatingSystemProfileActivator
38      implements ProfileActivator
39  {
40  
41      @Override
42      public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
43      {
44          Activation activation = profile.getActivation();
45  
46          if ( activation == null )
47          {
48              return false;
49          }
50  
51          ActivationOS os = activation.getOs();
52  
53          if ( os == null )
54          {
55              return false;
56          }
57  
58          boolean active = ensureAtLeastOneNonNull( os );
59  
60          if ( active && os.getFamily() != null )
61          {
62              active = determineFamilyMatch( os.getFamily() );
63          }
64          if ( active && os.getName() != null )
65          {
66              active = determineNameMatch( os.getName() );
67          }
68          if ( active && os.getArch() != null )
69          {
70              active = determineArchMatch( os.getArch() );
71          }
72          if ( active && os.getVersion() != null )
73          {
74              active = determineVersionMatch( os.getVersion() );
75          }
76  
77          return active;
78      }
79  
80      @Override
81      public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
82      {
83          Activation activation = profile.getActivation();
84  
85          if ( activation == null )
86          {
87              return false;
88          }
89  
90          ActivationOS os = activation.getOs();
91  
92          if ( os == null )
93          {
94              return false;
95          }
96          return true;
97      }
98  
99      private boolean ensureAtLeastOneNonNull( ActivationOS os )
100     {
101         return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
102     }
103 
104     private boolean determineVersionMatch( String version )
105     {
106         String test = version;
107         boolean reverse = false;
108 
109         if ( test.startsWith( "!" ) )
110         {
111             reverse = true;
112             test = test.substring( 1 );
113         }
114 
115         boolean result = Os.isVersion( test );
116 
117         return reverse ? !result : result;
118     }
119 
120     private boolean determineArchMatch( String arch )
121     {
122         String test = arch;
123         boolean reverse = false;
124 
125         if ( test.startsWith( "!" ) )
126         {
127             reverse = true;
128             test = test.substring( 1 );
129         }
130 
131         boolean result = Os.isArch( test );
132 
133         return reverse ? !result : result;
134     }
135 
136     private boolean determineNameMatch( String name )
137     {
138         String test = name;
139         boolean reverse = false;
140 
141         if ( test.startsWith( "!" ) )
142         {
143             reverse = true;
144             test = test.substring( 1 );
145         }
146 
147         boolean result = Os.isName( test );
148 
149         return reverse ? !result : result;
150     }
151 
152     private boolean determineFamilyMatch( String family )
153     {
154         String test = family;
155         boolean reverse = false;
156 
157         if ( test.startsWith( "!" ) )
158         {
159             reverse = true;
160             test = test.substring( 1 );
161         }
162 
163         boolean result = Os.isFamily( test );
164 
165         return reverse ? !result : result;
166     }
167 
168 }