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.artifact.versioning.DefaultArtifactVersion;
023    import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
024    import org.apache.maven.artifact.versioning.VersionRange;
025    import org.apache.maven.model.Activation;
026    import org.apache.maven.model.Profile;
027    import org.codehaus.plexus.util.StringUtils;
028    
029    @Deprecated
030    public class JdkPrefixProfileActivator
031        extends DetectedProfileActivator
032    {
033        private static final String JDK_VERSION = System.getProperty( "java.version" );
034    
035        public boolean isActive( Profile profile )
036            throws ProfileActivationException
037        {
038            Activation activation = profile.getActivation();
039    
040            String jdk = activation.getJdk();
041    
042            // null case is covered by canDetermineActivation(), so we can do a straight startsWith() here.
043            if ( jdk.startsWith( "[" ) || jdk.startsWith( "(" ) )
044            {
045                try
046                {
047                    return matchJdkVersionRange( jdk );
048                }
049                catch ( InvalidVersionSpecificationException e )
050                {
051                    throw new ProfileActivationException( "Invalid JDK version in profile '" + profile.getId() + "': "
052                        + e.getMessage() );
053                }
054            }
055    
056            boolean reverse = false;
057    
058            if ( jdk.startsWith( "!" ) )
059            {
060                reverse = true;
061                jdk = jdk.substring( 1 );
062            }
063    
064            if ( getJdkVersion().startsWith( jdk ) )
065            {
066                return !reverse;
067            }
068            else
069            {
070                return reverse;
071            }
072        }
073    
074        private boolean matchJdkVersionRange( String jdk )
075            throws InvalidVersionSpecificationException
076        {
077            VersionRange jdkVersionRange = VersionRange.createFromVersionSpec( convertJdkToMavenVersion( jdk ) );
078            DefaultArtifactVersion jdkVersion = new DefaultArtifactVersion( convertJdkToMavenVersion( getJdkVersion() ) );
079            return jdkVersionRange.containsVersion( jdkVersion );
080        }
081    
082        private String convertJdkToMavenVersion( String jdk )
083        {
084            return jdk.replaceAll( "_", "-" );
085        }
086    
087        protected String getJdkVersion()
088        {
089            return JDK_VERSION;
090        }
091    
092        protected boolean canDetectActivation( Profile profile )
093        {
094            return profile.getActivation() != null && StringUtils.isNotEmpty( profile.getActivation().getJdk() );
095        }
096    
097    }