View Javadoc

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