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   */
35  @Component( role = ProfileActivator.class, hint = "os" )
36  public class OperatingSystemProfileActivator
37      implements ProfileActivator
38  {
39  
40      public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
41      {
42          boolean active = false;
43  
44          Activation activation = profile.getActivation();
45  
46          if ( activation != null )
47          {
48              ActivationOS os = activation.getOs();
49  
50              if ( os != null )
51              {
52                  active = ensureAtLeastOneNonNull( os );
53  
54                  if ( active && os.getFamily() != null )
55                  {
56                      active = determineFamilyMatch( os.getFamily() );
57                  }
58                  if ( active && os.getName() != null )
59                  {
60                      active = determineNameMatch( os.getName() );
61                  }
62                  if ( active && os.getArch() != null )
63                  {
64                      active = determineArchMatch( os.getArch() );
65                  }
66                  if ( active && os.getVersion() != null )
67                  {
68                      active = determineVersionMatch( os.getVersion() );
69                  }
70              }
71          }
72  
73          return active;
74      }
75  
76      private boolean ensureAtLeastOneNonNull( ActivationOS os )
77      {
78          return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
79      }
80  
81      private boolean determineVersionMatch( String version )
82      {
83          String test = version;
84          boolean reverse = false;
85  
86          if ( test.startsWith( "!" ) )
87          {
88              reverse = true;
89              test = test.substring( 1 );
90          }
91  
92          boolean result = Os.isVersion( test );
93  
94          if ( reverse )
95          {
96              return !result;
97          }
98          else
99          {
100             return result;
101         }
102     }
103 
104     private boolean determineArchMatch( String arch )
105     {
106         String test = arch;
107         boolean reverse = false;
108 
109         if ( test.startsWith( "!" ) )
110         {
111             reverse = true;
112             test = test.substring( 1 );
113         }
114 
115         boolean result = Os.isArch( test );
116 
117         if ( reverse )
118         {
119             return !result;
120         }
121         else
122         {
123             return result;
124         }
125     }
126 
127     private boolean determineNameMatch( String name )
128     {
129         String test = name;
130         boolean reverse = false;
131 
132         if ( test.startsWith( "!" ) )
133         {
134             reverse = true;
135             test = test.substring( 1 );
136         }
137 
138         boolean result = Os.isName( test );
139 
140         if ( reverse )
141         {
142             return !result;
143         }
144         else
145         {
146             return result;
147         }
148     }
149 
150     private boolean determineFamilyMatch( String family )
151     {
152         String test = family;
153         boolean reverse = false;
154 
155         if ( test.startsWith( "!" ) )
156         {
157             reverse = true;
158             test = test.substring( 1 );
159         }
160 
161         boolean result = Os.isFamily( test );
162 
163         if ( reverse )
164         {
165             return !result;
166         }
167         else
168         {
169             return result;
170         }
171     }
172 
173 }