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