001package 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
022import org.apache.maven.model.Activation;
023import org.apache.maven.model.ActivationOS;
024import org.apache.maven.model.Profile;
025import org.apache.maven.model.building.ModelProblemCollector;
026import org.apache.maven.model.profile.ProfileActivationContext;
027import org.codehaus.plexus.component.annotations.Component;
028import 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 * @see ActivationOS
035 */
036@Component( role = ProfileActivator.class, hint = "os" )
037public class OperatingSystemProfileActivator
038    implements ProfileActivator
039{
040
041    public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
042    {
043        Activation activation = profile.getActivation();
044
045        if ( activation == null )
046        {
047            return false;
048        }
049
050        ActivationOS os = activation.getOs();
051
052        if ( os == null )
053        {
054            return false;
055        }
056
057        boolean active = ensureAtLeastOneNonNull( os );
058
059        if ( active && os.getFamily() != null )
060        {
061            active = determineFamilyMatch( os.getFamily() );
062        }
063        if ( active && os.getName() != null )
064        {
065            active = determineNameMatch( os.getName() );
066        }
067        if ( active && os.getArch() != null )
068        {
069            active = determineArchMatch( os.getArch() );
070        }
071        if ( active && os.getVersion() != null )
072        {
073            active = determineVersionMatch( os.getVersion() );
074        }
075
076        return active;
077    }
078
079    private boolean ensureAtLeastOneNonNull( ActivationOS os )
080    {
081        return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
082    }
083
084    private boolean determineVersionMatch( String version )
085    {
086        String test = version;
087        boolean reverse = false;
088
089        if ( test.startsWith( "!" ) )
090        {
091            reverse = true;
092            test = test.substring( 1 );
093        }
094
095        boolean result = Os.isVersion( test );
096
097        return reverse ? !result : result;
098    }
099
100    private boolean determineArchMatch( String arch )
101    {
102        String test = arch;
103        boolean reverse = false;
104
105        if ( test.startsWith( "!" ) )
106        {
107            reverse = true;
108            test = test.substring( 1 );
109        }
110
111        boolean result = Os.isArch( test );
112
113        return reverse ? !result : result;
114    }
115
116    private boolean determineNameMatch( String name )
117    {
118        String test = name;
119        boolean reverse = false;
120
121        if ( test.startsWith( "!" ) )
122        {
123            reverse = true;
124            test = test.substring( 1 );
125        }
126
127        boolean result = Os.isName( test );
128
129        return reverse ? !result : result;
130    }
131
132    private boolean determineFamilyMatch( String family )
133    {
134        String test = family;
135        boolean reverse = false;
136
137        if ( test.startsWith( "!" ) )
138        {
139            reverse = true;
140            test = test.substring( 1 );
141        }
142
143        boolean result = Os.isFamily( test );
144
145        return reverse ? !result : result;
146    }
147
148}