001    package org.apache.maven.model.profile.activation;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.maven.model.Activation;
023    import org.apache.maven.model.ActivationOS;
024    import org.apache.maven.model.Profile;
025    import org.apache.maven.model.building.ModelProblemCollector;
026    import org.apache.maven.model.profile.ProfileActivationContext;
027    import org.codehaus.plexus.component.annotations.Component;
028    import org.codehaus.plexus.util.Os;
029    
030    /**
031     * Determines profile activation based on the operating system of the current runtime platform.
032     * 
033     * @author Benjamin Bentmann
034     */
035    @Component( role = ProfileActivator.class, hint = "os" )
036    public class OperatingSystemProfileActivator
037        implements ProfileActivator
038    {
039    
040        public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
041        {
042            boolean active = false;
043    
044            Activation activation = profile.getActivation();
045    
046            if ( activation != null )
047            {
048                ActivationOS os = activation.getOs();
049    
050                if ( os != null )
051                {
052                    active = ensureAtLeastOneNonNull( os );
053    
054                    if ( active && os.getFamily() != null )
055                    {
056                        active = determineFamilyMatch( os.getFamily() );
057                    }
058                    if ( active && os.getName() != null )
059                    {
060                        active = determineNameMatch( os.getName() );
061                    }
062                    if ( active && os.getArch() != null )
063                    {
064                        active = determineArchMatch( os.getArch() );
065                    }
066                    if ( active && os.getVersion() != null )
067                    {
068                        active = determineVersionMatch( os.getVersion() );
069                    }
070                }
071            }
072    
073            return active;
074        }
075    
076        private boolean ensureAtLeastOneNonNull( ActivationOS os )
077        {
078            return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
079        }
080    
081        private boolean determineVersionMatch( String version )
082        {
083            String test = version;
084            boolean reverse = false;
085    
086            if ( test.startsWith( "!" ) )
087            {
088                reverse = true;
089                test = test.substring( 1 );
090            }
091    
092            boolean result = Os.isVersion( test );
093    
094            if ( reverse )
095            {
096                return !result;
097            }
098            else
099            {
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    }