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