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