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    @Override
042    public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
043    {
044        Activation activation = profile.getActivation();
045
046        if ( activation == null )
047        {
048            return false;
049        }
050
051        ActivationOS os = activation.getOs();
052
053        if ( os == null )
054        {
055            return false;
056        }
057
058        boolean active = ensureAtLeastOneNonNull( os );
059
060        if ( active && os.getFamily() != null )
061        {
062            active = determineFamilyMatch( os.getFamily() );
063        }
064        if ( active && os.getName() != null )
065        {
066            active = determineNameMatch( os.getName() );
067        }
068        if ( active && os.getArch() != null )
069        {
070            active = determineArchMatch( os.getArch() );
071        }
072        if ( active && os.getVersion() != null )
073        {
074            active = determineVersionMatch( os.getVersion() );
075        }
076
077        return active;
078    }
079
080    @Override
081    public boolean presentInConfig( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
082    {
083        Activation activation = profile.getActivation();
084
085        if ( activation == null )
086        {
087            return false;
088        }
089
090        ActivationOS os = activation.getOs();
091
092        if ( os == null )
093        {
094            return false;
095        }
096        return true;
097    }
098
099    private boolean ensureAtLeastOneNonNull( ActivationOS os )
100    {
101        return os.getArch() != null || os.getFamily() != null || os.getName() != null || os.getVersion() != null;
102    }
103
104    private boolean determineVersionMatch( String version )
105    {
106        String test = version;
107        boolean reverse = false;
108
109        if ( test.startsWith( "!" ) )
110        {
111            reverse = true;
112            test = test.substring( 1 );
113        }
114
115        boolean result = Os.isVersion( test );
116
117        return reverse ? !result : result;
118    }
119
120    private boolean determineArchMatch( String arch )
121    {
122        String test = arch;
123        boolean reverse = false;
124
125        if ( test.startsWith( "!" ) )
126        {
127            reverse = true;
128            test = test.substring( 1 );
129        }
130
131        boolean result = Os.isArch( test );
132
133        return reverse ? !result : result;
134    }
135
136    private boolean determineNameMatch( String name )
137    {
138        String test = name;
139        boolean reverse = false;
140
141        if ( test.startsWith( "!" ) )
142        {
143            reverse = true;
144            test = test.substring( 1 );
145        }
146
147        boolean result = Os.isName( test );
148
149        return reverse ? !result : result;
150    }
151
152    private boolean determineFamilyMatch( String family )
153    {
154        String test = family;
155        boolean reverse = false;
156
157        if ( test.startsWith( "!" ) )
158        {
159            reverse = true;
160            test = test.substring( 1 );
161        }
162
163        boolean result = Os.isFamily( test );
164
165        return reverse ? !result : result;
166    }
167
168}